-1

I have the following code:

<?php
    $host="database.com"; // Host name 
    $username="user"; // Mysql username 
    $password="password"; // Mysql password 
    $db_name="database"; // Database name 
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    $contact_id = mysql_real_escape_string($_GET['contact_id']);

    $sql="select ( if(month(now()) < 9, year(now()) - 1, year(now())) - year(dob) - if(month(dob)<9, 0, 1)  ) as ageOnLastSeptember from members_family where contact_id = '$contact_id' ";

    $result=mysql_query($sql);
    while($rows=mysql_fetch_array($result)){
        echo $rows['ageOnLastSeptember'];
    }
    mysql_close();
?>

For some reason, its not working and I am getting the following error message:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /record_view/index.php on line 4702

Eich
  • 3,630
  • 1
  • 20
  • 34
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Apr 15 '13 at 14:38
  • Echo the query to see what it's returning – user1048676 Apr 15 '13 at 14:38
  • did you try your query in a db tool? – juergen d Apr 15 '13 at 14:38
  • 2
    There are already millions of identical questions here...do we really need another duplicate question? :-( – Jocelyn Apr 15 '13 at 14:39
  • This is sorted - It was an incorrect SQL query. – user2281899 Apr 15 '13 at 14:40
  • @Connor you can't be fully sure contact_id is a numerical field, since we don't know the table schema. – Jocelyn Apr 15 '13 at 14:41

4 Answers4

2

It means your query failed. You would know this if you used error handlers...

$result=mysql_query($sql) or die(mysql_error());
Kermit
  • 33,206
  • 11
  • 83
  • 119
1

Looks like there is a problem with the SQL try running the SQL direct.

Also look at using mysqli / pdo the mysql functions will be depreciated soon

exussum
  • 17,675
  • 8
  • 30
  • 64
1

From another SO thread:

In case mysql_query fails, it returns false, a boolean value. When you pass this to mysql_fetch_array function (which expects a mysql result object) we get this error.

Community
  • 1
  • 1
Useless Intern
  • 1,254
  • 1
  • 10
  • 20
1

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. So the select is not correct. Try it separately in phpmyadmin or the place you have your bd and see if it works.

Miha
  • 11
  • 1