Jump to content

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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.