0

I am having an issue with the following php/sql. I ran the sql in the mysql terminal and it worked perfectly. I also tried my connect statement alerting a string and it succesfully alert the string when connected to the db. There is some error within this code that is returning null when it should be returning two integers. Any suggestions?

$xmax = mysql_fetch_row(mysql_query('SELECT MAX(x) AS xmax FROM headerfooter WHERE site = "Brighter_Vista" AND location = 0'));
$xmax = $xmax['xmax'];
$ymax = mysql_fetch_row(mysql_query('SELECT MAX(y) AS ymax FROM headerfooter WHERE site = "Brighter_Vista" AND location = 0'));
$ymax = $ymax['ymax'];
echo '<script>alert("'.$xmax.$ymax.'")</script>';
5
  • 2
    Please use PDO rather than mysql_ functions, they both have major security flaws and are old... like deprecated old. Commented Nov 21, 2012 at 2:01
  • Try using print_r($xmax) after the mysql_fetch_row call to see what, if anything, is returned from the query. Commented Nov 21, 2012 at 2:03
  • just wondering: are you sure mysql supports double quotes for strings encapsulation? Commented Nov 21, 2012 at 2:03
  • Jonathan is right. PDO is much more pleasant to use too. Commented Nov 21, 2012 at 2:04
  • Nothing is being returned in that print_r command. However, as I said, the query worked in the terminal and the db is definitely connected. Commented Nov 21, 2012 at 2:04

2 Answers 2

1

We can't see the rest of your code, but, you can refine this into one query, doing something like:

  $connection = mysql_connect(
        $DB_hostname, 
        $DB_username, 
        $DB_password) or die(mysql_error());
  mysql_select_db($DB_name, $connection);

  $query = mysql_query("
      SELECT MAX(x) AS xmax, MAX(y) AS ymax 
      FROM headerfooter 
      WHERE site = 'Brighter_Vista' AND location = '0'
  ", $connection) or die(mysql_error());

  while ($row = mysql_fetch_array($query))
  {
     $xmax = $row['xmax'];
     $ymax = $row['ymax'];
  }

  echo '<script>alert("'.$xmax.$ymax.'")</script>';

However, if you have the time, ability and wish to learn, you should (as noted by others) look at and using either mysqli or PDO. Wise advice and in time you'll understand why.

Sign up to request clarification or add additional context in comments.

Comments

1

The easist solution is to check if the sql statement is returning an error using:

mysql_query('SELECT MAX(y) AS ymax FROM headerfooter WHERE site = "Brighter_Vista" AND location = 0') or die('Error: '.mysql_error());

Side note: Don't use mysql_* functions anymore, that time has passed. Use either mysqli or PDO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.