Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE[42S22]: Column not found: 1054 Unknown column

Tags:

sql

php

mysql

pdo

I am attempting to insert a record to MySQL using PDO, my sql statement can be seen in the following code.

<?php
    try{
        //include file myfunctions.php allows us to calls functions from it
        include ("myfunctions.php");
        //Assign function getConnection() from myfunctions.php to variable $db
        $db = getConnection();


        foreach($_POST['chk'] as $check_value)
        {
            $check = $check_value;
            $fav = "channel/item [title = \"$check\"]";
            $holidayDoc = simplexml_load_file('holidays.xml');
            $favourites = $holidayDoc->xpath($fav);

        foreach($favourites as $currentFav)
        {
            echo "{$currentFav->link}". "<br \>";
            echo "{$currentFav->title}". "<br \>";
            echo "{$currentFav->description}". "<br \>";
            echo "{$currentFav->pubDate} ". "<br \>";

            $sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
            VALUES (`John`, `$currentFav->link`, `$currentFav->pubDate`, `$currentFav->title`, `$currentFav->description`)";

            $db->exec($sql);
            $db = null;
        }
    }
}
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
?>

When this code is executed i am met with the following error message;

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'John' in 'field list'

This is no doubt a simple solution to this problem but i cannot seem to see it, can anyone point me in the right direction?

like image 320
Darren Burgess Avatar asked Apr 12 '12 17:04

Darren Burgess


4 Answers

I believe this is because you are using backticks for your values. Change them to single quotes and you should be good

$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
            VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";

Please refer to this SO question about single quotes versus backticks if you want more information

like image 157
Justin Pihony Avatar answered Oct 25 '22 14:10

Justin Pihony


 $sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
            VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";

Use ` for fields only and use ' for values

like image 4
sujal Avatar answered Oct 25 '22 13:10

sujal


` is for specifying fields, you must use a single quote ' for values.

$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`) 
        VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";
like image 4
Pierre-Olivier Avatar answered Oct 25 '22 14:10

Pierre-Olivier


For anyone else struggling with this, this is why I have seen this error message and how I solved it:

  1. I was working on a shared code base (in work so many contributors)
  2. Someone else working on the code added a column without you knowing.
  3. I pulled the code, all was working yesterday but today I got the error message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'column_name' in 'field list' "

All you have to do is go to your table and add the Column (for me it was in phpmyadmin that I did this).

From the error message I received, I could deduce it was 'properties' table that needed the additional column pricing_formula. Just insert this column directly into the DB.

For reference, this was my error message:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pricing_formula' in 
'field list' (SQL: select `site`.`name`, `domain`, `site`.`uuid`, 
`pricing_formula`, `client`.`uuid` as `clientRef` from `site` inner join 
`client` on `site`.`client_id` = `client`.`id` where `client_id` = 1 and 
`site`.`deleted_at` is null)"

Hope this helps someone. If anyone still doesn't fully know how to fix this issue but thinks my explanation describes their problem, reach out - I will try to help you fix it.

Also for info, this was a Laravel backend project with an Angular front end.

like image 1
Lilly_Code Avatar answered Oct 25 '22 13:10

Lilly_Code