douzi1986 2015-11-16 07:04
浏览 53

只需尝试连接数据库(初学者)时SQL语法出错

I already used some solutions put forward in other threads but it just doesn't want to work. My MYSQL version is: 5.5.38 and here is the code I use (the goal is to simply connect to the database). The code goes on, that's why the "{" is not closed from the if-clause, but the important part that should connect to the database is listed below:

$user = mysql_real_escape_string($_POST['user']);
$password = ($_POST['password']);

if ($_POST['user'] <> "" AND $_POST['password'] <> "")
{
mysql_connect('localhost', 'xxx', 'xxx');
mysql_select_db('xxx');

$sql = "SELECT * AS user FROM xxx WHERE nutzer LIKE '$nutzer' AND pw LIKE '$password'" ;

$result = mysql_query($sql);
if (false === $result) {
die (mysql_error());
}

$datensatz = mysql_fetch_array($result, MYSQL_ASSOC);

These errors come right at the beginning when I just open the site:

Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in /var/www/web1159/html/lehrer.php on line 15

Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/web1159/html/lehrer.php on line 15

And this is the error on which I stumble when I try to log in and connect to the database. I know what it says, but it does not make much sense to me - are the " or ' wrongly placed? I tried other variations - still doesn't work. And why line 1??:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS user FROM lehrerzugang WHERE nutzer LIKE '' AND pw LIKE 'lfb'' at line 1

  • 写回答

1条回答 默认 最新

  • doubu4406 2015-11-16 07:16
    关注

    Setup your database connection before calling mysql_real_escape_string.

    Besides the default username and passowrd for localhost are 'root' and empty string '', so unless you have explicitly changed these credentials you need to provide the access as root user with empty sting password.

    mysql_connect('localhost', 'root', '');
    mysql_select_db('yourDatabase');
    

    If you have setup your own user access to the database then make sure you provide the correct credentials.

    mysql functions are now deprecated in PHP and there is an Improved MYSQL library to support database connection and muanipulation mysqli, so use it.

    You cannot give an alias to all columns selection * so SELECT * AS is invalid, aliases can be given to specific column selection SELECT dateOfBirth AS DOB, dateCreated AS DC FROM yourTable

    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型