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

EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • 4
    Sheesh. Downvoting spree... Would the anonymous coward who hates all the correct answers please step forward and explain themselves? – Marc B Aug 15 '11 at 15:37
  • Don't need 50 me too answers saying the exact same thing – GBa Aug 15 '11 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. – Marc B Aug 15 '11 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) – Pekka Aug 15 '11 at 16:06
  • What does acidic mean in this context? – GBa Aug 15 '11 at 17:07

6 Answers6

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.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
1

Wrong spelling...

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

should be

$query = "SELECT * FROM products WHERE id = '13' LIMIT 1";
ace
  • 7,293
  • 3
  • 23
  • 28
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);
Marc B
  • 356,200
  • 43
  • 426
  • 500
1

You have a typo, LIMT -> LIMIT

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
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?

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

Simple typo: LIMT should be LIMIT

JK.
  • 5,126
  • 1
  • 27
  • 26