Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2014

    Unanswered: Assistance with MySQL Error "Unknown column 'color' in 'where clause' "

    If someone could assist me with this It would be greatly appreciated. I've been working on this all day and still not able to make it work. The rest of the code appears to work fine.

    I have 2 php files the first (selection_list.php) I have included in the second file (file.php)

    color is the value in the table that corresponds the array value $row[0] which is column1. I have set this as the primary key as it will not appear on any other rows in a table of 3 columns $selection is the variable assigned to ($_POST['selection']) ( $row[1] in the drop down menu) and it's in column2


    PHP Code:

    Working Code

    $selection mysql_real_escape_string($_POST['selection']);
      while (
    $row mysql_fetch_array($result)) {
    if ($row[1] == $selection) {
    // Applies the second value of the $row array to the drop down list and assigns the first value of the array as its value.
    echo '<option value=" ' $row[0] . ' " >' $row[1] . '</option>';
          } else {
    '<option value=" ' $row[0] . ' " selected="selected">' $row[1] . '</option>';
    // Ends first while loop. 

    PHP Code:
    include "selection_list.php";


     Working php code


    // this is where the error occurs

    $query "SELECT * FROM table1 WHERE column2 = $selection";
    $result mysql_query($query$link);
                   if (!
    $resulttrigger_error("An unexpected event has caused an error" " " mysql_error($link));
    $row1 mysql_fetch_array($result);

  2. #2
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    which query is failing
    what is the value of the sql
    have you debugged the where clause to make certain its valid either ECHO the SQL
    or use an or die construct
    which one of the the two code samples, both of which claim to be working code shoudl we start looking for the error
    what is the vlaue of query
    do you really have a column called column2 in your database, really?

    ...always, always examine the actual value of the SQL that is causing the error. just because it compiles as PHP doesn't mean its valid SQL

    As a final comment if its a PHP problem then it shgould be posted in the PHP section, if its a database problem then select the right database section. at first glance Im expectign this to be a php problem
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Sep 2009
    San Sebastian, Spain
    Hi, you need to look more closely at the order in which things are getting called within your PHP scripts. When running code in PHP to access data you need to first of all connect to the database which I am assuming has happened in another PHP script. Then you need to do the following:

    1. Generate your SQL query
    2. Query/Execute the query
    3. Read the rows one by one in a while loop.

    File1.php is calling selection_list.php but you have not setup your query. The include syntax in PHP is basically copying your script into the location. Your result is something like

    Working Code 
    $selection = mysql_real_escape_string($_POST['selection']); 
      while ($row = mysql_fetch_array($result)) { 
          if ($row[1] == $selection) { 
              // Applies the second value of the $row array to the drop down list and assigns the first value of the array as its value. 
              echo '<option value=" ' . $row[0] . ' " >' . $row[1] . '</option>'; 
          } else { 
              echo '<option value=" ' . $row[0] . ' " selected="selected">' . $row[1] . '</option>'; 
      } // Ends first while loop.  
     Working php code 
    // this is where the error occurs 
    $query = "SELECT * FROM table1 WHERE column2 = $selection"; 
                  $result = mysql_query($query, $link); 
                   if (!$result) trigger_error("An unexpected event has caused an error" . " " . mysql_error($link)); 
                  $row1 = mysql_fetch_array($result); 
    I think you need to add the include at the end of file1.php, not at the start, and then this will work.

    Also be aware that as of PHP5.6 mysql_connect, mysql_query is deprecated. I would strongly advise you to use either mysqli or PDO. I have examples of both
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    Follow me on Twitter

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts