The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-DB - Query Multiple Rows With Same Column Value
Discuss Query Multiple Rows With Same Column Value in the PHP Development forum on Dev Shed. Query Multiple Rows With Same Column Value PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 8th, 2013, 03:45 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 64
Time spent in forums: 11 h 36 m 11 sec
Reputation Power: 1
|
|
|
PHP-DB - Query Multiple Rows With Same Column Value
I don't know why I'm having so much trouble with this, but I'm just trying to query the id of every row with a specific value in a given column.
For instance:
Table: images
imgID|userID
1 | 101
2 | 102
3 | 101
Let's say i wanted to query all the imgID's corresponding with userID 101, is there a way to query this without creating a PHP loop?
PHP Code:
$uploads = mysql_fetch_array(mysql_query("SELECT `imgID` FROM `images` where userID = '$user[0]'"));
This snippet of code will only return a single imgID, but i need all of them.
|

January 8th, 2013, 05:13 AM
|
|
|
Why would you not want to create a PHP loop? If you have multiple records being returned from a query then the only way to get them displayed is with a loop (while or foreach).
I would suggest you need to change your query:
PHP Code:
$uploads = mysql_query("SELECT `imgID` FROM `images` where userID = '$user[0]'");
while ($img_row = mysql_fetch_array($uploads)) {
//Do something here
}
Before someone else tells you, mysql functions are now deprecated (and will eventually, but way in the future I would hope, become obsolete) and you should look into using mysqli or PDO instead 
|

January 8th, 2013, 12:12 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 64
Time spent in forums: 11 h 36 m 11 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by simplypixie Before someone else tells you, mysql functions are now deprecated (and will eventually, but way in the future I would hope, become obsolete) and you should look into using mysqli or PDO instead  |
Thanks, is the mysqli syntax all the same, just mysqli instead of mysql?
|

January 8th, 2013, 12:15 PM
|
|
|
|
Generally yes but you need to ensure your db connection uses mysqli instead of mysql to instantiate it. If you search for it there is a lot of stuff for you to learn from.
|

January 8th, 2013, 12:16 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 64
Time spent in forums: 11 h 36 m 11 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by simplypixie Generally yes but you need to ensure your db connection uses mysqli instead of mysql to instantiate it. If you search for it there is a lot of stuff for you to learn from. |
Yeah I'll need to do some more research on that, thanks for the heads up.
|

January 8th, 2013, 12:56 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
if you simply replace "mysql_..." with "mysqli_...", you haven't really gained anything.
I think this is a misunderstanding: The mysql_ functions aren't bad just because they're obsolete. That alone wouldn't really be a problem, because they're likely to exist for many more years.
The problem is that they're difficult to use correctly. Every single value has to be escaped with mysql_real_escape_string() before being put into the query string. Since almost nobody gets that right (you didn't either, unfortunately), people using the old functions constantly end up with massive security holes.
The new extensions, on the other hand, support prepared statements, which are a safe and foolproof way of passing values to queries. This is why many of us promote dropping the old extension in favor of the new ones. It's not about wearing the latest fashion or something.
|

January 8th, 2013, 12:58 PM
|
|
|
Thank you for elaborating as I couldn't think how to word it all before 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|