1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: More MySQL Help

Discussion in 'Web Design & Development' started by lumentec, Sep 27, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    I'm just trying to retrieve some values from my MySQL database, but all I get is "Resource ID #2".

    Here's the PHP code, without the specifics like username, password, and database name.
    Code:
    <?php
    
    $connection = mysql_connect("localhost","user","password");
    
    if (!$connection)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    
    mysql_select_db("database",$connection);
    
    $result = mysql_query("SELECT * FROM user");
    
    echo $result;
    
    
    
    
    
    ?>
     
  2. TheRobatron

    TheRobatron

    Joined:
    Oct 25, 2007
    Messages:
    551
    Just a simple bit of code you need :) What you're doing is printing the ID of the query and what you need is to fetch the results:

    PHP:
    while ($arr mysql_fetch_array($result)) {

    // Copy the info to separate arrays/variables here

    }
    mysql_fetch_array() returns an array with both numeric and associative keys, but $arr will be replaced with the new values each time it loops round to get another row. To solve this, you need to extract the values from the array and put each column in its own array. Say your columns are called ID, name and email.

    PHP:
    $x 0;
    while (
    $arr mysql_fetch_array($result)) {

    $id[$x] = $arr['id'];

    $username[$x] = $arr['username'];

    $email[$x] = $arr['email'];

    $x++;

    }
    Now you have each row stored in three arrays, accessable by a single numeric key.

    HTH
     
  3. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
    Thanks! ;D
     
  4. TheRobatron

    TheRobatron

    Joined:
    Oct 25, 2007
    Messages:
    551
    No problem :) Is it all working correctly?
     
  5. lumentec

    lumentec Thread Starter

    Joined:
    Mar 23, 2008
    Messages:
    103
  6. TheRobatron

    TheRobatron

    Joined:
    Oct 25, 2007
    Messages:
    551
    I don't know if you spotted the error I made earlier, but I forgot to add 1 to $x each time it looped (my post is edited with the correct code).
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/753997

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice