954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie

Can someone please help, I have this code below and every time i execute it i get this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/dbconnect1.php on line 14

I can work out what i am doin wrong.


<?php

$dbhost = "localhost";
$dbuser = "root";
$dbpassword= "spewguts";
$dbdatabase - "productsdb";

$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($database, $db);

$sql = "SELECT * FROM products;";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
echo $row['product'];
}

?>

emarkus
Newbie Poster
4 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

this is your error dude:

$sql = "SELECT * FROM products;";

change that one to this one: $sql = "SELECT * FROM products";

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

if i may ask why is using a semi colon at the end of an SQL statement a problem ? this is valid sql markup

Fungus1487
Posting Pro in Training
459 posts since Apr 2007
Reputation Points: 66
Solved Threads: 56
 
if i may ask why is using a semi colon at the end of an SQL statement a problem ? this is valid sql markup


The problem is that he has a semicolon inside the quotation marks and at the end of the statement. Check it out. :cool:

TheZert
Newbie Poster
4 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

i realise there is a semi colon inside the sql statement and after which is correct syntax for sql string then he appends the php line with a semi colon ?

does this affect the way php reads it?
if so then i have learned something new today.

Fungus1487
Posting Pro in Training
459 posts since Apr 2007
Reputation Points: 66
Solved Threads: 56
 
i realise there is a semi colon inside the sql statement and after which is correct syntax for sql string then he appends the php line with a semi colon ? does this affect the way php reads it?

Yes, php's mysql_query "automaticaly adds semicolon" after the query string.

black_ip82
Newbie Poster
15 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

i saw someother errors in the code. did you solve the problem?

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

yeah,this one:

$dbdatabase - "productsdb";

$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($database, $db);

should be: $dbdatabase = "productsdb";

$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

No i still have the same problem.
I have made some changes but sill the same error.

<?php

        $dbhost = "localhost";
        $dbuser = "root";
        $dbpassword= "spewguts";
        $dbdatabase = "productsdb";

        $db = mysql_connect($dbhost, $dbuser, $dbpassword);
        mysql_select_db($database, $db);

                $sql = "SELECT * FROM products";
                $result = mysql_query($sql);

                while ($row = mysql_fetch_assoc($result)) {
                        echo $row['product'];
                        }

?>


Any suggestions would be great.

emarkus
Newbie Poster
4 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

use mysql_error()...

oh, and you named your vars wrong.... ;)
see if it works now

<?php
  $dbhost = "localhost";
  $dbuser = "root";
  $dbpassword = "spewguts";
  $dbdatabase = "productsdb";

  $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Error: ".mysql_error());
        mysql_select_db($dbdatabase, $db);

                $sql = "SELECT * FROM products";
                $result = mysql_query($sql) or die("Error: ".mysql_error());

                while ($row = mysql_fetch_assoc($result)) {
                        echo $row['product'];
                        }
?>
hacker9801
Junior Poster
130 posts since Aug 2007
Reputation Points: 59
Solved Threads: 16
 
this is your error dude: $sql = "SELECT * FROM products;"; change that one to this one: $sql = "SELECT * FROM products";

It doesn't matter if you have a ; in your query.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

[QUOTE=emarkus;486870]No i still have the same problem.

mysql_select_db($database, $db);

should be:

mysql_select_db($dbdatabase,$db);
black_ip82
Newbie Poster
15 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You