【问题标题】:php and sql query failingphp和sql查询失败
【发布时间】:2010-09-26 00:45:13
【问题描述】:

我有这个代码

http://www.nomorepasting.com/getpaste.php?pasteid=22580

这是一个小型 ajax 应用程序的一部分。我想知道一种更好、更有效的方法来分配 $query,而不是每次都用不同的查询或一堆 if 子句复制 sql。基本上查询将取决于单击的链接,但我不确定如何在逻辑中显示它。我也不确定为什么我在 $result 中的 SQL 查询失败了。

【问题讨论】:

    标签: php sql


    【解决方案1】:

    更新:我将 Eran 的功能集成到重构的代码中。注意:我通过将 $table 变量传递给它并重命名它来更正它,因为它不只搜索查询文本,而是主要返回所需的行!

    主要错误

    • 错误 1:在所有情况下都使用 query2 覆盖查询,这会破坏代码。
    • 错误 2:LIKE'%$query%' LIKE 和 '=> LIKE '%... 之间缺少一个空格...这很可能也会破坏您的代码

    其他问题

    • 安全问题:sql注入危险,使用mysql_real_escape_string
    • \n 不独立于平台:使用 PHP_EOL
    • 另一种写短 if 块的方法
    • 使用大括号表示普通 if 结构和所有此类结构

    这里是你的代码有一些变化,看看 cmets

    <?php
    session_start(); //ommit, no session var used
    
    //use braces, always!
    //you may write such statements with the short form like
    if (isset($_GET['cmd'])) : $cmd = $_GET['cmd']; else : die (_MSG_NO_PARAM); endif;
    
    $query = '';
    //escpae your input - very important for security! sql injection!
    if ( isset ($_GET["query"]))
    {
        $query = mysql_real_escape_string($_GET["query"]);
    }
    //no need for the other part you had here
    
    $con = mysql_connect("localhost", "root", "geheim");
    
    if (!$con) : die ('Connection failed. Error: '.mysql_error()); endif;
    
    mysql_select_db("ebay", $con);
    
    if ($cmd == "GetRecordSet")
    {
        $table = 'Auctions';
        $rows = getRowsByArticleSearch($searchString, $table);
    
        //use PHP_EOL instead of \n in order to make your script more portable
    
        echo "<h1>Table: {$table}</h1>".PHP_EOL;
        echo "<table border='1' width='100%'><tr>".PHP_EOL;
        echo "<td width='33%'>Seller ID</td>".PHP_EOL;
        echo "<td width='33%'>Start Date</td>".PHP_EOL;
        echo "<td width='33%'>Description</td>".PHP_EOL;
        echo "</tr>\n";
    
        // printing table rows
        foreach ($rows as $row)
        {
            $pk = $row['ARTICLE_NO'];
            echo '<tr>'.PHP_EOL;
            echo '<td><a href="#" onclick="GetAuctionData(\''.$pk.'\')">'.$row['USERNAME'].'</a></td>'.PHP_EOL;
            echo '<td><a href="#" onclick="GetAuctionData(\''.$pk.'\')">'.$row['ACCESSSTARTS'].'</a></td>'.PHP_EOL;
            echo '<td><a href="#" onclick="GetAuctionData(\''.$pk.'\')">'.$row['ARTICLE_NAME'].'</a></td>'.PHP_EOL;
            echo '</tr>'.PHP_EOL;
        }
    }
    mysql_free_result($result);
    //mysql_close($con); no need to close connection, you better don't
    
    
    function getRowsByArticleSearch($searchString, $table) 
    {
        $searchString = mysql_real_escape_string($searchString);
        $result = mysql_query("SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME FROM {$table} WHERE upper ARTICLE_NAME LIKE '%" . $searchString . "%'");
        if($result === false) {
                return mysql_error();
        }
        $rows = array();
        while($row = mysql_fetch_assoc($result)) {
                $rows[] = $row;
        }
        return $rows;
    }
    
    // ?> ommit closing php tag
    

    【讨论】:

    • 我打算这样做但跑了。为你 +1
    • 不需要 PHP_EOL。它是 HTML。首先,换行无关紧要。其次,我见过的所有源代码查看器(记事本除外)都只使用“\n”。
    • 在 "LIKE'%...%'" 中省略空格是没有问题的 - 它是函数名周围的括号,当我尝试它时会出错。
    【解决方案2】:

    您可以在一个接受搜索文本作为参数的函数中抽象您的查询。比如:

    function searchQuery($text) {
        $text = mysql_real_escape_string($text);
        $result = mysql_query("SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME FROM {$table} WHERE upper ARTICLE_NAME LIKE '%" . $text . "%'");
        if($result === false) {
            return mysql_error();
        }
        $rows = array();
        while($row = mysql_fetch_assoc($result)) {
            $rows[] = $row;
        }
        return $rows;
    }
    

    请注意,您应该转义用户输入以防止 SQL 注入攻击(这里我使用 mysql_real_escape_string() 来做到这一点)。如果查询失败,这个函数也会返回错误码,所以你应该检查结果是否是一个数组:

     $result = searchQuery($_GET['query']);
     if(!is_array($result) ) {
          echo 'An error has occurred:' . $result;
     } else {
       //iterate over rows
     }
    

    用大括号 { 包裹您的逻辑结构 (IF/ELSE)。更好的可读性,有助于避免不必要的错误。

    【讨论】:

      【解决方案3】:

      你应该像nickf说的那样做。

      而且你肯定很容易发生 SQL 注入:

      维基书:http://en.wikibooks.org/wiki/Programming:PHP:SQL_Injection 长文:http://www.securiteam.com/securityreviews/5DP0N1P76E.html

      【讨论】:

        【解决方案4】:

        您可能需要在 LIKE 和“%$query%”之间留一个空格。此外,您应该查看 mysql_error() 函数 - 让 MySQL 准确地告诉您错误是什么。

        【讨论】:

        • 至少在我测试时,缺少的空间不会导致任何问题。
        【解决方案5】:

        用于功能用途:

        $result = mysql_query($sql_query) or die(mysql_error());
        

        看看你得到什么样的mysql错误。

        【讨论】:

          【解决方案6】:
          "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME
          FROM {$table} WHERE upper ARTICLE_NAME LIKE'%$query%'"
          

          您需要在upper 函数的参数周围加上方括号。将您的查询更改为此,它应该可以工作:

          "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME
          FROM {$table} WHERE upper(ARTICLE_NAME) LIKE'%$query%'"
          

          【讨论】:

            【解决方案7】:

            您没有将 IF/THEN/ELSE 结构中的语句包含在 accolades 中,因此只有每个块中的第一个语句有条件地执行,其余的都是。

            在大多数情况下,您会将 $query2 分配给 $query,而 $query2 可能尚未定义。

            另一个提示:清理您的输入,不要像那样将用户输入粘贴到您的 SQL 中,这很危险。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-04-05
              • 2012-05-20
              • 2019-06-18
              • 1970-01-01
              • 2014-05-21
              • 1970-01-01
              相关资源
              最近更新 更多