php - 'abcdefg' 23 中的未知列 'where clause'

标签 php mysql database mysqli

好的,我在查询时遇到了一些问题。我也在努力学习MySQLi,所以可能会有一些错误。我有一个名为 Authentication 的表,其中包含以下列

||id
||UserName
||Password

运行查询时,我将用户名作为列名,因此它给出了未知列错误。我似乎看不出我的代码有什么问题。如有任何帮助,我们将不胜感激。

 <?php
// Report all errors
error_reporting(E_ALL);
session_start(); // Start PHP
// Get info sent to server from login form.
$my_username = $_POST['username'];
$my_password = $_POST['password'];
// MD5 Encrypt the password.
$my_password_md5 = md5($my_password);
// Connect to DB
$db = new MySQLi('localhost', 'user', 'password!', 'database');
if ($db->connect_error) {
    $error = $db->connect_error;
}

//SQL query
$sql = <<<SQL
    SELECT UserName
    FROM `Authentication`
    WHERE `username` = $my_username HAVING `username` = $my_password_md5
SQL;

$result = $db->query($sql) or die($db->error.__LINE__);

if($result = $db->query($sql))
$rows=mysqli_fetch_assoc($result);
// Count how many rows match that information.
$count=mysqli_num_rows($result);
// Check if there are any matches.
if($count==1)
{// If so, register $my_username, $my_password and redirect to the index page.
ini_set("session.gc_maxlifetime", "18000"); 
session_cache_expire(18000);
$cache_expire = session_cache_expire();
$_SESSION['username'] = $my_username;
$_SESSION['id'] = $rows['id'];
header("location:http://somesitegoeshere.com");
}

// If not, redirect back to the index page and provide an error.
else {
header("location:http://somesitgoeshere.com?err=1");
}
?>

最佳答案

$sql = <<<SQL
    SELECT UserName
    FROM `Authentication`
    WHERE `username` = $my_username HAVING `username` = $my_password_md5
SQL;

您忘记引用$my_username。所以你的查询看起来像 WHERE 'username' = abcdefg HAVING...

Mysql 认为您正在尝试与列进行比较,请将您的用户名放在引号中。还要将您的密码放在引号中,这样它就不会认为您的密码是一列。

$sql = <<<SQL
    SELECT UserName
    FROM `Authentication`
    WHERE `username` = "$my_username" HAVING `username` = "$my_password_md5"
SQL;

关于php - 'abcdefg' 23 中的未知列 'where clause',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28589054/

相关文章:

php - Symfony 服务器 :run in php Docker container

php - php 代码需要简单的 sql 查询

php - Laravel如何获取两个结果值的平均值

android - SQLite 数据库和游标

mysql - 数据库设计 - 来自 super /父实体的共享关联

php - 将 xmlns 值添加到 Symfony2 SoapServer 响应

php - 使用 preg_replace 在 PHP 字符串中查找电话号码的终极方法

mysql - 是否可以使用MySQL在存储过程中动态创建表的名称?

php - Codeigniter配置文件页面功能不断循环

database - 持久化 JSON 数据(使用 yang 模型验证)