【问题标题】:SQL injection proof SELECT with LIKE and wildcard into associative array带有 LIKE 和通配符的 SQL 注入证明 SELECT 到关联数组中
【发布时间】:2016-08-01 06:17:15
【问题描述】:

计划是创建一个面向公众的互联网服务,可以自动完成数据库中的单词。我使用常规的 mysql 查询让它成功运行,但我担心因为我将把这个连接包含到一个 wordpress 插件中,它会使我的服务器容易受到攻击。

我在 MYSQL 中创建了一个新用户,仅对该表具有选择权限,因此我不担心在脚本中泄露密码,因为我相信它无论如何都会有助于调试。为了调试目的,我放了各种回声等。最终这个输出将进入一个 Json 文件,并由 Jquery UI 自动完成检索。

基本上我遇到的问题是我无法让查询工作。我尝试逐行添加代码,但连接不起作用。随意在 mysql 工作台上测试凭据,IP 地址为 46.101.8.220。使调试变得更加困难的是代码中的某个人正在阻止从 Web 浏览器查看页面输出时出现的任何内容。任何帮助都将不胜感激,因为我花了很多时间尝试 Mysqli、PDO、面向对象和程序,试图让它工作。

$term = isset($_GET['term']) ? $_GET['term'] . '%': 'test%';
echo $term;

$conn = new mysqli('localhost', 'search-words','','admin_soup');
print ($conn ? 'connected' : 'cant con');
$query = 'SELECT id as value, word as label FROM word where word LIKE ?;';
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $term)
    if ($stmt->execute()){
        $result = $stmt->get_result();
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
            $row_set[] = $row;
        }
    }
echo json_encode($row_set);//format the array into json data
$stmt->close();
$conn->close();
print_r($row_set);

在第一个建议代码更改为并正常工作之后。谢谢!

$term = isset($_GET['term']) ? $_GET['term'] . '%': 'test%';
echo $term;
$conn = new mysqli('localhost', 'search-words','','admin_soup');
print ($conn ? 'connected' : 'cant con');
$query = 'SELECT id as value, word as label FROM word where word LIKE ?;';
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $term);
    if ($stmt->execute()){
        $stmt->bind_result($value,$label);
        while($row = $stmt->fetch()){
            $row_set[] = array('value'=>$value, 'label'=>$label);
        }
    }

echo json_encode($row_set);//format the array into json data
$stmt->close();
$conn->close();

代码仍然没有返回任何内容。仔细检查了用户信息。当我测试 sql 语句时,我唯一需要更改的是我需要在“test%”周围添加引号,但我认为 bind_param 不需要引号。如果我从 $query downwars 中删除所有代码,如果我再次在下面添加,它将在屏幕上显示连接的消息。屏幕什么也没有显示。

我的服务器上可能有什么需要更新的东西吗?

【问题讨论】:

    标签: php mysql mysqli pdo


    【解决方案1】:
    $stmt->bind_param("s", $term)
    

    缺少“;”

    $result = $stmt->get_result();
    

    get_result() 方法并不总是可用。查看php.net:http://php.net/manual/en/mysqli-stmt.get-result.php

    仅适用于 mysqlnd。

    这就是 pdo 如此方便的原因之一......

    但我猜你可以使用bind_result() 然后fetch() 示例中显示的数据http://php.net/manual/en/mysqli-stmt.fetch.php

    if ($stmt->execute()){
        $stmt->bind_result($value, $label);
        while($row = $stmt->fetch()){
            $row_set[] = array('value' => $value, 'label' => $label);
        }
    }
    

    通过使用 PDO,代码可能如下所示:

    $stmt = $dbh->prepare('SELECT id as value, word as label FROM word where word LIKE ?');
    $stmt->execute(array($term));
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    

    【讨论】:

    • 我尝试了 mysqli 解决方案,但仍然一无所获,我用一些新想法更新了这个问题。我开始怀疑这是否与代码无关。
    • @StephenMclaughlin $row_set[] = array('value'=>$value, 'label'=>$label) 缺失;再次。
    • 只见树木不见森林 :'(
    猜你喜欢
    • 2014-06-24
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    相关资源
    最近更新 更多