# PHP/mySQL not working?

**URL:** <https://forum.kirupa.com/t/php-mysql-not-working/64351>\
**Category:** programming\
**Created:** [September 15, 2004, 2:02pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351 "2004-09-15T14:02:17Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![red\_A](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@red\_A](https://forum.kirupa.com/u/red_A)\
**Post date:** [September 15, 2004, 2:02pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/1 "2004-09-15T14:02:17Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 15, 2004, 4:33pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/2 "2004-09-15T16:33:02Z")

</div>

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

```php
<?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));

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 15, 2004, 5:19pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/3 "2004-09-15T17:19:02Z")

</div>

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???

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 15, 2004, 5:28pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/4 "2004-09-15T17:28:04Z")

</div>

yep!  
Check out [http://php.net](http://php.net) for descriptions of the many functions of PHP.  
Check out [http://www.w3schools.com/](http://www.w3schools.com/) for some tutorials on PHP and SQL statements.  
take a look at  
[this tutorial](http://webmonkey.wired.com/webmonkey/99/21/index2a.html) for database interaction.  
and lastely learn about [relational databases](http://www.edm2.com/0612/msql7.html), which is what kinda database mySQL is.  
have fun :mu:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 15, 2004, 5:30pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/5 "2004-09-15T17:30:17Z")

</div>

WOW!! i now love italy too hehehe 🙂 THANK YOU!!!

-Albert

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 15, 2004, 5:32pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/6 "2004-09-15T17:32:54Z")

</div>

lol, no problem!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 11:27am UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/7 "2004-09-16T11:27:38Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 11:42am UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/8 "2004-09-16T11:42:35Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 12:37pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/9 "2004-09-16T12:37:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 12:45pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/10 "2004-09-16T12:45:54Z")

</div>

> [@iloveitaly](#):
>
> well, for starters i wouldn’t put your password/username online…  
> ok, your problem is that you’ve never defined $tablerows, try this…
> 
> ```php
> <?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));

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 1:20pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/11 "2004-09-16T13:20:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 2:11pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/12 "2004-09-16T14:11:59Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 2:16pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/13 "2004-09-16T14:16:27Z")

</div>

sure, just use a loop to repeat the action…

```php

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 2:25pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/14 "2004-09-16T14:25:39Z")

</div>

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

your good at this 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 2:27pm UTC](https://forum.kirupa.com/t/php-mysql-not-working/64351/15 "2004-09-16T14:27:47Z")

</div>

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