Grow your CSS skills. Land your dream job.

Last updated on:

Return Only One Variable from MySQL Query

Function

function mysql_get_var($query,$y=0){
       $res = mysql_query($query);
       $row = mysql_fetch_array($res);
       mysql_free_result($res);
       $rec = $row[$y];
       return $rec;
}

Usage

$name = mysql_get_var("SELECT name from people where email = 'roger@freekrai.net'");

Will return the name field, so what gets returned will be "Roger" (if that was my name in the database).

Reference URL

Comments

  1. Permalink to comment#

    does this work with mulitple rows like if i wanted to hole name table could i use mysql_fectch_array with it?

    check out my site

    • Permalink to comment#

      No this function only works for returning one row.

      About the function, if the $y will only allow an integer to be given to specify which field to select then it would be better to use mysql_fetch_row instead of mysql_fetch_array.
      Also you can just do a:
      return $row[$y];
      instead of creating a new variable.

  2. I want to thank you from the bottom of my heart. I spent the last 4 hours trying to figure out what I was doing wrong, come to CSS-Tricks, found this, my problems are fixed. Thank you.

  3. Kevin Meek
    Permalink to comment#

    Even easier is to use the LIMIT keyword for MySQL so using the above example your query string would be…

    SELECT name FROM people WHERE email = ‘roger@freekrai.net’ LIMIT 1

    Which would give you the first record MySQL comes across. This can be changed using different orders such as
    ORDER BY id or
    ORDER BY date etc.
    MySQL is optimized to get the data faster this way than PHP. Your way MySQL would spend a lot of time maybe getting 10,00 records packaged and delivered whereas when using LIMIT it knows to only package 1 record fast.

  4. Mark
    Permalink to comment#

    Thank you. I’ve been digging for the longest time to find exactly this. The best thing of all is that it actually works!

  5. Dale
    Permalink to comment#

    THANK YOU SO VERY MUCH!!! This was very, extremely useful! thank you thank you.

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".