【发布时间】:2016-02-08 11:06:54
【问题描述】:
我正在尝试运行 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 是否有不同的解释。以前有人遇到过这个问题吗?
【问题讨论】:
-
您尝试过使用 LIKE 吗? WHERE stock_simpleName LIKE 'C%'
-
显示你的php代码
-
@ArmandoSM 我尝试了许多不同的查询。它们都在 MYSQL 中工作,只是在 PHP 调用代码时它们不起作用。作为回报,我不断得到随机结果。
-
echo $sql向您展示了什么? -
@Mureinik 它向我显示了与答案开头相同的代码。我通常将其注释掉。
标签: php mysql phpmyadmin