944,114 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1741
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 22nd, 2007
0

Newbie

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

?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
emarkus is offline Offline
4 posts
since Nov 2007
Nov 22nd, 2007
0

Re: Newbie

this is your error dude:


$sql = "SELECT * FROM products;";


change that one to this one:


$sql = "SELECT * FROM products";
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Nov 22nd, 2007
0

Re: Newbie

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

Re: Newbie

Click to Expand / Collapse  Quote originally posted by Fungus1487 ...
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.
Last edited by TheZert; Nov 23rd, 2007 at 3:17 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheZert is offline Offline
4 posts
since Nov 2007
Nov 23rd, 2007
0

Re: Newbie

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.
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Nov 24th, 2007
0

Re: Newbie

Quote ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
black_ip82 is offline Offline
15 posts
since Nov 2007
Nov 24th, 2007
0

Re: Newbie

i saw someother errors in the code. did you solve the problem?
Last edited by kkeith29; Nov 24th, 2007 at 4:10 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Nov 24th, 2007
0

Re: Newbie

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);
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Dec 8th, 2007
0

Re: Newbie

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

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $dbhost = "localhost";
  4. $dbuser = "root";
  5. $dbpassword= "spewguts";
  6. $dbdatabase = "productsdb";
  7.  
  8. $db = mysql_connect($dbhost, $dbuser, $dbpassword);
  9. mysql_select_db($database, $db);
  10.  
  11. $sql = "SELECT * FROM products";
  12. $result = mysql_query($sql);
  13.  
  14. while ($row = mysql_fetch_assoc($result)) {
  15. echo $row['product'];
  16. }
  17.  
  18. ?>

Any suggestions would be great.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
emarkus is offline Offline
4 posts
since Nov 2007
Dec 8th, 2007
0

Re: Newbie

use mysql_error()...

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

php Syntax (Toggle Plain Text)
  1. <?php
  2. $dbhost = "localhost";
  3. $dbuser = "root";
  4. $dbpassword = "spewguts";
  5. $dbdatabase = "productsdb";
  6.  
  7. $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Error: ".mysql_error());
  8. mysql_select_db($dbdatabase, $db);
  9.  
  10. $sql = "SELECT * FROM products";
  11. $result = mysql_query($sql) or die("Error: ".mysql_error());
  12.  
  13. while ($row = mysql_fetch_assoc($result)) {
  14. echo $row['product'];
  15. }
  16. ?>
Last edited by hacker9801; Dec 8th, 2007 at 8:13 pm.
Reputation Points: 59
Solved Threads: 16
Junior Poster
hacker9801 is offline Offline
129 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: dynamic graphs in php
Next Thread in PHP Forum Timeline: attach file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC