PDA

View Full Version : PHP geting MySql data


endy
01-24-2005, 09:20 PM
I am having a problem geting my PHP script to retrive data from my sql database. I can write to it fine but not read. Ill post the code if anyone wants to take a stab at my problem, im sure it is somthing simple (i am new to this).

Ill start where I connect to the database, dont worry I defined all my connection variables earlier.
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to connect to database");
$query="INSERT INTO info VALUES ('$input')";
mysql_query($query);
$query="SELECT * FROM info;";
$result=mysql_query($query);
mysql_close();
//$calculation= "$result";
echo "&returnVal=$result";
?>
There you have it. It always gives me the error: "Resource id #3".
Thanks for any help!

Billy T
01-24-2005, 10:12 PM
that aint an error mate

Billy T
01-24-2005, 10:14 PM
need something like this

// Execute query
$result = mysql_query($query);

// If query was okay AND we have at least 1 item...
if ($result)
{


$i=0;

// Initialise variable to hold items
$data="";
// For each item returned from query...
while($row = mysql_fetch_array($result))
{

$data.="&galleryHeading$i=".$row['galleryHeading'];
$i++;

}
$data.="&numGalleries=$i";
// Output news items back to Flash
print "$data";
}

petefs
01-24-2005, 10:15 PM
$query="SELECT * FROM info;";
$result=mysql_query($query);

at this point, $result contains a result resource. You need to iterate through it and retrieve the values you want. I'm not sure what's in your info database, but let's assume there's a field called "name"

$query="SELECT * FROM info;";
$result=mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
echo $row['name'];
}
mysql_close();

this will echo every name returned from your query. Does that answer your question? ^_^

Also, welcome to the forums, and please use code tags so that your code is readable ^_^

petefs
01-24-2005, 10:16 PM
nevermind, I was slow :b hopefully BillyT's answer and mine help you understand ; )

Mateusz
01-24-2005, 10:17 PM
Use mysql_fetch_****** functions to get data.
Read some php + mysql manual or book, you need it, really :D