|brotherhood of LAN|
| 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.
| 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|
| 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.
| 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!