By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,722 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,722 IT Pros & Developers. It's quick & easy.

Question re getting a single value from a mysql database

P: n/a
If I run a mysql query that gets a single field from a row in a database
table, what is the easiest way to get the value from the result?

I normally do something like this:

$query = "SELECT name FROM users WHERE id = 1"
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);

Obviously this is one less line to write than

$row = mysql_fetch_row($result);
$name = $row['name'];

Is there a simpler way than using the list function with a single
parameter?

Thanks

Hamilton
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <fk********************@news.xtra.co.nz>,
"Spidah" <h.*********@eggstra.co.nz> wrote:
If I run a mysql query that gets a single field from a row in a database
table, what is the easiest way to get the value from the result?

I normally do something like this:

$query = "SELECT name FROM users WHERE id = 1"
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);

Obviously this is one less line to write than

$row = mysql_fetch_row($result);
$name = $row['name'];

Is there a simpler way than using the list function with a single
parameter?

Thanks

Hamilton


One line less is of course this:

$result = mysql_query("SELECT name FROM users WHERE id = 1");
list($name) = mysql_fetch_row($result);

Or even like this (though it borders on unreadable, I wouldn't recommend
it):

list($name) = mysql_fetch_row(mysql_query("SELECT name FROM users WHERE
id = 1"));

Personally, I prefer to use PEAR::DB, where you can do this:

$name = $database_object->getOne("SELECT name FROM users WHERE id = 1");
JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #2

P: n/a
Why not just get it like this, I also changed the SQL a bit.

$username = mysql_result(mysql_query("SELECT `name` FROM `users` WHERE
`id`='1' "), 0, 'name');
Jul 17 '05 #3

P: n/a
On Sun, 18 Apr 2004 19:39:33 +0100, MJaC <mj**@mjac.co.uk> wrote:
Why not just get it like this, I also changed the SQL a bit.

$username = mysql_result(mysql_query("SELECT `name` FROM `users` WHERE
`id`='1' "), 0, 'name');


Because that doesn't give you a way of checking for an error coming from the
mysql_query call.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4

P: n/a
> Andy Hassall wrote something silly:
Because that doesn't give you a way of checking for an error coming from the
mysql_query call.


Come on, why would there be an error with the query SELECT `name` FROM
`users` WHERE `id`='1'
Checking queries for errors also brings exploits to light, heard of SQL
Injections?

I see no problem, however IF you want to be picky, and you are likely to
get errors:

$username = mysql_query("SELECT `name` FROM `users` WHERE `id`='1' ") or
die("An unexpected error has occured!<br />" . mysql_error() . ");
$username = mysql_result($username, 0, 'name');
Jul 17 '05 #5

P: n/a
On Sun, 18 Apr 2004 22:14:54 +0100, MJaC <mj**@mjac.co.uk> wrote:
Andy Hassall wrote something silly:
Because that doesn't give you a way of checking for an error coming from the
mysql_query call.
Come on, why would there be an error with the query SELECT `name` FROM
`users` WHERE `id`='1'


Syntax errors. Wrongly named identifiers. Permissions. Out of memory or disk
space. Corruption. Bugs. Disconnection from database server. Other unexpected
circumstances.
Checking queries for errors also brings exploits to light, heard of SQL
Injections?
How does checking for errors result in exploits? That's absurd. Proceeding
past errors without checking results in, at best, unexpected behaviour as you
start passing FALSE to things that expect result set resources.
I see no problem, however IF you want to be picky, and you are likely to
get errors:

$username = mysql_query("SELECT `name` FROM `users` WHERE `id`='1' ") or
die("An unexpected error has occured!<br />" . mysql_error() . ");
$username = mysql_result($username, 0, 'name');


That is exactly what I meant. Check that mysql_query returns a valid result
set resource before attempting to use it in mysql_result.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.