我遇到一个问题,无法在PHP程序中正常运行数据库查询。如果对查询进行了硬编码,则该查询在程序中可以正常工作,但否则将失败,并且不传递任何结果。我已经回显了两个结果,并且给出了不同的字符串长度,但是给出的字符串是相同的(通过var_dump获得的字符串)。我机智的尽头;我不太确定查询是什么问题。
我尝试了几种不同的修复程序,这些修复程序针对类似的问题发现了,但是没有一个起作用。我修剪了发布的输入,并且还对变量加了双引号,而不是单引号,以便执行引用。我真的不知道怎么了。
以下是与此项目相关的代码:
AJAX调用php类:
chlorinator = ($('#chlorinator').val()).concat(' GS').trim();
$.ajax(
{
type: "POST",
url: "gravity.php",
data: "chlorinator="+chlorinator,
cache: false,
beforeSend: function () {
$('#results').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html)
{
$("#results").html( html );
}});
这是相关的php代码:
<?php
include 'connection.php';
$chlorinator = trim( mysqli_real_escape_string ($dbhandle,$_POST["chlorinator"]));
$query = 'SELECT chlorinators.model_name, equipment.name, equipment.cutsheet_url, chlorinators.pump_specific, equipment.file_name
FROM chlorinators
INNER JOIN chlorinator_equipment
ON chlorinators.chlorinator_index = chlorinator_equipment.chlorinator_index
INNER JOIN equipment
ON chlorinator_equipment.equipment_index = equipment.equipment_index
WHERE chlorinators.model_name= "' . $chlorinator . '"';
echo "The value of the combined string is:<br> ";
var_dump($query);
echo '<br><br>';
echo "The value of the hard-coded string is:<br> ";
$query = 'SELECT chlorinators.model_name, equipment.name, equipment.cutsheet_url, chlorinators.pump_specific, equipment.file_name
FROM chlorinators
INNER JOIN chlorinator_equipment
ON chlorinators.chlorinator_index = chlorinator_equipment.chlorinator_index
INNER JOIN equipment
ON chlorinator_equipment.equipment_index = equipment.equipment_index
WHERE chlorinators.model_name= "2075 GS"';
var_dump($query);
echo '<br><br>';
if ($result = $dbhandle->query($query))
{?>
<br><br><?php
var_dump($result->fetch_assoc());
printf("<p style='font-family:sans-serif; text-align: center;'>The components of the %s are listed below</p><table id='form' name='pump' style='margin: auto; padding: auto'>", $_POST["chlorinator"]);
while($row = $result->fetch_assoc())
{
printf ("<div><tr><td>%s</td><td><a href='%s' download>Download</a></td></tr>", $row["name"],$row["cutsheet_url"]);
}
printf('</table>');
}
?>
对于此特定示例,我将值“ 2075 GS”用作加氯器值。它通常通过选择框上的更改来传递,因此值是经过硬编码和正确的。此特定示例的输出为:
string(403)“ SELECT chlorinators.model_name,equipment.name,equipment.cutsheet_url,chlorinators.pump_specific,
equipment.file_name来自加氯机INNER JOIN chlorinator_equipment
开启chlorinators.chlorinator_index =
chlorinator_equipment.chlorinator_index INNER JOIN设备开启
chlorinator_equipment.equipment_index =装备。指数
WHERE chlorinators.model_name =“ 2075 GS”“
string(404)“ SELECT chlorinators.model_name,equipment.name,
equipment.cutsheet_url,chlorinators.pump_specific,
equipment.file_name来自加氯机INNER JOIN chlorinator_equipment
开启chlorinators.chlorinator_index =
chlorinator_equipment.chlorinator_index INNER JOIN设备打开
chlorinator_equipment.equipment_index =装备。指数
WHERE chlorinators.model_name =“ 2075 GS”“
我看不出两个输出之间有什么区别;关于一个字符差异在哪里以及如何消除它以使我的查询正常工作的任何想法?任何帮助是极大的赞赏。
最佳答案
您应该尝试使用以下适当的功能进行调试:
echo 'MySQL reports error #'.mysqli_errno($dbhandle).' - '.mysqli_error($dbhandle);
ps。查看您的代码,我无法发现您的错误。
关于php - PHP mysql返回null,硬编码的SQL查询有效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37147998/