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'];
                        }

?>

Recommended Answers

All 11 Replies

this is your error dude: $sql = "SELECT * FROM products;"; change that one to this one: $sql = "SELECT * FROM products";

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

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:

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.

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.

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

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);

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.

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'];
                        }
?>

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.

No i still have the same problem.

mysql_select_db($database, $db);

should be:

mysql_select_db($dbdatabase,$db);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.