hey guys what is wrong with my mysql_fetch_array
here is my code :

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td>
<strong><h2>Welcome to Our Website</h2></strong>
</td>
</tr>
</table>
<br />
<?
$mysql_host = "localhost";
$mysql_database = "members";
$tbl_name = "posts";
mysql_connect('localhost','admin','*******')or die("can not connnect to database");
mysql_select_db("$mysql_database")or die("sorry , can not sellect db");
$sql="SELECT*FROM $tbl_name";
$result=mysql_query($sql);
while($rows = mysql_fetch_array("$result"))
{
?>
<table width="400" border="0" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr><td>
<table width="400" border="0" cellpadding="2" cellspacing="1" bgcolor="#ffffff">
<tr>
<td>Title : <? echo $rows['title']; ?></td>
</tr>
<tr>
<td> <?   echo $rows['post']; ?> </td>
</tr> 
</table></td></tr> <? } ?></table>
<? mysql_close(); ?>

and here is the error :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /www/zxq.net/a/w/a/awah/htdocs/add_email_test/admin/view-posts.php on line 19

:?:

Dani AI

Generated

Quick diagnosis: that warning means mysql_fetch_array got something that is not a MySQL result resource — it expects the resource returned by mysql_query(), not a string or boolean. In this thread the immediate cause was passing the wrong value (the variable was quoted, becoming a string), so removing the quotes fixes the symptom. Also be aware a malformed query or missing permissions will make mysql_query return false, which then produces the same warning when you try to fetch rows. (php.net)

Immediate troubleshooting (do this before any refactor): check the query result and print the database error so you know whether the query itself failed. For example:

$result = mysql_query($sql);
if (! $result) {
    die('Invalid query: ' . mysql_error());
}

That will show the real MySQL error (syntax, unknown table, permission, etc.). Always check the return of mysql_query() before calling fetch functions. (php.net)

Other quick notes tied to earlier replies: as and pointed out, don’t pass a quoted variable to the fetch function. ’s suggestion to fix the SELECT formatting is sensible — a syntactically wrong SQL string will make mysql_query() fail. Also avoid the short <? opening tag used in the original post: it can be disabled on many servers; prefer <?php for portability. (php.net)

Longer-term advice: the old ext/mysql functions are deprecated and removed in modern PHP releases — migrate to MySQLi or PDO and use prepared statements (safer and compatible with current PHP versions). Converting now avoids surprise breakage if you move to PHP 7+ or later. (php.net)

Recommended Answers

All 4 Replies

Remove the double quotes. It expects a variable containing a resource, not a string.

thanks

use

while($rows = mysql_fetch_array($result))
$sql="SELECT * FROM $tbl_name";

$result=mysql_query($sql);

while($rows = mysql_fetch_array($result))

replace with above code and try

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.