Jump to content

Archived

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

Jocka

pull one row from the db

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?

Share this post


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

Share this post


Link to post
Share on other sites
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_")"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
thats exactly what extract is supposed to do http://us2.php.net/extract
It sets the array into a variables. so with that code it should work like
$settings_value = $row['value'];
basically

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Regards
Huggie

Share this post


Link to post
Share on other sites
^ 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.

Share this post


Link to post
Share on other sites
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]

Regards
Huggie

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
^ thats actually what I ended up doing

Share this post


Link to post
Share on other sites

×

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.