-1

Anyway to get around using "&" in a query? When I try this query, an undefined variable error is returned.

"SELECT DISTINCT model FROM carval_data WHERE make = 'A&W' ORDER BY CAST(model AS CHAR)"

EDIT: Just figured out it's not an issue with MySQL, but rather PHP. Any solutions?

Second EDIT: This is what's returned

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/var/www/html/carval.php</b> on line <b>33</b><br /><br />

<b>Notice</b>:  Undefined variable: query in <b>/var/www/html/carval.php</b> on line <b>41</b><br /><br />

<b>Warning</b>:  mysqli_query(): Empty query in <b>/var/www/html/carval.php</b> on line <b>41</b><br /><br />

<b>Warning</b>:  mysqli_error() expects exactly 1 parameter, 0 given in <b>/var/www/html/carval.php</b> on line <b>44</b><br />

Here's the lines 32-50

foreach($body as $key => $jsons) { 
    foreach($jsons as $key => $value) {
        if($key == 'query'){
            $query = $value;
        }
    }
}

mysqli_set_charset($link, 'utf8');
$result = mysqli_query($link, $query);

if($result === FALSE){
    die(mysqli_error()); 
}

if (!$query) {
    echo 'MySQL Error: ' . mysqli_error();
    exit;
}
JerryCrowley
  • 145
  • 1
  • 12
  • CHAR(38) use the CONCAT() function for more predictable results – Sql Surfer Aug 14 '15 at 03:26
  • 1
    Your query is perfectly valid for MySQL 5.6. See example here: http://sqlfiddle.com/#!9/a649a/2. Perhaps your PHP or similar script has a variable that is undefined. – zedfoxus Aug 14 '15 at 03:28
  • Yes, just figured that out. I guess it's an issue with my php. Any way to fix that? – JerryCrowley Aug 14 '15 at 03:44
  • What **exactly** is the error message and what does the code look like around the line indicated? – Phil Aug 14 '15 at 03:52
  • ..and the code from `carval.php`, lines 33 - 44? Do you think we can guess just from error messages alone? – Phil Aug 14 '15 at 04:15
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Phil Aug 14 '15 at 04:16
  • Let us know the php code. – Happy Coding Aug 14 '15 at 04:24
  • It works for every other variable I use though. It only fails with "A&W." Is there no escape character for this? – JerryCrowley Aug 14 '15 at 04:30

1 Answers1

1

CHAR(38) use the CONCAT() function for more predictable results. also test your answer in a query window and then in the .Net C# (or whatever) library. You may find your problem is not at the raw SQL level but the client library.

Sql Surfer
  • 1,344
  • 1
  • 10
  • 25