homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
get a single entry from a db
Mr_Cat

5+ Year Member



 
Msg#: 4416623 posted 5:14 pm on Feb 11, 2012 (gmt 0)

Hi folks,

I think this is a php thing...

If I just want to fetch one single specific bit from a single row in my db, how is the best way?

If field 2 in row with ID 7 is the number 8

$query = "SELECT field2";
$query.= " FROM table1";
$query.= " WHERE row_ID = 7";
$result = mysql_query($query);
$almost_desired_bit = mysql_fetch_array($result);
$desired_bit = $almost_desired_bit['field2'];

seems a bit long winded somehow just to fetch the number 8, there must be a far simper way.

Would I be closer with mysql_fetch_object or something?

 

brotherhood of LAN

WebmasterWorld Administrator brotherhood_of_lan us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4416623 posted 8:42 pm on Feb 11, 2012 (gmt 0)

Why not just concatenate the query into one line, or better still, hard encode it into mysql_query()

$result = mysql_query("SELECT field2 FROM table1 WHERE row_ID = 7");
$desired_bit = array_shift(mysql_fetch_array($result));

You could put all that in one line.

Mr_Cat

5+ Year Member



 
Msg#: 4416623 posted 9:18 pm on Feb 11, 2012 (gmt 0)

Ah ha! Yea I'm just used to doing longer queries so I have the habbit of breaking them up into lines for readability even when they're hugely simple :)
It was the $result - $almost_desired_bit - $desired bit I was bothered about. array_shift is new to me but I see it just drops the ID from the beginning if I've understood it correctly.

Thanks! much sleeker.

brotherhood of LAN

WebmasterWorld Administrator brotherhood_of_lan us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4416623 posted 11:35 pm on Feb 11, 2012 (gmt 0)

yes, array_shift will return the first element of an array and reduce the array's length by one. It's worth noting that in your example, 'field2' would be returned in 2 array values as per [php.net...]



The one line solution would be:

$desired_bit = array_shift(mysql_fetch_array(mysql_query("SELECT field2 FROM table1 WHERE row_ID = 7")));

but it would probably be sensible to see that the query did execute without a problem.

Mr_Cat

5+ Year Member



 
Msg#: 4416623 posted 12:05 pm on Feb 12, 2012 (gmt 0)

Right, I was struggling a bit with the concept of exactly what an array is. I was looking at it that an array is something that contains a collection of data that's been retrieved from the database.

If field2 contains "info" and field3 contains "moreinfo"

$query = "SELECT field2, field3 FROM table1 WHERE row_ID = 7";
$result = mysql_fetch_array($query);
$info = $result['info'];
$moreinfo = $result['moreinfo'];

makes sense to distinguish the different data

but if I just want one little bit, "info" I didn't quite get why I had to put it in an array and then get it out again, whether there was something other than 'fetch_array' I should be using instead, and why $result didn't just automatically = "info" given there's nothing else in it rather than 'resource ID#2' or something similar when you try and echo it.

Sorry to be a bit slow on the uptake, I've looked at the manual, but when you say
It's worth noting that in your example, 'field2' would be returned in 2 array values


You mean it returns the id and the data as 2 values?

It's making more sense now tho :) Thanks!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved