I believe I have this query formatted corrected, but it keeps giving me my error message I set up. Is there something I'm missing?

<?php
// Get what biz unit this document is
$imagenum = $HTTP_GET_VARS['key'];
// Get Long Name from the bizUnits Table
$query_bizUnits = "SELECT longBU FROM bizUnits WHERE key='$imagenum'";
$list_bizUnits = mysql_query($query_bizUnits)
	or die("Couldn't execute query Get Long BU Names");
$row_bizUnits = mysql_fetch_assoc($list_bizUnits);
$totalRows_bizUnits = mysql_num_rows($list_bizUnits);
?>

Thanks for the help!

    $list_bizUnits = mysql_query($query_bizUnits)
        or die(mysql_error()); 

      The Error Message I'm getting now is:
      You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'key='3'' at line 1

      at line 1 is just <html> which doesn't make any sense

      in the <title> tag I have the following

      <title><?php echo $row_bizUnits['longBU']; ?> Event Survey Results Page</title>

      That should give me the full name of the Business Unit based upon the query and the key that is being passed.

        1. The "at line 1" part is telling you what line of your query is at error, not what line of your script.

        2. Is key a numeric column? If so, you should give SQL a numeric value to work with (meaning no quotes around the value). If not... it looks like it holds numeric data, so why isn't it?

        3. Key is a reserved name in MySQL, so you need to enclose it with backticks ( ` ) so that MySQL knows you are referring to a column name. As a general rule, I just enclose all database/table/column names with backticks in my queries to avoid possible conflicts:

        SELECT `column1`, `column2` FROM `myTable` WHERE `aColumn` = 5;
        1. Since you're only retrieving 1 column from 1 row, why not just use [man]mysql_result/man to fetch that specific cell in the table, instead of using a function that returns an array of columns?

        2. Similarly, you could add 'COUNT(*) AS numRows' to your query to get the total number of rows found, though I'm not sure which one has better performance.

          THANKS Alot!!! The back ticks is what solved the problem.

          It should've looked like this:

          $query_bizUnits = "SELECT * FROM bizUnits WHERE `key`='$imagenum'";

          You learn something new everyday! Thanks for the help!

            Generally, it's best practice to not used reserved names for variables and columns, even if you can get around it.

            Just my 2 cents.

              I definitely agree with Kudose on this one.

              Also, another comment about your new query (even though you apparently ignored my previous ones...) - using "SELECT *" is highly deprecated as it often wastes resources. Instead, you should be selecting the column(s) that you know you are going to use. Even if you use all of the columns in a table, it's best to list them for scalability reasons - what if you decide to add a new column or two later on?

                Write a Reply...