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,021
  • 16
  • 87
  • 172
  • 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
  • 144,362
  • 22
  • 272
  • 285
1

Wrong spelling...

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

should be

$query = "SELECT * FROM products WHERE id = '13' LIMIT 1";
ace
  • 6,917
  • 3
  • 21
  • 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
  • 348,685
  • 41
  • 398
  • 480
1

You have a typo, LIMT -> LIMIT

Ed Heal
  • 57,599
  • 16
  • 82
  • 120
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
  • 431,103
  • 135
  • 960
  • 1,075
0

Simple typo: LIMT should be LIMIT

JK.
  • 5,086
  • 1
  • 26
  • 26