PHP Mysql查询没有返回结果

时间:2017-01-08 15:13:44

标签: php mysql

我有一种情况,我的PHP查询没有返回任何结果,但如果我在mysql DB上运行实际查询它工作正常,但不是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没有显示任何内容,没有错误,apache错误日志很明显,没有线索为什么它没有显示结果。我疯了,因为这个简单的事情不起作用我到底做错了什么。

从PHP我回复了实际的查询。 结果查询如下。

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 ','');

现在如果我在mysql中运行此查询,我得到的结果见下文。

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>

我的问题是我做错了什么不行? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我设法通过从$ barcode_str变量字符串中删除空格来修复它,并删除最后3个字符。

,&#39;&#39;导致了这个问题。

我用过以下来修复它。现在PHP正确返回结果。

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