I'm having trouble passing a variable($num) to my working MYsql db. The commented out sql$ works with the explicit 203-263-0011 passed directly. The working sql$ string was generated thru the mysqladmin tool. Thank You for any advice anyone may have.

~Steve

Here is the code:

<?php

$name = $_REQUEST['Name'] ;
$address = $_REQUEST['Address'] ;

$num = "203-263-0011";
$num = addslashes($num);
$username = "user";
$password = "pass";
$host="host";
$database="datatrac_test";
$conn = mysql_connect($host,$username,$password);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($database);
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
mysql_select_db($database, $conn);

           Working:
/* $sql = 'UPDATE `cust` SET `telephone1` = \' "203-264-0013"\' WHERE CONVERT(`lookupname` USING utf8) = \'"Carmen Anthony Fishhouse"\' ;'; */

       Not working:
 $sql = 'UPDATE `cust` SET `telephone1` = \'$num\' WHERE CONVERT(`lookupname` USING utf8) = \'"Carmen Anthony Fishhouse"\' ;';  

$result = mysql_query($sql);

    what happens if you get rid of the slashes before and after $num in the non-working query?

      With the slashes removed, I get a parse error.. I also tried the sideways quotes and got the variable itself num$ entered into the database

        You can't echo a variable from within a single quoted string. If you were to echo the query string you'd see that $num would be in the string as a literal '$num', rather than its value being present. Try it this way:

        $sql = "UPDATE `cust` SET `telephone1` = '$num' WHERE CONVERT(`lookupname` USING utf8) = 'Carmen Anthony Fishhouse'"; 
        

          NO, using 'num$' enters num$ into the field.

          I removed the line:

          $num = addslashes($num)

          as this did not seem to help. The telephone1 field is a character field, by the way. Entering a value (ex: 203-263-0011) works!

          Thanks for any answers!

          ~Hobiecat

            You didn't even bother to try the query string I posted above, did you? Since the whole string is enclosed in double quotes, the $num variable will be interpolated irrespective of the single quotes around it. Copy and paste it in as it is and I'll bet it works just fine.

              Hi All,

              I solved the problem:

              mysql_query("UPDATE cust SET lname='".$num."' WHERE lookupname = 'Taff'");

              Apparently '". is needed! Thanks for all the efforts.

                hobiecat;10968924 wrote:

                Apparently '". is needed! Thanks for all the efforts.

                It was needed in your earlier code since you used single quotes, in which variable interpolation doesn't occur. Using double quotes, however, means that variable interpolation within the string will occur and thus there's no need for concatenation.

                Then again, Pikachu2000 already noted all of this in his suggested solution back in post #4 of this thread. 🙂

                  Write a Reply...