dou12352 2012-05-14 03:28
浏览 109

mysqli_connect失败并显示dbpassword

My system seems to have some weird problems I can't explain. I have a following code snip that I would use to connect to and read data from the database

define('DB','products');
define('HOST','localhost');
define('DBPASS','password');
define('DBUSER','root');
define('DBTABLE','product_tb');
$conn=mysqli_connect(HOST,DBUSER,DBPASS) or die('Could not connect: ' . mysqli_error($conn));

But it always reports an error that I can't make a connection with mysqli_connect. I try running this code on another system and it works fine. Here are the two warning and an failed connection error.

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'root'@'localhost' (using password: NO) in D:\xampp\htdocs\products.php on line 7

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in D:\xampp\htdocs\products.php on line 7 Could not connect:

This happens when I test system doesn't require password, so I left the DBPASS as blank but once I use this on the real system, it fails with the exact same error, no matter whether I left out the DBPASS or not.... I thought it was cache but I restarted mysql and apache but nothing works then. Thank you if you could offer some help.

  • 写回答

2条回答 默认 最新

  • dongwei1921 2012-05-14 03:55
    关注

    To check for connection errors you should use mysqli_connect_error() not mysqli_error()

    That will solve one of your error messages.

    $conn=mysqli_connect(HOST,DBUSER,DBPASS) or die('Could not connect: ' . mysqli_connect_error());
    

    The other error message is caused by invalid privileges in the database server. Stopping you from accessing the database. Like @Dragon commented, can you login with the username/password combo from the cmdline. If you don't have cmd line access, do you have php myadmin or something else, that you can check the user/pass/db combo with to make sure it works.

    Also as @MarcB says, it would be wise to use a login that only has access to a specific database on your server rather then root access to all databases. That way if you code got hacked, they would only have access to the 1 database (in theory).

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助