[英]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.