User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,968 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,739 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1034 | Replies: 11
Reply
Join Date: Nov 2007
Posts: 4
Reputation: emarkus is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
emarkus emarkus is offline Offline
Newbie Poster

Help Newbie

  #1  
Nov 22nd, 2007
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'];
}

?>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Cavite,Philippines
Posts: 508
Reputation: ryan_vietnow is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 68
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: Newbie

  #2  
Nov 22nd, 2007
this is your error dude:


$sql = "SELECT * FROM products;";


change that one to this one:


$sql = "SELECT * FROM products";
"death is the cure of all diseases..."
http://ryantetek.wordpress.com
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 378
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 38
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Newbie

  #3  
Nov 22nd, 2007
if i may ask why is using a semi colon at the end of an SQL statement a problem ? this is valid sql markup
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Nov 2007
Location: Baton Rouge, LA
Posts: 4
Reputation: TheZert is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
TheZert's Avatar
TheZert TheZert is offline Offline
Newbie Poster

Re: Newbie

  #4  
Nov 23rd, 2007
Originally Posted by Fungus1487 View Post
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.
One of the problems the internet has introduced is that in the electronic village all the village-idiots have internet access. -Peter Nelson
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 378
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 38
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Newbie

  #5  
Nov 23rd, 2007
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.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Nov 2007
Location: Indonesia
Posts: 15
Reputation: black_ip82 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
black_ip82's Avatar
black_ip82 black_ip82 is offline Offline
Newbie Poster

Re: Newbie

  #6  
Nov 24th, 2007
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.
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 636
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 71
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: Newbie

  #7  
Nov 24th, 2007
i saw someother errors in the code. did you solve the problem?
Last edited by kkeith29 : Nov 24th, 2007 at 4:10 am.
Reply With Quote  
Join Date: Aug 2007
Location: Cavite,Philippines
Posts: 508
Reputation: ryan_vietnow is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 68
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: Newbie

  #8  
Nov 24th, 2007
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);
"death is the cure of all diseases..."
http://ryantetek.wordpress.com
Reply With Quote  
Join Date: Nov 2007
Posts: 4
Reputation: emarkus is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
emarkus emarkus is offline Offline
Newbie Poster

Re: Newbie

  #9  
Dec 8th, 2007
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.
Reply With Quote  
Join Date: Aug 2007
Posts: 128
Reputation: hacker9801 is on a distinguished road 
Rep Power: 2
Solved Threads: 13
hacker9801 hacker9801 is offline Offline
Junior Poster

Re: Newbie

  #10  
Dec 8th, 2007
use mysql_error()...

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

  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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:10 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC