Page 1 of 1

mysql_fetch_array() expects parameter 1 to be resource

Posted: Wed Apr 13, 2011 9:48 am
by chand.sethi77

Code: Select all

<?php
$connect = mysql_connect("localhost", "root", "");
    if (!$connect) {die ("Error." . mysql_error() ); }
$db_select = mysql_select_db("grabitup", $connect);
    if (!$db_select) {die ("Error." . mysql_error() ); }
if (isset($_GET['cat'])) {
    $cat = $_GET['cat'];
    } else {
    $cat = "<a href='walkth.php?refid=walk'>Click here to go to the walkthrough";
    }
$query = mysql_query("SELECT * FROM posts WHERE category = $cat");
$result = mysql_fetch_array($query);
?>
<html>
    <head>
        <title> Grab it up </title>
        <style type="text/css">
            body {height:100%; width:100%; background: #CCCC00; }
            #page {height: 900px; width: 75%; position: absolute; left: 15%; padding: 1em; background: white; }
                #page h1, h2, a:link {color: #99CC00; font-family: tahoma;}
                #page section {font-family: tahoma; line-height:28px;font-size: 15px; line-height: 19px; padding: 1em; color : black; border:1px solid black; width: 55%;}
                .img1 {border: 0;padding: 2em; height: 20%; width: 35%;}
            #rightbar {position:absolute; left: 65%; top: 13%;  height:600px; width:30%; border: 1px solid black; padding:1em;}
                #rightbar h1, h2, a:link{color: #99CC00; font-family: tahoma; text-decoration: none;}
                #page a:hover{color: #99CC00; font-family: tahoma; text-decoration: underline;}
            .text {border: 1px solid #99CC00; font-family: tahoma; color: #99CC00; font-size:20px; line-height:24px; }
            table {border: 1px solid #99CC00; width: 300px; }
        </style>
    </head>
    <body>
        <div id="page">
            <h1> Walkthrough </h1>
            <h2> <?php echo $cat; ?> </h2>
            <?php
            while ($result = mysql_fetch_array($query)) {
                echo "<table>";
                echo "<tr><td> {$result} </td></tr>";
                echo "</table>";
            }
            ?>
                
            
        </div>
    
    </body>
</html>
That's my code.
mysql_fetch_array() expects parameter 1 to be resource, boolean given in line "$result = mysql_fetch_array($query); " and also in line "while ($result = mysql_fetch_array($query)) "
When I remove WHERE category = $cat all goes well except the result. It says "array array array array array" and so on
Thanks

Re: mysql_fetch_array() expects parameter 1 to be resource

Posted: Wed Apr 13, 2011 10:39 am
by waytoecommerce
hi,

You are doing wrong if it asking about category you are assigning full string how it will compare.

Wrong Method :
if (isset($_GET['cat'])) {
$cat = $_GET['cat'];
} else {
$cat = "<a href='walkth.php?refid=walk'>Click here to go to the walkthrough";
}
$query = mysql_query("SELECT * FROM posts WHERE category = $cat");

Correct Method :
if (isset($_GET['cat'])) {
$cat = $_GET['cat'];
} else {
$cat = "<a href='walkth.php?refid=walk'>Click here to go to the walkthrough"; //// Please remove this line it is creating problem.. when it does not find $_GET['cat']
}
$query = mysql_query("SELECT * FROM posts WHERE category = $_GET['cat']");

hope it will help you...

thanks!

Re: mysql_fetch_array() expects parameter 1 to be resource

Posted: Wed Apr 13, 2011 12:00 pm
by danwguy
He doesn't need to get rid of that line, he just needs to move the query to a different place...

Code: Select all

$connect = mysql_connect("localhost", "root", "");
    if (!$connect) {die ("Error." . mysql_error() ); }
$db_select = mysql_select_db("grabitup", $connect);
    if (!$db_select) {die ("Error." . mysql_error() ); }
if (isset($_GET['cat'])) {
    $cat = $_GET['cat'];
    $query = mysql_query("SELECT * FROM posts WHERE category = '$cat'");

    } else {
    $cat = "<a href='walkth.php?refid=walk'>Click here to go to the walkthrough";
    }
// get rid of this for sure though     $result = mysql_fetch_array($query);
?>
<html>
    <head>
        <title> Grab it up </title>
        <style type="text/css">
            body {height:100%; width:100%; background: #CCCC00; }
            #page {height: 900px; width: 75%; position: absolute; left: 15%; padding: 1em; background: white; }
                #page h1, h2, a:link {color: #99CC00; font-family: tahoma;}
                #page section {font-family: tahoma; line-height:28px;font-size: 15px; line-height: 19px; padding: 1em; color : black; border:1px solid black; width: 55%;}
                .img1 {border: 0;padding: 2em; height: 20%; width: 35%;}
            #rightbar {position:absolute; left: 65%; top: 13%;  height:600px; width:30%; border: 1px solid black; padding:1em;}
                #rightbar h1, h2, a:link{color: #99CC00; font-family: tahoma; text-decoration: none;}
                #page a:hover{color: #99CC00; font-family: tahoma; text-decoration: underline;}
            .text {border: 1px solid #99CC00; font-family: tahoma; color: #99CC00; font-size:20px; line-height:24px; }
            table {border: 1px solid #99CC00; width: 300px; }
        </style>
    </head>
    <body>
        <div id="page">
            <h1> Walkthrough </h1>
            <h2> <?php echo $cat; ?> </h2>
            <?php
           if($query) {
            while ($result = mysql_fetch_array($query)) {
                echo "<table>";
                echo "<tr><td> {$result} </td></tr>";
                echo "</table>";
            }
            }
            ?>
                
            
        </div>
    
    </body>
</html>

You will still have a problem with the while loop if $_GET['cat'] isn't set because it won't know what that is. I would add an if statement to it to make sure that you won't get that error if $_GET['cat'] isn't set, I've added it above, this script should do what you need it to.