I have a mysql query that does not work in PHP but works fine in MySQL. Below is partial code for the HTML form that processes POST variables and processes it for mysql query.
See below.
<?php
$codes = $_POST['codes'];
// get as array like so
$barcodes = explode("\n", $codes);
// build up string of barcodes
$barcode_str = "";
$prefix = '';
foreach ($barcodes as $barcode){
$barcode_str .= $prefix . "'" . $barcode . "'";
$prefix = "," ;
}
$dbhost = 'localhost:3036';
$dbuser = 'xxxx';
$dbpass = 'xxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('disks');
$sql = "SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result
FROM hdds WHERE serial IN ( $barcode_str) ;";
echo $sql;
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row['manu'];
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
/** Lets remove 20000000000000 bytes values from capacity array */
preg_match('/\[(.*?)\]/', $row['capacity'], $matches);
if (isset($matches[1])) {
$row['capacity'] = $matches[1];
}
echo '<tr>';
echo
"<td> {$row['manu']} </td>".
"<td> {$row['model']} </td>".
"<td> {$row['serial']} </td>".
"<td> {$row['capacity']} </td>".
"<td> {$row['firmware']} </td>".
"<td> {$row['method']} </td>".
"<td> {$row['date']} </td>".
"<td> {$row['stime']} </td>".
"<td> {$row['etime']} </td>".
"<td> {$row['wks']} </td>".
"<td> {$row['result']} </td>".
"<td> <a href=\"certificate.php?Customer={$row['cust']}&serial={$row['serial']}&manu={$row['manu']}&capacity={$row['capacity']}&method={$row['method']} \">Print</a>";
/** foreach($row as $key=>$value) {
echo '<td>',$value,'</td>';
}*/
echo '</tr>';
}
mysql_close($conn);
?>
As you can see above I have echo the $sql query and I get following on the screen.
SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result FROM hdds WHERE serial IN ( '5MQ3DJPM ','5MQ3DJPM ','5MQ3DJPM ','') ;
If I run this query I am getting rows back.
But with PHP nothing is returned, there are no errors.
But if I replace the php query with static query then it works, but I want the query to work with the variable.