简体   繁体   English

PHP mysql返回null,硬编码的SQL查询有效

[英]PHP mysql returns null, hard coded SQL query works

I'm having an issue in which I'm unable to get my database query within a PHP program to work. 我遇到一个问题,无法在PHP程序中运行数据库查询。 The query works fine within the program if it is hard coded, but it fails otherwise and passes back no results. 如果对查询进行了硬编码,则该查询在程序中可以正常运行,但否则将失败,并且不传递任何结果。 I have echo'd the two results and am given a different string length, but the string I am given is identical (strings gotten via var_dump). 我已经回显了两个结果,并且给出了不同的字符串长度,但是给出的字符串是相同的(通过var_dump获得的字符串)。 I'm at my wit's end; 我机智的尽头; I'm not really sure what is the issue with the query. 我不太确定查询是什么问题。 I have tried several different fixes which I found for similar problems, but none of them have worked. 我曾尝试过针对类似问题找到的几种不同修复程序,但均无效果。 I trim the posted input and also have the variable double quoted as opposed to single quoted so that the reference executes. 我修剪了发布的输入,并且还对变量加了双引号而不是单引号,以便执行引用。 I really just have no clue what's wrong. 我真的不知道怎么了。 Here is the code that's relevant to this project: AJAX call to php class: 以下是与此项目相关的代码:php类的AJAX调用:

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 );
            }});

And here is the relevant php code: 这是相关的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>');
}


?>

For this particular example I'm using the value '2075 GS' as the chlorinator value. 对于此特定示例,我将值“ 2075 GS”用作加氯器值。 It is generally passed via change on a selection box, so the values are hard coded and correct. 它通常通过选择框上的更改来传递,因此这些值是经过硬编码和正确的。 The output of this specific example is: 此特定示例的输出是:

string(403) "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"" string(403)“ 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.chlorinatoripindex_INNER JOIN设备ON chlorinator_equipment_ment_ment_ment_E_WIN chlorinators.model_name =“ 2075 GS”“

string(404) "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"" string(404)“ 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.chlorinatoripindex_INNER JOIN设备ON chlorinator_equipment_ment_ment_ment_E_WIN chlorinators.model_name =“ 2075 GS”“

I don't see any difference between the two outputs; 我看不出两个输出之间有什么区别; any idea as to where the one character difference is and how I can eliminate it so that my query will properly work? 关于一个字符差异在哪里以及如何消除它以使我的查询正常工作的任何想法? Any help is greatly appreciated. 任何帮助是极大的赞赏。

Use double quoted string to build these query striings its so much easier as $variables are then automatically expanded and you dont have to fiddle with . 使用双引号字符串来构建这些查询记录变得如此容易,因为$ variables随后会自动扩展,而您无需费心. concatenation at all 串联

$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'";

Of course it could be an annoying byte corruption in your source file. 当然,这可能是源文件中令人讨厌的字节损坏。 I cant count the hours I have wasted trying to work out why code wont compile when it was just a byte corruption in the source. 我无法数出我在试图弄清楚为什么代码只是源代码中的字节损坏而无法编译的时间。 In that case copy and paste the code to a new file, then delete the old file and rename your new file to the old filename 在这种情况下,将代码复制并粘贴到新文件中,然后删除旧文件并将新文件重命名为旧文件名。

I think the reason you are seeing no data in the page is that you have read it and thrown it away 我认为您在页面中看不到任何数据的原因是您已经阅读并丢弃了它

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;

?>

You should try debugging with the proper functions like this: 您应该尝试使用以下适当的功能进行调试:

echo 'MySQL reports error #'.mysqli_errno($dbhandle).' - '.mysqli_error($dbhandle);

ps Looking at your code, I cannot spot your error. ps查看您的代码,我无法发现您的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM