I am having trouble accessing information from a database which I created. I am learning how to interact with a MySQL database using PHP. I created the database using PHP code, and then checked here to see if the database had been added, which it had. Then I added a few entries to a table, which went in no problem.
Now I am trying to access the information, but am getting no results. I am using the same code on each page to open the database and config, so this isn't the problem. I am using the following code to try and troubleshoot the problem:
@mysql_select_db($dbname) or die("Couldn't connect to DB");
$query="SELECT * FROM homepage";
$result=mysql_query($query);
if ($result=="")
{ echo "Result is blank"; }
else
{ echo $result; }
'homepage' is the name of my database. The result keeps coming out "Result is blank". I've tried replacing the '*' with the fieldnames, but still I get the same result. Can anyone tell me where I'm going wrong?
do "SELECT * FROM homepage" in phpmyadmin and see what it gives you.
try this:
<?php
mysql_connect("localhost","DB USERNAME","DB PASSWORD");
@mysql_select_db("DB NAME") or die( "Unable to select database");
$query="SELECT * FROM homepage";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$var=mysql_result($result,$i,"filed name");
if ($var=="")
{ echo "Result is blank"; }
else
{ echo $var; }
++$i;
}
?>
Thanks guys, I'll try those now...
Using the code:
<?php
include 'config.php';
include 'opendb.php';
@mysql_select_db($dbname) or die("Couldn't connect to DB");
$query="SELECT * FROM homepage";
$result= (mysql_query($query));
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$var=mysql_result($result,$i,"mname");
if ($var=="")
{ echo "Result is blank"; }
else
{ echo $var; }
}
mysql_close($conn);
?>
I get the following error message:
"Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in \\nas27ent\domains\e\egclan.co.uk\user\htdocs\viewdata.php on line 10"
This means that the mysql_query failed.
OK. How do I fix that?
Well don't I feel silly. ;D
You'd think I would've been able to suss that one myself. I just relooked through the code and this thread, and I remember saying that 'homepage' was the name of the database. Whoops.
I replaced this with 'member', (the name of the table), and everything works. I should slap myself about now. It's been a long time since I programmed anything.