MYSQL and PHP loop and grab just one item from a joint table?

ShiggyDooDah

I am currently building a custom CMS to help me learn PHP and MYSQL. I have two database tables 'users' and 'articles'. When the user submits an article, the field 'author_id' in the 'articles' table places to the users 'users_id' from the 'users' table. This way i can join the tables and get all the articles from that user. Now i am trying to make a feature section on the home page. I want to loop through all authors/users and get one article from that user. Here is a sample of my code...

$author= db::getInstance()->query("SELECT * FROM users, articles WHERE user_id = author_id"); 

   foreach($author->results() as $author) {
      echo $author->profile_img;
      echo $author->user_name
      echo $author->article_title; 
   }

This works fine if the user has only posted one article but if there are more than 1 then it will loop through all the posts of that user. I just want to echo 1 article from each user but not sure how i can achieve this. Can anyone point me in the right direction?

Many Thanks,

Louis Lombardi

Fakhruddin Ujjainwala

You can use MySql limit:

SELECT * FROM users, articles WHERE user_id = '$author_id' limit 0,1

Complete documentation: https://dev.mysql.com/doc/refman/5.5/en/limit-optimization.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql/php loop with multiple forms or just one?

From Dev

Grab one row from a table

From Dev

MySQL: Add a condition on a subquery from a joint table

From Dev

SQL select all from one table of joint tables

From Dev

grab just the cents from string

From Dev

grab just the cents from string

From Dev

PHP MySQL Select ID from one table and photo from another

From Dev

PHP for loop generating the radio buttons and I want just one selected from those buttons

From Dev

PHP foreach loop only outputting the last value from mysql table

From Dev

Displaying data in table format from mysql database with php loop

From Dev

Grab from table values

From Dev

Oracle how to grab columns for just one index?

From Dev

How to get just the fields from a table in MySQL?

From Dev

How to get just the fields from a table in MySQL?

From Dev

how to grab the not equal row from joined table in mysql?

From Dev

Transferring information from one table to another using PHP and MySQL

From Dev

Insert ID from one table to another in MySQL php

From Dev

Select one value from table and pass it to variable - php mysql

From Dev

Copy only specific rows from one table to another in PHP and MySQL

From Dev

PHP/MySQL - Get multiple fields from one entry in a table

From Dev

PHP + MySQL - one table holding reference to IDs from multiple tables

From Dev

how to get just one item from array of strings if the other is the same

From Dev

how can I highlight just one item from the ls output

From Dev

Grab complex data from mysql and show them using php

From Dev

How to get one value from table in php when table written in while loop?

From Dev

PHP & MySQL One item in multiple categories

From Dev

PHP or MySQL is fetching all rows from the tags table and not just what the intermediary term_tags table says it should

From Dev

PHP mysql how to join two tables to link name from one table and post from other table

From Dev

How to Select an item from Dropdown Menu using while Loop in Php Mysql

Related Related

  1. 1

    mysql/php loop with multiple forms or just one?

  2. 2

    Grab one row from a table

  3. 3

    MySQL: Add a condition on a subquery from a joint table

  4. 4

    SQL select all from one table of joint tables

  5. 5

    grab just the cents from string

  6. 6

    grab just the cents from string

  7. 7

    PHP MySQL Select ID from one table and photo from another

  8. 8

    PHP for loop generating the radio buttons and I want just one selected from those buttons

  9. 9

    PHP foreach loop only outputting the last value from mysql table

  10. 10

    Displaying data in table format from mysql database with php loop

  11. 11

    Grab from table values

  12. 12

    Oracle how to grab columns for just one index?

  13. 13

    How to get just the fields from a table in MySQL?

  14. 14

    How to get just the fields from a table in MySQL?

  15. 15

    how to grab the not equal row from joined table in mysql?

  16. 16

    Transferring information from one table to another using PHP and MySQL

  17. 17

    Insert ID from one table to another in MySQL php

  18. 18

    Select one value from table and pass it to variable - php mysql

  19. 19

    Copy only specific rows from one table to another in PHP and MySQL

  20. 20

    PHP/MySQL - Get multiple fields from one entry in a table

  21. 21

    PHP + MySQL - one table holding reference to IDs from multiple tables

  22. 22

    how to get just one item from array of strings if the other is the same

  23. 23

    how can I highlight just one item from the ls output

  24. 24

    Grab complex data from mysql and show them using php

  25. 25

    How to get one value from table in php when table written in while loop?

  26. 26

    PHP & MySQL One item in multiple categories

  27. 27

    PHP or MySQL is fetching all rows from the tags table and not just what the intermediary term_tags table says it should

  28. 28

    PHP mysql how to join two tables to link name from one table and post from other table

  29. 29

    How to Select an item from Dropdown Menu using while Loop in Php Mysql

HotTag

Archive