0

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?

<?php

$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...

5
  • 4
    Sheesh. Downvoting spree... Would the anonymous coward who hates all the correct answers please step forward and explain themselves? Commented Aug 15, 2011 at 15:37
  • Don't need 50 me too answers saying the exact same thing Commented Aug 15, 2011 at 15:40
  • 3
    @Greg: Note the timestamps on the answers. They were all done at pretty much the same time. You don't get notified by SO immediately when an answer is posted. Commented Aug 15, 2011 at 15:49
  • ... and no matter what, it's not good form to downvote correct answers @Greg. If you see a pile of "me too" answers, just leave them alone (or leave an acidic comment) Commented Aug 15, 2011 at 16:06
  • What does acidic mean in this context? Commented Aug 15, 2011 at 17:07

6 Answers 6

1

$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.

Sign up to request clarification or add additional context in comments.

Comments

1

Wrong spelling...

$query = "SELECT * FROM products WHERE id = '13' LIMT 1";

should be

$query = "SELECT * FROM products WHERE id = '13' LIMIT 1";

Comments

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);

Comments

1

You have a typo, LIMT -> LIMIT

Comments

1

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?

1 Comment

Thank you for the reference, it will help me as a beginner to write better code.
0

Simple typo: LIMT should be LIMIT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.