Getting info from Database?

Hi guys

Im doing a few tutorials trying to learn Php, I can get info into the database, but i cannot seem to return and present this info on my webpage. I have written some Php code called fetch.php the code is below:

<?php mysql_connect("localhost","root","jack"); mysql_select_db("faultreportdata"); $query = mysql_query("SELECT * FROM faults"); $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($query)) { echo $row['id'] . "-" . $row['roomno']; echo "
"; } ?>

But its not working, so time to call in the Cavalry. Thanks for any help.

Willo

This is probably what’s messing it up:
[php]$query = mysql_query(“SELECT * FROM faults”);
$result = mysql_query($query) or die(mysql_error());[/php]

Notice the duplicate mysql_query()? Should just be:

[php]
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {

}
[/php]

Also take note that the ‘mysql’ driver is in process of being depreciated. You should look for tutorials on mysqli or PDO instead…

Mysqli i will be looking this up thankyou, and for the help. I can get it to show the results but not on my results page i have created. when i run the script it shows results but upon a blank white page, and not my results page pls help

Not sure what you mean, where are the results appearing? What happens if you do a print_r() in your while loop?

[php]
while ($row = mysql_fetch_array($result)) {
print_r($row);
}
[/php]

This should show you the array set.

Hey gODzuki99

Many thanks for your help with this. i have changed the code as to below:

<?php mysql_connect("localhost","root","jack"); mysql_select_db("faultreportdata"); $query = mysql_query("SELECT * FROM faults"); $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo $row['id'] . "-" . $row['roomno']; echo "
"; } ?>

but now im getting an error meg on line 8 which is:

$result = mysql_query($query) or die(mysql_error());

Was this a copy/paste error? You still have the duplicate mysql_query.

[php]<?php

mysql_connect(“localhost”,“root”,“jack”);
mysql_select_db(“faultreportdata”);

$query =‘SELECT * FROM faults’;
$result = mysql_query($query) or die(mysql_error());
// Test: var_dump($result); should be a mysql resource
while ($row = mysql_fetch_assoc($result)) {
// Test: print_r($row); should show the current row’s array set
echo $row[‘id’] . ‘-’ . $row[‘roomno’] . ‘
’;
}

?>[/php]

Also, what’s the exact error?

Yes sorry this is code i have now:

<?php mysql_connect("localhost","root","jack"); mysql_select_db("faultreportdata"); $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo $row['pcid'] . "-" . $row['roomno']; echo "
"; } ?>

error is Query was empty

You’re missing the query now…

[php]$query =‘SELECT * FROM faults’;[/php]

You my Friend are both patient and a Star, its worked :smiley: and now returns results.

What i meant before is this Php file is an external file to the Results page i have. As of now when i run the Script you have helped me with it returns the fields but on a blank white screen. I want to try and get it to return the fields to my Results page. The results page is set-up in the way i want, if i use Dreamweaver and its recordset, and binding functions its has i can do it. But i want to learn to code it myself.

Thankyou godzuki

You’re welcome, glad it works :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service