简体   繁体   中英

PHP Mysql query not returning the results

I have a situation where my PHP query is not returning any results, but if I run the actual query on mysql DB it works fine, but not with PHP.

<html>
<body>
<?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 = "," ;
}
$conn = new mysqli('127.0.0.1', 'zzzzz', 'xxxx', 'xxxx');

// Oh no! A connect_errno exists so the connection attempt failed!
if ($conn->connect_errno) {
    // The connection failed. What do you want to do?
    // You could contact yourself (email?), log the error, show a nice page, etc.
    // You do not want to reveal sensitive information

    // Let's try this:
    echo "Sorry, this website is experiencing problems.";

    // Something you should not do on a public site, but this example will show you
    // anyways, is print out MySQL error related information -- you might log this
    echo "Error: Failed to make a MySQL connection, here is why: \n";
    echo "Errno: " . $conn->connect_errno . "\n";
    echo "Error: " . $conn->connect_error . "\n";

    // You might want to show them something nice, but we will simply exit
    exit;
}
$sql = "SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result
       FROM hdds WHERE serial IN (". $barcode_str .");";
if (!$result = $conn->query($sql)) {
    // Oh no! The query failed.
    echo "Sorry, the website is experiencing problems.";

    // Again, do not do this on a public site, but we'll show you how
    // to get the error information
    echo "Error: Our query failed to execute and here is why: \n";
    echo "Query: " . $sql . "\n";
    echo "Errno: " . $conn->errno . "\n";
    echo "Error: " . $conn->error . "\n";
    exit;
}

if ($result->num_rows === 0) {
    // Oh, no rows! Sometimes that's expected and okay, sometimes
    // Oh, no rows! Sometimes that's expected and okay, sometimes
    // it is not. You decide. In this case, maybe actor_id was too
    // large?
    echo "We could not find a match for ID $barcode_str,  sorry about that. Please try again.\n\n";
    echo "Query: " . $sql . "\n";
    echo "Errno: " . $conn->errno . "\n";
    echo "Error: " . $conn->error . "\n";

    exit;
}

while($row = $result->fetch_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>";


//                     foreach($row as $key=>$value) {
//                                echo '<td>',$value,'</td>';
//                        }
                        echo '</tr>';
}

$result->free();
$conn->close();
?>
</body>
</html>

PHP does not show anything, no errors, apache errors logs are clear there is no clue why it is not showing the results. I am going crazy what exactly I am doing wrong for this simple thing not to work.

From PHP i have echo the actual query. The resulting query is following.

SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result FROM hdds WHERE serial IN ('5MQ3DJPM ','5MQ3DJPM ','5MQ3DJPM ','5MQ3DJPM ','M7856RHY803191 ','A00 ','ID0DC3604760858200D6 ','');

Now If I run this query in mysql I get the results see below.

mysql> SELECT cust, manu, model, serial, capacity, firmware, method, date, stime, etime, wks, result FROM hdds WHERE serial IN ('5MQ3DJPM ','5MQ3DJPM ','5MQ3DJPM ','5MQ3DJPM ','M7856RHY803191 ','A00 ','ID0DC3604760858200D6 ','');
+---------+------------------------------------------+------------+----------+-----------------------------+----------+--------+------------+----------+----------+------+-----------+
| cust    | manu                                     | model      | serial   | capacity                    | firmware | method | date       | stime    | etime    | wks  | result    |
+---------+------------------------------------------+------------+----------+-----------------------------+----------+--------+------------+----------+----------+------+-----------+
| Default | Seagate Barracuda 7200.7 and 7200.7 Plus | ST340014AA | 5MQ3DJPM | 40000000000 bytes [40.0 GB] | 8.12     | zero   | 2017-01-06 | 01:26:50 | 01:27:40 | 113  | Succeeded |
| Default | Seagate Barracuda 7200.7 and 7200.7 Plus | ST340014AS | 5MQ3DJPM | 40000000000 bytes [40.0 GB] | 8.12     | zero   | 2017-01-06 | 00:53:19 | 01:22:07 | 147  | Succeeded |
| Default | Seagate Barracuda 7200.7 and 7200.7 Plus | ST340014AS | 5MQ3DJPM | 40000000000 bytes [40.0 GB] | 8.12     | zero   | 2017-01-06 | 00:53:18 | 01:22:07 | 49   | Succeeded |
| Default | Seagate Barracuda 7200.7 and 7200.7 Plus | ST340014AS | 5MQ3DJPM | 40000000000 bytes [40.0 GB] | 8.12     | zero   | 2017-01-06 | 01:14:35 | 01:14:35 | 34   | Succeeded |
| Default | Seagate Barracuda 7200.7 and 7200.7 Plus | ST340014AS | 5MQ3DJPM | 40000000000 bytes [40.0 GB] | 8.12     | zero   | 2017-01-06 | 00:53:18 | 01:07:39 | 49   | Succeeded |
+---------+------------------------------------------+------------+----------+-----------------------------+----------+--------+------------+----------+----------+------+-----------+
5 rows in set (0.00 sec)

mysql>

My question is what I am doing wrong for this not to work? Thanks for your help.

I have managed to fix it by removing the empty spaces from $barcode_str variable string and also remove last 3 characters.

,'' was causing the problem.

I have used following to fix it. Now the PHP returns the results correctly.

$str=preg_replace('/\s+/', '', $barcode_str);
$str1=substr($str, 0, -3);
echo "$str1 \n";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM