簡體   English   中英

PHP Mysql查詢未返回結果

[英]PHP Mysql query not returning the results

我遇到的情況是我的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>

我的問題是我做錯了什么才能使它不起作用? 謝謝你的幫助。

我設法通過刪除$ barcode_str變量字符串中的空白並也刪除了最后3個字符來解決此問題。

''導致了問題。

我已經使用以下修復它。 現在,PHP可以正確返回結果。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM