$a = "bob";
mysql_query( 'SELECT Name, Email, Company FROM cust WHERE Name=$a');

Can anyone offer a suggestion or resource to better understand how to slip a php variable inside of an SQL query? No matter what I do, I get an error whenever I try to push a variable into the query.

😕

    You need to single quote non-numeric values in your SQL. My usual method is to use double quotes for the string, single quotes around SQL values, and break out of the string for variables. Like so:

    $a   = "bob";
    $sql = "SELECT Name, 
                   Email, 
                   Company 
              FROM cust 
             WHERE Name='".$a."'";
    $result = mysql_query($sql);

      Works perfect.

      Printed your reply in color and taped it to the wall. So simple, yet so seemingly elusive.

      Many thanks. Your usual method is now my standard.

        Write a Reply...