0
    <?php
include 'config.php';
$tid = $_GET['tid'];
$sql="SELECT FROM topics where tid='$tid'";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?> 
<div class="topics"><font size="4"><?php echo $rows['title']; ?></font></div><div class="tdm"><br/><center><img src="http://appricart.com/test/img/<?php echo $rows['photo']; ?>" height="100" width="100"/><br/>
<small><?php echo $rows['message']; ?></small></div>
<?php
}
include 'foot.php';
?>

Sometime this code works sometimes it does not please help me with this problem.

It shows this error

mysql_fetch_array() expects parameter 1 to be resource
Peter Foti
  • 5,526
  • 6
  • 34
  • 47
  • 1
    When there's an error with your query, you still call [mysql_fetch_array](http://php.net/manual/en/function.mysql-fetch-array.php) with `$result`, which is not a legal resource. Also you should notice that the mysql extension is deprecated. You should use a more modern one, like [Mysqli](http://www.php.net/manual/en/book.mysqli.php) or [PDO Mysql](http://www.php.net/manual/en/ref.pdo-mysql.php) – Itay Aug 18 '13 at 17:17
  • I assume your query is a copy/paste error, ie. `SELECT title, photo, message FROM ...` or `SELECT * FROM ...` instead of `SELECT FROM ...`. Also, you are open to SQL injection by using a `$_GET` value directly in your query without escaping it. – Sean Aug 18 '13 at 17:20

2 Answers2

1

You aren't doing any error checks to prevent this. This error is caused when your MySQL query fails to execute. You have a mysql_query() statement, and the results are stored into a variable. Later, you're using that variable in mysql_fetch_array() statement.

If mysql_query fails, it returns FALSE which is a boolean value. mysql_fetch_array expects it to be a resource instead.

To solve this issue and understand why your query is failing, use the mysql_error() function.

$result = mysql_query($sql);
if (!$result) { 
    die('Query failed because: ' . mysql_error());
}
else {
//proceed
}

In this case, you aren't selecting anything from your database, as Sean mentioned above.

Try the following:

$sql = "SELECT * FROM topics where tid='$tid'";

Or

$sql = "SELECT topics FROM topics where tid='" . $tid . "'";

Also, please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0
 <?php
include 'config.php';
$tid = $_GET['tid'];
$sql="SELECT FROM topics where tid='$tid'"; // here is the error
$result=mysql_query($sql);

You are not selecting anything from the table topic. hence its showing the error..

$sql="SELECT * FROM topics where tid='$tid'"; // this would be better

for more info visit mysql_fetch_array

Rahul
  • 1,181
  • 1
  • 11
  • 20