Quantcast
Viewing latest article 4
Browse Latest Browse All 34

What is the error in my code that I get mysql_fetch_array wa

I am trying to display some data from mySQL, the db details are correct but I get this.
*Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource*
What is the error with my code below?

$con = mysql_connect(“localhost”,” “,” “);
mysql_select_db(” “, $con);
mysql_query(‘set names utf8′);

$query = “SELECT * FROM products WHERE id = ’13′ LIMIT 1″;
$row = mysql_fetch_array(mysql_query($query));
echo $row['name'];

?>UPDATE
I found my error it was a typo. This is what you get if you still use notepad…
……………………..

$query will be FALSE because the query you have entered is not valid; the return from mysql_query is FALSE when there was some error compiling or executing the query.
Change LIMT to LIMIT and try again.
……………………..

Wrong spelling…
$query = “SELECT * FROM products WHERE id = ’13′ LIMT 1″;should be
$query = “SELECT * FROM products WHERE id = ’13′ LIMIT 1″;
……………………..

You’ve got an error somewhere. Try:
$con = mysql_connect(…) or die(mysql_error());

mysql_select_db(…) or die(mysql_error());

$res = mysql_query(…) or die(mysql_error());
$row = mysql_fetch_array($res);
……………………..

You have a typo, LIMT -> LIMIT
……………………..

Use error handling to get information about errors. The message will tell you that you have a syntax error (LIMT instead of LIMIT).
A minimal example:
$query = “SELECT * FROM products WHERE id = ’13′ LIMT 1″;

if (!$query) trigger_error(“mySQL Error: “.mysql_error(), E_USER_ERROR);The use of trigger_error() instead of die() is so you can avoid the message getting shown on a live site. Other than that, die() is also fine.
See also Reference: What is a perfect code sample using the mysql extension?


Viewing latest article 4
Browse Latest Browse All 34

Trending Articles