-5

I don't know what's wrong, I get:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in.

There's something wrong with my fetch_array. its in

line 34 while($row = mysql_fetch_array($sql))

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("blog1");

    ?>

    <!DOCTYPE html>
    <html>
    <head>

        <title>BLOGGEN</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta name="description" content="" />


        <link rel="stylesheet" type="text/css" href="css/kickstart.css" media="all" />
        <link rel="stylesheet" type="text/css" href="style.css" media="all" /> 


        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="js/kickstart.js"></script>
    </head>
    <body>

            <ul class="menu">
            <li class="current"><a href="index.php">Hem</a>
            </li>
            <li><a href="blogg.php">Din Blogg</a>
            </ul>

            <?php
            $sql = mysql_query("SELECT * FROM blogdata ORDER BY id DESC");
            while($row = mysql_fetch_array($sql)){
            $title = $row["title"];
            $content = $row["content"];
            $category = $row["category"];

            ?>

            <table border = "1">
            <tr><td><?php echo $title; ?></td><td><?php echo $category; ?></td></tr>
            <tr><td colspan="2"><?php echo $content; ?></td></tr>
        </table>
            <?php
            }
            ?>      


        </div>

    </div> 
    </body>
    </html>
Taha Paksu
  • 14,871
  • 1
  • 44
  • 74

4 Answers4

4

Your query ($sql) is not producing a query resource, instead producing FALSE.

To Debug, try this:

 <?php
        $sql = mysql_query("SELECT * FROM blogdata ORDER BY id DESC") or die(mysql_error());
        if(mysql_num_rows($sql)>0)
        {
            while($row = mysql_fetch_array($sql)){
            $title = $row["title"];
            $content = $row["content"];
            $category = $row["category"];

            ?>

            <table border = "1">
            <tr><td><?php echo $title; ?></td><td><?php echo $category; ?></td></tr>
            <tr><td colspan="2"><?php echo $content; ?></td></tr>
        </table>
            <?php
            }
        }
        ?>      

The mysql_error() error message will guide you to the solution.

NOTE:

mysql_ functions are deprecated, use PDO or mysqli instead.

Jenson M John
  • 5,187
  • 4
  • 26
  • 46
2

Your call to mysql_query returns false instead of a resource. Check your logs for SQL errors, or make a call to mysql_error.

You should check if your query went ok, before trying to get the results out:

if (!$sql) {
    echo "something went wrong in the query: ".mysql_error();
} else {
    // process data
}

All that being said, stop using mysql_*, it is deprecated. Use MySQLi or PDO instead and us eprepared statements.

Bart Friederichs
  • 32,037
  • 14
  • 96
  • 185
1

This error means that you have a mysql error, this error does not trigger when the result is empty.

Your query is failing and therefore not producing a query resource, but instead producing FALSE.

check your sql syntax , or try echoing the mysql_error()

Alireza Fallah
  • 4,581
  • 3
  • 29
  • 57
1

The errors might occur at the following two lines!

mysql_connect("localhost", "root", "");
mysql_select_db("blog1");

So, change those to!

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("blog1") or die(mysql_error());

and you will see what's wrong

or the following line might have error

$sql = mysql_query("SELECT * FROM blogdata ORDER BY id DESC");

So, add a line to echo out the error

if( !$sql ) die(mysql_error());

When you see the error message then fix it. And finally, mysql_* functions are deprecated!

Tun Zarni Kyaw
  • 2,099
  • 2
  • 21
  • 25