我遇到了一个问题,我无法在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>');
}
?>
对于这个特殊的例子,我使用了价值&#39; 2075 GS&#39;作为氯化剂值。它通常通过选择框上的更改传递,因此值是硬编码和正确的。这个具体例子的输出是:
string(403)&#34; SELECT chlorinators.model_name,equipment.name,equipment.cutsheet_url,chlorinators.pump_specific, equipment.file_name FROM氯化器INNER JOIN chlorinator_equipment ON chlorinators.chlorinator_index = chlorinator_equipment.chlorinator_index INNER JOIN设备ON chlorinator_equipment.equipment_index = equipment.equipment_index 在哪里chlorinators.model_name =&#34; 2075 GS&#34;&#34;
string(404)&#34; SELECT chlorinators.model_name,equipment.name, equipment.cutsheet_url,chlorinators.pump_specific, equipment.file_name FROM氯化器INNER JOIN chlorinator_equipment ON chlorinators.chlorinator_index = chlorinator_equipment.chlorinator_index INNER JOIN设备ON chlorinator_equipment.equipment_index = equipment.equipment_index 在哪里chlorinators.model_name =&#34; 2075 GS&#34;&#34;
我看不出两个产出之间有什么不同;任何关于一个字符差异在哪里以及如何消除它以使我的查询正常工作的想法?非常感谢任何帮助。
答案 0 :(得分:0)
使用双引号字符串来构建这些查询条带它变得非常容易,因为$变量随后会自动扩展,你根本不需要使用.
连接
$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'";
当然,它可能是源文件中令人讨厌的字节损坏。我无法计算我浪费的时间试图解决为什么代码不会编译时,它只是源中的字节损坏。在这种情况下,将代码复制并粘贴到新文件,然后删除旧文件并将新文件重命名为旧文件名
我认为您在页面中看不到任何数据的原因是您已阅读并将其丢弃
if ($result = $dbhandle->query($query)) {
echo '<br><br>';
// this line read your result row and ignored it
//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"]);
// now the data should be available to read here
// I assume you only have one row with
// chlorinators.model_name= "2075 GS"
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>');
} else {
// there must have been an erro in the query we just executed
// so show it.
echo $dbhandle->error;
exit;
?>
答案 1 :(得分:0)
您应该尝试使用正确的函数进行调试:
contractBegin
P.S。查看您的代码,我无法发现您的错误。