PHP, mysql SQLSTATE[42000]: Syntax error or access violation: 1064 statement works with an id but not with a text field, i.e. ‘name’ [duplicate]

  mysql, php, syntax

In the below function If I uncomment out the coach_id line and comment out the coachname line, the prepare statement works. But as it exist currently with coachname = $coachname it does not work. I get the sql error in title.
Any advice as to why?

Please excuse all the echo’s but trying to figure out why the sql statement will not pass the prepare statement as seen below.
Also hard coded coach_id = 23 in order to make sure would work.
BTW: If copy to sql and execute (replacing $coachname with a valid name) works.

function validateCoach($coach_name, &$coach_id,
    &$ok, &$coachname, &$phone, &$email, &$school_id,
    &$date_entered, $pdo) {
    echo "cgdFunctions validateCoach $coach_name $school_id<br>";
    if (!isset($pdo)) {
        echo "What Now??";
    } else {
        $coach_id = 23;
        echo "Before sql execution<br>";
        $sql = "select * from coaches";
        $sql .= " where coachname = $coach_name";
        // $sql .= " where coach_id = $coach_id";
        // $sql .= " and school_id = $school_id";
        try {
            echo "In Try(1)<br>";
            $coaches = $pdo->prepare($sql);

Source: Ask PHP

LEAVE A COMMENT