Jump to content

pull one row from the db


Jocka

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/23019-pull-one-row-from-the-db/
Share on other sites

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());
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 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 to

echo $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!
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.