Jump to content


Photo

pull one row from the db


  • Please log in to reply
12 replies to this topic

#1 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 04 October 2006 - 09:15 PM

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?

#2 tleisher

tleisher
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 04 October 2006 - 09:27 PM

Thats the only way to get one columns information... you could also do it this way:

<?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"];
}

?>

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' LIMIT 1") or die("Error: " . mysql_error());

#3 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 04 October 2006 - 09:30 PM

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
<?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"];
}

?>
what I'd like
<?php

$sql = mysql_query("SELECT value FROM settings WHERE key='about'") or die("Error: " . mysql_error());
$about = mysql_fetch_array($sql); // THIS IS IT

?>
I tried using extract but couldn't get it to work.. using
"extract($row, EXTR_PREFIX_SAME, "settings_")"


#4 tleisher

tleisher
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 04 October 2006 - 09:34 PM

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.

#5 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 04 October 2006 - 09:37 PM

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

#6 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 04 October 2006 - 10:48 PM

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.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 04 October 2006 - 11:13 PM

You can use mysql_fetch_row() but this will still give you an array of numbered columns.

$sql = "SELECT * FROM table WHERE id = 1";
$result = mysql_fetch_row($sql);
echo "$result[0] $result[1] $result[2]";

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 04 October 2006 - 11:15 PM

^ 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.

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 04 October 2006 - 11:24 PM

In that case give $result a shorter name too, like $c for say columns...

$sql = "SELECT * FROM table WHERE id = 1";
$c = mysql_fetch_row($sql);
echo "$c[0] $c[1] $c[2]";

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 05 October 2006 - 12:33 AM

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.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#11 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 05 October 2006 - 12:40 AM

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.


you can't do that, mysql_fetch_row() returns a numerical array only!

me!

#12 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 05:21 AM

You can do something like this.
<?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;
?>

JayBachatero
SMF Developer && Converter Specialist

#13 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 05 October 2006 - 05:00 PM

^ thats actually what I ended up doing




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users