php - MySQL - 得到两个不同的结果(PHP 和 phpMyAdmin)

标签 php mysql phpmyadmin

我正在尝试运行 SQL 查询,它在 phpMyAdmin 中运行正常,但在 PHP 中运行时,查询返回时非常不稳定。以下查询产生两个不同的结果:

SELECT `stock_ticker`, `stock_simpleName`, `stock_logo` 
FROM `stocks` 
WHERE stock_simpleName REGEXP'^c'

我在 phpMyAdmin 中得到以下结果(这是正确的):

stock_simpleName
----------------------
Coca-Cola
Campbell's
ConAgra Foods

但是,在 PHP 中,它的结果真的很奇怪:

stock_simpleName
-----------------------
Coca-Cola
MasterCard
Campbell's
Microsoft
The Walt Disney Company
PepsiCo
The Hershey Company
Proctor & Gamble
ConAgra Foods
...etc...

为什么会这样?这没有任何意义。是由于 PHP 中的服务器设置还是某种形式的编码或诸如此类的原因?


编辑:

这是我的 PHP 代码:

子模型类(件的创建者):

public function allOtherSearchResults($query, $dontQuery = null) {

    $name = "stocks";
    $where = "stock_simpleName REGEXP'^" . $query . "'";
    $cols = array("stock_ticker", "stock_simpleName", "stock_logo");
    $limit = 5;

    return $this->select($name, $cols, $where, $limit);

}

主模型类(运行查询):

public function select($tableName, $columns, $where = null, $limit = null) {

    global $purifier;

    // Make columns SQL friendly
    $cols = "`";
    $cols .= implode("`, `", $columns);
    $cols .= "`";

    $table = "`" . $tableName . "`";

    if (!empty($where)) {

        $where = " WHERE " . $where;

    }

    // Check limit
    if (!empty($limit)) {

        $limit = " LIMIT $limit";

    }

    // SQL CODE
    $sql = "SELECT " . $cols . " FROM " . $table . $where . $limit;

    // SQL DEBUGGING IF CODE RETURNS BOOLEAN ERROR
    echo $sql . "<br>";

    $query = $this->conn->query($sql);

    // Store the value in a variable called table with an array of that table's name followed by it's values
    // EX: $model->table["bands"]["band_name"]
    //
    // Accessible by the individual page/directory's controller's

    while($row = $query->fetch_assoc()){

        // Store values as $model->table["tableName"]["columnName"]["index (usually 0)"]
        foreach ($row as $key => $val) {
            $this->data[$tableName][$key][] = $row[$key];
        }

    }


    // Loop through results to clean them
    // Foreach loops through each column
    // Make sure the table isn't empty (i.e. login returns an error)
    if (!empty($this->data[$tableName])) {
        foreach ($this->data[$tableName] as $key => $tableArray) {

            // For loop goes through each value in a certain row
            for ($i = 0; $i < count($tableArray); $i++) {
                // Convert from data variable to table after HTML PURIFIER
                $this->table[$tableName][$key][$i] = $purifier->purify($tableArray[$i]);
            }

        }
    }


    // Declare the array after loop has finished for use in view
    $this->table;

    if (!empty($this->table)) {

        return true;

    }

}

它给了我与上面相同的 SQL 输出。我不确定 PHP 中的某些字符与 phpMyAdmin 中的标准 MySQL 是否有不同的解释。以前有人遇到过这个问题吗?

最佳答案

我猜,^ 字符有问题。 尝试设置正确的连接和结果编码,eq。

$this->conn->query("MYSQL SET NAMES utf8");
$this->conn->query("MYSQL SET CHARACTER SET utf8");

此外,请检查您的 php 脚本文件是否以 UTF-8 编码保存。

此外,您应该考虑使用准备好的语句(甚至可以防止 SQL 注入(inject)):

$this->conn->prepare("SELECT * FROM `stocks` WHERE `stock_simpleName` REGEXP ?");
$this->conn->bind_param("s", "^c");
$this->conn->execute();
$query = $this->conn->get_result();

关于php - MySQL - 得到两个不同的结果(PHP 和 phpMyAdmin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33581413/

相关文章:

php - 无法连接 MySQL

mysql - 选择包含输入数组所有值的字段

php - 在 MySQL 中的十进制列中查找最接近的匹配项

php - mysql 请求没有返回想要的内容

mysql - 日期功能不起作用

php - PHP 操作码缓存是否与 __autoload 一起使用?

php - 在 WordPress 之外获取具有自定义尺寸的特色图像

mysql - phpmyadmin:如何取消导入大数据库的时间限制

php - 如何使用 php、DomDocument 和 DomXPath 查询 graphml?

php - 仅在选定的页面上放置 WordPress 插件