Solved: More MySQL Help

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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;





?>
 
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
 
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).
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Members online

Top