1

I have a database with three tables, user, update and error.

I have set up a user and all that jazz. In my PHP i have connected to the database and the connection works just fine. However, when i try to access the update table i get this error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /members/pages/updates.php on line 13

When I try to access the database like this:

include ("../includes/connect_to_db_member.php");

$getUpdates = mysql_query(" SELECT * FROM update ");

if(!$getUpdates)
    echo "No Updates! <br />";


while($row = mysql_fetch_array($getUpdates))
{
    //echo $row['date'] . " " . $row['title'] . "<br />";
    //echo $row['content'];
}

I use the same code but change the table name from update to error or user and it works just fine. I don't understand the error that is being given to me so I was wondering if anyone knowns whats going on or had an error like this before?

Himanshu Jansari
  • 30,115
  • 28
  • 106
  • 129
ragebunny
  • 1,524
  • 10
  • 33
  • 53
  • 2
    the `update` is a mysql reserved word, enclose it in `backticks`. Look at [this](http://stackoverflow.com/questions/8701062/expects-parameter-1-to-be-resource-man-boolean-given/8701110#8701110) you have similar problem – Shakti Singh Jan 05 '12 at 12:36
  • That is it! Thank you so much! Oh and if you put that as an answer i'll give it a tick. – ragebunny Jan 05 '12 at 12:40

3 Answers3

2

UPDATE is reserved key in MySQL, rename your table.

devdRew
  • 4,285
  • 3
  • 22
  • 32
2

update is a mysql reserved word so that you have to enclose with backticks like this

$getUpdates = mysql_query(" SELECT * FROM `update` ");

As a side note you should consider to switching from mysql_ to PDO

Shakti Singh
  • 81,083
  • 20
  • 131
  • 150
2

The keyword update has already been mentioned. Either rename the table or use backticks to mark the identifier as such.

  • mysql_query returning a falsy value doesn't mean the result set is empty. It's indicating an error, see mysql_error
  • connect_to_db_member.php should somehow make the mysql resource returned by mysql_connect() accessible in order to pass it to the other mysql_ functions, like e.g. mysql_query('SELECT ...', $mysql)
  • your script can't "live" without the database connection, i.e. connect_to...php isn't optional. Therefore I'd use require instead of include
  • Do you have some error handling for a failed database connection?

e.g.

<?php
require '../includes/connect_to_db_member.php';
$getUpdates = mysql_query(" SELECT * FROM `update` ");

if(!$getUpdates) {
    echo "an error occured ";
    echo mysql_error();
    die();
}


$row = mysql_fetch_array($getUpdates);
if ( !$row ) {
    echo 'no updates';
}
else {
    do
    {
    //echo $row['date'] . " " . $row['title'] . "<br />";
    //echo $row['content'];
    } while( false!==($row = mysql_fetch_array($getUpdates)) ); 
}
VolkerK
  • 93,904
  • 19
  • 160
  • 225