Jocka Posted October 4, 2006 Share Posted October 4, 2006 I was making this script and I was trying to find an easier way to pull just ONE row from the database without using the mysql_fetch_array or assoc. I searched and searched. i could have sworn I used something before that only pulled one row and not all rows but I can't find it now. What I'm using now is something like:<?$results = mysql_query("SELECT value FROM settings WHERE key='about'") or die("Error: " . mysql_error());$about = mysql_fetch_array($results);$about = $about['value'];?>Is this my only option? Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/ Share on other sites More sharing options...
tleisher Posted October 4, 2006 Share Posted October 4, 2006 Thats the only way to get one columns information... you could also do it this way:[code]<?php$sql = mysql_query("SELECT value FROM settings WHERE key='about'") or die("Error: " . mysql_error());while($row = mysql_fetch_array($sql)){ echo $row["value"];}?>[/code]Or, if you're talking about getting an entire ROW (not a column, but the just one row) you could do this:$sql = mysql_query("SELECT value FROM settings WHERE key='about' [b]LIMIT 1[/b]") or die("Error: " . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-103952 Share on other sites More sharing options...
Jocka Posted October 4, 2006 Author Share Posted October 4, 2006 yea, the whole thing is, I wanted to find a way where I didn't have to add all that extra code to get my value. Example:your code[code]<?php$sql = mysql_query("SELECT value FROM settings WHERE key='about'") or die("Error: " . mysql_error());while($row = mysql_fetch_array($sql)){ echo $row["value"];}?>[/code]what I'd like[code]<?php$sql = mysql_query("SELECT value FROM settings WHERE key='about'") or die("Error: " . mysql_error());$about = mysql_fetch_array($sql); // THIS IS IT?>[/code]I tried using extract but couldn't get it to work.. using"extract($row, EXTR_PREFIX_SAME, "settings_")" Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-103956 Share on other sites More sharing options...
tleisher Posted October 4, 2006 Share Posted October 4, 2006 That's just going to return an array, there isn't a way to read just one row from one column and set it instantly to a variable. Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-103960 Share on other sites More sharing options...
Jocka Posted October 4, 2006 Author Share Posted October 4, 2006 thats exactly what extract is supposed to do http://us2.php.net/extractIt sets the array into a variables. so with that code it should work like$settings_value = $row['value'];basically Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-103961 Share on other sites More sharing options...
roopurt18 Posted October 4, 2006 Share Posted October 4, 2006 You still have to check the return value of mysql_query and handle errors. If you don't want to do it every time, write a function. You'll still have to check the return value of your own function though. Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-103990 Share on other sites More sharing options...
HuggieBear Posted October 4, 2006 Share Posted October 4, 2006 You can use mysql_fetch_row() but this will still give you an array of numbered columns.[code=php:0]$sql = "SELECT * FROM table WHERE id = 1";$result = mysql_fetch_row($sql);echo "$result[0] $result[1] $result[2]";[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-104012 Share on other sites More sharing options...
Jocka Posted October 4, 2006 Author Share Posted October 4, 2006 ^ that might work though. I just want to use something where I don't have to write numerous lines of code. I don't care if it is in an array really, i just don't want all the needless code. Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-104014 Share on other sites More sharing options...
HuggieBear Posted October 4, 2006 Share Posted October 4, 2006 In that case give $result a shorter name too, like $c for say columns...[code=php:0]$sql = "SELECT * FROM table WHERE id = 1";$c = mysql_fetch_row($sql);echo "$c[0] $c[1] $c[2]";[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-104020 Share on other sites More sharing options...
roopurt18 Posted October 5, 2006 Share Posted October 5, 2006 I would recommend against being so lazy as to want to write:echo $c[0]as opposed toecho $c['FName']The second line of code is going to make a lot more sense 6 months from now. Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-104043 Share on other sites More sharing options...
printf Posted October 5, 2006 Share Posted October 5, 2006 [quote author=roopurt18 link=topic=110526.msg446992#msg446992 date=1160008385]I would recommend against being so lazy as to want to write:echo $c[0]as opposed toecho $c['FName']The second line of code is going to make a lot more sense 6 months from now.[/quote]you can't do that, mysql_fetch_row() returns a numerical array only!me! Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-104045 Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 You can do something like this.[code]<?php$result = mysql_query("SELECT value FROM settings WHERE key = 'about' LIMIT 1") or die("Error: " . mysql_error());list ($about) = mysql_fetch_row($result);mysql_free_result($results);echo $about;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-104166 Share on other sites More sharing options...
Jocka Posted October 5, 2006 Author Share Posted October 5, 2006 ^ thats actually what I ended up doing Quote Link to comment https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/#findComment-104366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.