Free Web Hosting Forum
(#1 (permalink))
Old
Junior Member
DJDrew22200 is on a distinguished road
 
Posts: 5
Join Date: Oct 2008
Default mysql_fetch_array error - 10-09-2008, 03:05 PM

Hi there,

I have this on my webpage:

PHP Code:
<?php

$selectnews 
mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");

                while (
$getnews mysql_fetch_array($selectnews)){

                        echo 
"<li><h3>";
                        echo 
$getnews["date"];
                        echo 
"</h3><p>";
                        echo 
$getnews["message"];
                        echo 
"</p></li>";

                }

?>
And I keep getting this message:

Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3674547/public_html/footer.php on line 45
I did some research on Google and I think it's having a problem connecting to the database, but I'm not 100% sure... This is how I structured the mysql_connect code:

PHP Code:
<?php
$dbhost 
'mysql3.000webhost.com';
$dbuser 'a3674547_****';
$dbpass '****';

$connect mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());

$dbname 'a3674547_****';
mysql_select_db($dbname) or die(mysql_error());

?>
Does anyone know what's wrong?
Reply With Quote
(#2 (permalink))
Old
Junior Member
flames is on a distinguished road
 
Posts: 3
Join Date: Oct 2008
Default 10-09-2008, 06:21 PM

change

PHP Code:
$dbhost 'mysql3.000webhost.com'
and
$connect mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error()); 
and
mysql_select_db($dbname) or die(mysql_error()); 
PHP Code:
$dbhost 'localhost'
and
$connect mysql_connect($dbhost$dbuser$dbpass) );
and
mysql_select_db($dbname,$connect) or die(mysql_error()); 
Reply With Quote
(#3 (permalink))
Old
Member
Devastator is on a distinguished road
 
Posts: 50
Join Date: Aug 2008
Default 10-09-2008, 10:36 PM

I would probably add a "or die(mysql_error())" to the mysql_query line. The fact that there are no errors during the connection and database selection tells me that there is nothing wrong with that section of the code.

In fact, I'm led to believe that the mysql_query is returning a null. With $selectnews being null, mysql_fetch_array complains because there's nothing there to fetch from.
Reply With Quote
(#4 (permalink))
Old
Junior Member
CKPD is on a distinguished road
 
Posts: 19
Join Date: Oct 2008
Default 10-13-2008, 04:34 PM

Flames, your code wouldnt work because its mot the localhost. The hos tis the 000webhost host.

Try this (Similar to what youve got just less complicated.

PHP Code:
<?php
   
  $host 
=  'mysql3.000webhost.com'$user 'YOUR USER'$pass 'YOUR PASS'$db 'DATABASE NAME';

  
mysql_connect($host,$user,$pass)or die(mysql_error());
  
mysql_select_db($db)or die(mysql_error());

?>
Rather then fiddling with all this other rubbish and making variables for ecerything, just keep it plain and simple and do the query without making it into a variable etc.

I would also change your query to:

PHP Code:
<?php

  $selectnews 
mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");

                while (
$getnews mysql_fetch_assoc($selectnews)){

                        echo 
"<li><h3>".$getnews['date']."</h3>"
                        
echo "<p>".$getnews['message']."</p></li>";

                }

?>
I have changed the "fetch_array" to "fetch_assoc" which does pretty much the same thing; only has a minor different which you can check on php.net. As has already been said you may have a null value in there but that WOULDN't return the error youve been given. It would just echo nothing, in other words give a blank space.

That error is when you have something wrong in your code for example:

PHP Code:
<?php

  $query 
mysql_query("SELECT * FROM `news` WHERE `name` = '$name'");
  
$result mysql_fetch_array($yreuq);

 echo 
''.$result['news'];

?>
Notice how i spelt query backwards. That is when you would get that sort of error because it cant find anything with that variable. If i were you id check all your spelling etc as ive made that mistake before!

Last edited by CKPD : 10-13-2008 at 04:47 PM.
Reply With Quote
(#5 (permalink))
Old
Junior Member
DJDrew22200 is on a distinguished road
 
Posts: 5
Join Date: Oct 2008
Default 10-15-2008, 04:13 PM

Thanks for the tips guys, but nothing is working... Both of these codes are in the HEADER.PHP file:

PHP Code:
<?php
   
  $host 
=  'mysql3.000webhost.com'$user 'a3674547_user'$pass 'mypassword'$db 'a3674547_main';

  
mysql_connect($host,$user,$pass)or die(mysql_error());
  
mysql_select_db($db)or die(mysql_error());

?>
PHP Code:
<?php

$selectnews 
mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");

    while (
$row mysql_fetch_assoc($selectnews)){

                echo 
'<ul>
                    <li>
                                <h3>'
.$row["date"].'</h3>
                                   <p>'
.$row["message"].'</p>
                    </li>
                </ul>'
;

    }

?>
There IS a table called "news" and it has 4 rows in it... I'm still getting this error:

Quote:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a3674547/public_html/header.php on line 74
Line 74 is:

Quote:
while ($row = mysql_fetch_assoc($selectnews)){
Any other suggestions?

Thanks for all the tips so far guys!
Reply With Quote
(#6 (permalink))
Old
Junior Member
CKPD is on a distinguished road
 
Posts: 19
Join Date: Oct 2008
Default 10-15-2008, 04:50 PM

Check your private message!
Reply With Quote
(#7 (permalink))
Old
Junior Member
DJDrew22200 is on a distinguished road
 
Posts: 5
Join Date: Oct 2008
Default 10-15-2008, 04:53 PM

Hey, I figured it out... I was asking it to sort by `id` by that column doesn't exist lol...

I meant `newsid`
Reply With Quote
(#8 (permalink))
Old
Junior Member
CKPD is on a distinguished road
 
Posts: 19
Join Date: Oct 2008
Default 10-15-2008, 05:02 PM

Nice one dude
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0
vBulletin Skin developed by: vBStyles.com