-1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

This is what I have

<?php 
            mysql_select_db("my_db", $conn);

            $result = mysql_query("SELECT name FROM card");

            while($row = mysql_fetch_array($result))
            {
            echo $row['name'];
            echo "<br />";
            }
?>

and

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\UFSDBSearch.php on line 113

is what I'm getting. I have no idea what that means. I'm just trying to get it to echo out the names of the cards stored on that table. If I need to tell more info just let me know. Thanks for the help in advance.

Community
  • 1
  • 1
Werbel
  • 121
  • 1
  • 2
  • 14
  • 1
    Why don't you practice first? http://sqlfiddle.com/ – Jared Farrish Sep 16 '12 at 18:43
  • 1
    where is your [mysql_connect()](http://www.php.net/mysql_connect)? – rationalboss Sep 16 '12 at 18:45
  • 5
    Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [**red box**](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is a good PDO tutorial](http://goo.gl/vFWnC). – PeeHaa Sep 16 '12 at 18:45

3 Answers3

5

mysql_query() returned FALSE. This happens when some error prevents the query from completing successfully. Please review the code in which you connect to the DB, and make sure SELECT name FROM card is working on that DB.

To investigate further, do like this:

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

You will see the actual error returned by MySQL.

Moreover, like someone suggested, you may consider learning new (and better) DB interaction paradigms like PDO. mysql_* functions are legacy (deprecated).

gd1
  • 11,088
  • 7
  • 47
  • 85
2

PDO is so much better than mysql_.

<?php
$db = new PDO('mysql:host=localhost;dbname=my_db', 'username', 'password');
// The next line is only needed if you are using prepared statements,
// see the next block of code below
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$query = $db->query('SELECT name FROM card');
$rows = $query->fetchAll();
foreach($rows as $row) {
    echo $row['name'] . '<br />';
}
?>

EDIT: If you are using a dynamic query, you should use something like this:

<?php
$db = new PDO('mysql:host=localhost;dbname=my_db', 'username', 'password');
// Turn off prepare emulation, *this is good practice*
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$query = $db->prepare('SELECT name FROM card WHERE id = ?');
$query->execute(array($_GET['id']));
$rows = $query->fetchAll();
foreach($rows as $row) {
    echo $row['name'] . '<br />';
}
?>
uınbɐɥs
  • 6,988
  • 5
  • 25
  • 42
-1

Try the following and see if it helps you. It has more error reporting in it.

mysql_select_db("my_db", $conn);

$result = mysql_query("SELECT name FROM card") or die(mysql_error());

if (mysql_num_rows($result) > 0)
{
    while($row = mysql_fetch_array($result))
        {
        echo $row['name'];
        echo "<br />";
        }
}
else{
    echo 'There were no results found.';
}
James
  • 674
  • 2
  • 15
  • 37