0

I am trying to run this query:

$query="SELECT max(templateid) FROM vtiger_emakertemplates";
$sql_result = mysql_query($query);

the error it shows is:

Warning : mysql_fetch_array() expects parameter 1 to be resource, boolean given

It was supposed to give me an integer value but it is returning a boolean value. The column templateid contains only integer values. I can't understand where the boolean value is coming from. Any ideas?

Here is the full code

    $conncity=mysql_connect($dbconfig['db_hostname'],$dbconfig['db_username'],$dbconfig['db_password']);
    mysql_select_db($dbconfig['db_name'],$conncity);

    $query="SELECT max(templateid) FROM vtiger_emakertemplates";
    $sql_result = mysql_query($query);
    $id=mysql_fetch_array($sql_result);
laylarenee
  • 3,226
  • 7
  • 31
  • 40
Nagri
  • 2,759
  • 4
  • 29
  • 58

5 Answers5

4

Replace

$sql_result = mysql_query($query);

with

$sql_result = mysql_query($query) or die(mysql_error());

And it will tell you where you made a mistake in your query.

Artem Kalinchuk
  • 6,232
  • 5
  • 41
  • 55
1

This is probably due to $sql_query returning false. you should check $sql_result value before applying mysql_fetch_array on it.

dweeves
  • 5,415
  • 21
  • 28
1

Assuming that you are doing:

mysql_fetch_array($sql_result)

The error means that $sql_result is a boolean, probably false. This means that mysql_query($query)returned false, so your query failed.

Check for errors with mysql_error()to see what error was given.

Tchoupi
  • 14,384
  • 5
  • 36
  • 69
0

Change your query like this.

$query = "SELECT MAX(templateid) as MaxID  FROM vtiger_emakertemplates";

And fetch value like this.

$sql_result = mysql_query($query);
$row = mysql_fetch_array($sql_result);
echo $row['MaxID'];
Bhoopesh Pathak
  • 159
  • 1
  • 10
0

You should verify you received a good result before you fetch the array.

$query="SELECT max(templateid) as MaxID FROM vtiger_emakertemplates";
$sql_result = mysql_query($query);
if ($sql_result) {
  $id=mysql_fetch_array($sql_result);
  $maxID = $id[MaxID];
} else $maxID = false;
if (!$maxID) {
   print "There are no templateid's in vtiger_emakertemplates.\n";
}

Also, when I am debugging PHP/MySQL applications, I find it helpful to test my queries directly in the MySQL database using phpMyAdmin or other application as it provides immediate query syntax errors, and allows me to quickly make changes to find the correct query syntax.