www.webdeveloper.com
Results 1 to 5 of 5

Thread: Storing result to a variable

  1. #1
    Join Date
    Jan 2006
    Posts
    78

    [solved] Storing result to a variable

    How do you store the result set from a SELECT statement into a variable

    This code works perfectly if I take out the assigning of the variable and just have it immediately echo the result. However, I need it to assign it to a variable and then echo the variable.

    PHP Code:
    $link mysql_connect('localhost''user_name''password');
    $db_selected mysql_select_db('Statistics'$link);
    $result mysql_query('SELECT `Name` FROM `Test Stats` WHERE 1 LIMIT 0, 30 ');
    if (!
    $result) {
       die(
    'Could not query:' mysql_error());
    }
    echo 
    "Name: ";
    $nme mysql_result($result0);
    echo 
    $nme
    mysql_close
    ($link); 
    Last edited by tenniskid493; 02-11-2006 at 02:51 PM.

  2. #2
    Join Date
    Oct 2005
    Posts
    25
    in asp you would create a recordset,
    Code:
    connTemp.Open pDatabaseConnectionString
    set rsTemp = server.createObject("adodb.recordset")
    rsTemp.open mySQL, connTemp
    pName=rsTemp("name")
    not sure with php though,

    also...are you missing a field in your sql statement that should be 1?
    mysql=SELECT name FROM Test Stats WHERE 1 LIMIT 0, 30
    Last edited by mojo3120; 02-10-2006 at 11:51 PM.

  3. #3
    Join Date
    Jan 2006
    Posts
    78
    no..the select statement is correct. The only thing that doesn't work is when I store it to the variable and then try to echo the variable. It works fine if I just echo the result without storing it to a variable. Anyone else know what I can do?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    Might just be that you're missing the closing ";" at the end of the echo line?

    Also, since your query is theoretically retrieving up to 30 rows, you could do something like the following to output them all:
    PHP Code:
    $link mysql_connect('localhost''user_name''password') or die("DB Connect Failed");
    $db_selected mysql_select_db('Statistics'$link) or die("DB Select Failed");
    $query 'SELECT `Name` FROM `Test Stats` WHERE 1 LIMIT 0, 30';
    $result mysql_query($query) or die("Query Failed: $query - " mysql_error();
    if(
    mysql_num_rows($result) > 0)
    {
      echo 
    "<h3>Names:</h3>\n<ul>\n";
      while(
    $row mysql_fetch_assoc($result))
      {
        echo 
    "<li>" $row['Name'] . "</li>\n";
      }
      echo 
    "</ul>\n";
    }
    else
    {
      echo 
    "<p>ERROR: No names returned by query.</p>\n";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jan 2006
    Posts
    78
    woops, yea that was what I was missing. Thanks for the help. Also, I wasn't actually retrieving 30 names, thats just put in standard from my host. I was actually only retrieving one name. Thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles