PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 8th, 2013, 03:45 AM
nbasso713 nbasso713 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 64 nbasso713 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old January 8th, 2013, 05:13 AM
simplypixie simplypixie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 104 simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 33 m 57 sec
Reputation Power: 11
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

Reply With Quote
  #3  
Old January 8th, 2013, 12:12 PM
nbasso713 nbasso713 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 64 nbasso713 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old January 8th, 2013, 12:15 PM
simplypixie simplypixie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 104 simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 33 m 57 sec
Reputation Power: 11
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.

Reply With Quote
  #5  
Old January 8th, 2013, 12:16 PM
nbasso713 nbasso713 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 64 nbasso713 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old January 8th, 2013, 12:56 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,854 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 15 h 27 m 29 sec
Reputation Power: 813
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.

Reply With Quote
  #7  
Old January 8th, 2013, 12:58 PM
simplypixie simplypixie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 104 simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level)simplypixie User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 33 m 57 sec
Reputation Power: 11
Thank you for elaborating as I couldn't think how to word it all before

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-DB - Query Multiple Rows With Same Column Value

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap