PHP/mySQL not working?

Here the PHP:
<?php
$db = mysql_connect(“name”,“user”,“pass”);
mysql_select_db(“links” ,$db);
$sql = mysql_query(“SELECT * FROM links” ,$db);
echo ("<table border =‘1’>");
echo ("<tr><td>url</td><td>description</td></tr>");
echo("<tr><td><a href=’$tablerows[1]’>$tablerows[1]</a></td><td>$tablerows[2]</td></tr> ");
echo “</table>”;
mysql_close($db);
?>

and i have a “links” table with 3 fields named id, url, and description…

and when i upload it to the server all i get is a two column table with “url” and “description” … the links that i created in the url field, their descriptions do not seem to appear???

What could be the problem…

well, for starters i wouldn’t put your password/username online…
ok, your problem is that you’ve never defined $tablerows, try this…

<?php
$db = mysql_connect("name","user","pass");
mysql_select_db("links" ,$db);
$tablerows = mysql_fetch_row(mysql_query("SELECT * FROM links WHERE id='number'" ,$db));
echo ("<table border ='1'>");
echo ("<tr><td>url</td><td>description</td></tr>");
echo("<tr><td><a href='$tablerows[1]'>$tablerows[1]</a></td><td>$tablerows[2]</td></tr> ");
echo "</table>";
mysql_close($db);
?>

The other problem you have is you must define which row you want to grab off the DB by using a WHERE statement in your SQL query, like this:
$tablerows = mysql_fetch_row(mysql_query(“SELECT * FROM links WHERE id=‘number’” ,$db));

thanks heheh oops i forgot to change the important values before posting…eeep! but i changed them now!!

anyways…thanks sooo much! you have been soo helpful to me :)) thanks!

ps. do you know where i could find some books or tutorials on php interacting with databases???

yep!
Check out http://php.net for descriptions of the many functions of PHP.
Check out http://www.w3schools.com/ for some tutorials on PHP and SQL statements.
take a look at
this tutorial for database interaction.
and lastely learn about relational databases, which is what kinda database mySQL is.
have fun :mu:

WOW!! i now love italy too hehehe :slight_smile: THANK YOU!!!

-Albert

lol, no problem!

hey i keep getting this error whenever i run the script:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in X:/Apache2/htdocs/docs/work/img_2004.php on line 13

the code is exactly as stated above!!

hmmm, i dont see any reason why it wouldn’t work…
Th only thing i can think of is, make sure than the password and username is correct…

yea the pass is ok, and so is the username…

FROM links WHERE id=‘number’" <---- NUMBER ???

ohhh, ok, well thats the problem. I assumed you changed that to a number.
The ‘number’ needs to be the row number that you want to grab, or the primary ID row.

can i add more than one statement, say i have a table with 5 rows of links and i want them all to be fetched and displayed onload???

by the way, i fixed the problem earlier, me = stupid! hehe i was typing in the db name wrong, so here’s a tip, never choose long db names!

sure, just use a loop to repeat the action…


for($x=0; $x!=$finalrow; $x++) {
$tablerows = mysql_fetch_row(mysql_query("SELECT * FROM links WHERE id='$x'" ,$db)); 
echo ("<table border ='1'>"); 
echo ("<tr><td>url</td><td>description</td></tr>"); 
echo("<tr><td><a href='$tablerows[1]'>$tablerows[1]</a></td><td>$tablerows[2]</td></tr> "); 
echo "</table>";
}

just define the $finalrow var, and it should work…

$finalrow i don’t know what that is???

your good at this :slight_smile:

oops sorry i got it $finalrow=5 or whatever! Thanks that worked like a charm!!