无法使用PHP连接到数据库

时间:2014-02-27 04:45:27

标签: php mysql

我无法正确使用此代码:

$connection=mysql_connect(db_server, db_user, db_pass);
if(!$connection)
{
  die("Database Connection failed".mysql_error());
}

$db_select=mysql_select_db(db_name,$connection);
if(!$db_select)
{
  die("Database Selection failed".mysql_error());
}

当我尝试脚本时,它说:

  

解析错误:语法错误,第3行/home/a8592246/public_html/pic/include/connect.php中的意外T_DNUMBER

如果可能,任何人都可以使用正确的设置复制并粘贴代码吗?

$mysql_host = "mysql16.000webhost.com";
$mysql_database = "a8592246_dbname";
$mysql_user = "a8592246_dbuser";
$mysql_password = "mypassword";

5 个答案:

答案 0 :(得分:3)

第二个}语句中缺少

if,并在mysql_connectmysql_select_db个函数中使用了正确的参数 请尝试此代码

         $mysql_host = "mysql16.000webhost.com";
         $mysql_database = "a8592246_dbname";
         $mysql_user = "a8592246_dbuser";
         $mysql_password = "mypassword"; 



            $connection=mysql_connect( $mysql_host,$mysql_user,$mysql_password);
            if(!$connection){
               die("Database Connection failed".mysql_error());
            }

            $db_select=mysql_select_db( $mysql_database,$connection);
            if(!$db_select) {
               die("Database Selection failed".mysql_error());
             }

答案 1 :(得分:2)

我不使用mysql_connect,因为最好使用PDO或ADOdb,但lemme试一试!试试这段代码:)

    $connection=mysql_connect("mysql16.000webhost.com", "a8592246_dbuser", "mypassword");
    if(!$connection)
    {
    die("Database Connection failed".mysql_error());
    }

    $db_select=mysql_select_db("a8592246_dbname",$connection);
    if(!$db_select)
    {
    die("Database Selection failed".mysql_error());
    }

答案 2 :(得分:1)

mysqli_connect(host,username,password,dbname);

host: you need to write here IP address or the name of the host you are working on eg:
mysqli_connect("localhost",username,password,dbname);

username: you need to mention the username here eg:
mysqli_connect(host,"root",password,dbname); 

password: write password if any.

dbname: write name of the db you want to connect with. eg.
mysqli_connect(host,username,password,"my_db");

在你的情况下:

$mysql_host = "mysql16.000webhost.com";
$mysql_database = "a8592246_dbname";
$mysql_user = "a8592246_dbuser";
$mysql_password = "mypassword";

mysqli_connect($mysql_host, $mysql_user,$mysql_password,$mysql_database);

答案 3 :(得分:1)

根据您提供的代码,请参阅以下内容: -

$mysql_host = "mysql16.000webhost.com";
$mysql_database = "a8592246_dbname";
$mysql_user = "a8592246_dbuser";
$mysql_password = "mypassword";


    $connection=mysql_connect($mysql_host, $mysql_user, $mysql_password);
    if(!$connection)
    {
         die("Database Connection failed".mysql_error());
    }

    $db_select=mysql_select_db($mysql_database,$connection);
    if(!$db_select)
    {
       die("Database Selection failed".mysql_error());
    }

在现有代码中, mysql_connect()功能和 db_name

中缺少 $

答案 4 :(得分:0)

尝试使用 mysqli _connect而不是 mysql _connect

<?php

     $con=mysqli_connect(db_server,db_user,db_pass,"database_name");

  /** Check connection **/
  if (mysqli_connect_errno())
  {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  else{
      echo "Connection established ";
  }

mysqli_close($con);
?>