dtlygweb2017 2010-11-02 23:11
浏览 8
已采纳

PHP无法连接到MySQL

$link = mysql_connect('localhost', $username, $password);
if (!$link) 
{
     die('Could not connect: ' . mysql_error());
}

I get this error:

Could not connect: Access denied for user 'www-data'@'localhost' (using password: NO)

Why?

Edit:

$username="root";
$password="root";
$database="test";

    function Save($name)
    {
        $link = mysql_connect('localhost', $username, $password);
        if (!$link) {
            die('Could not connect: ' . mysql_error());
        }
        @mysql_select_db($database) or die( "Unable to select database");
        $query = "INSERT INTO test (name)" .
                 "VALUES ('" . $name . "')";
        mysql_query($query);
        mysql_close();
    }
  • 写回答

5条回答 默认 最新

  • dongwalun2507 2010-11-03 00:01
    关注

    From your edit, the issue appears to be a scoping one.

    You're attempting to make a connection inside a function where the $username, $password and $database variables are defined outside that function.

    I suggest you read the Variable Scope section of the manual and turn up your error reporting as @netcoder suggests in the question comments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b