dsdv76767671 2012-09-08 07:08
浏览 130

用于更改端口号的php数据库连接错误

I am facing database connection problem while trying to connect a .php file through wamp server
the error message is something like- "Access denied for the user " @ ' localhost' " for database
'aschool'. 'aschool' is my database name.
Mentioning that I've changed my port number of wamp server, I am worried that is it really for changing port number or anything else.Here is my code.

$con = mysql_connect();
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("aschool", $con);

After this line the error message comes. I've tried parameters "localhost" inside the mysql_connect()
function or more parameters but the result is same.
Thanks in advance anyone gives me any solution

  • 写回答

1条回答 默认 最新

  • dongzouban9871 2012-09-08 07:13
    关注

    That's because you are using mysql_connectwrong for your use case.

    If you check the documentation page it says that you can also a server-path, something like mysql_connect('localhost:1234', 'username', 'password').

    But you shouldn't use mysql_connect.

    Use PDO so that you can use parameterized queries.

    In code it would go like this:

    try
    {
      $pdo = new \PDO('mysql:dbname=aschool;host=127.0.0.1', 'myUser', 'myPassword');
    } catch (PDOException $exception)
    {
      // Do something with your exception.
      // Echo it, dump it, log it, die it.
      // Just don't ignore the exceptions!
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题