linein designs

21 Mar, 2007

MySQL Return Single Data or Single Array with PHP

Posted by: john In: mysql|php|programing

Quite a few times during projects I only want to find a single value from the database. But rather than setting up WHILE loop, I’ve been using a quick function that does two things perfectly.

Here’s the function:

// returns single result
function mysql_one_data($query)
{
   $one=mysql_query($query);
   $r=mysql_fetch_row($one);
   return($r[0]);
}

// returns single array
function mysql_one_array($query)
{
   $one=mysql_query($query) or die (mysql_error());
   $r=mysql_fetch_array($one);
   return($r);
}

Say for example I wanted just the date of a record. All I have to do is make sure to call the function and then do the following:

// single value
$date = mysql_one_data("SELECT date FROM records WHERE id='2' LIMIT 1");

// single array
$date_info = mysql_one_array("SELECT firstname, lastname, date FROM records WHERE id='2' LIMIT 1");

// you can then use the single $date_info array like
echo $date_info['firstname'].' '.$date_info['lastname'].' ('.$date_info['date'].')';

Just let me know if you have any questions about this.

Similar Posts:

4 Comments »

Comment by WebGyver

Good stuff. I like it.

Sometimes, I just need to grab a quick COUNT(*), and I can see how this would come in handy. Or would you happen to know of any other time-and-resource-saving tips that would allow me to grab the actual COUNT(*) value quickly?

At any rate, I love this. Thank you.

 
Comment by Zach M

I couldn’t get the mysql_one_data function to work. Is this a built in function or a function that isn’t supported anymore? I like the idea of using this instead of having to use an array for one piece of data. It seems like a waste that way.

 
Comment by john

You need to have the mysql_one_data function declared before you call it. I usually store it in another file and just require that file when I know I’m going to use those two functions.

 
Comment by john

I would steer away from using COUNT(*). Try counting one field instead if possible – such as COUNT(id)

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post


  • Seren: Thanks - google did not fail me in finding this snippet (after 2 hours of 'why isn't this working!' before I choose to look for answers. That select
  • zdenis: type your scp command with nohup at the beginning: >nohup scp... enter your password stop temporarily the command with CTRL+z put on b
  • Criz: php script >> How to get the number of hours and minutes of a two set of time like this sample. 1st set of time : 8:00 to 17:00 2nd set of


Please Support Our Friends

Banner
FreshBooks

About

My name is John Veldboom and have been in design and web programing for just over 8 years now. Started off as a hobby in high school but it has developed into a full time job now. I always tell people that if I wasn't doing this at work each day, I would be at home doing it for free. I love it!

Please visit the contact page to drop me a message.