2014-02-27 48 views
2

我無法得到這個代碼正確連接到數據庫:無法使用PHP

$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()); 
} 

當我試圖腳本它說:

Parse error: syntax error, unexpected T_DNUMBER in /home/a8592246/public_html/pic/include/connect.php on line 3

如果posssible,任何人都可以複製和粘貼具有正確設置的代碼?

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

是什麼線路上無3? –

回答

2

,因爲它是更好地使用PDO我不使用的mysql_connect或ADOdb,但是讓我試試看!試試這個代碼:)

$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()); 
    } 
+0

Jack先生,謝謝!我從上面的代碼得到它:) 這是我需要的所有幫助。我做這個作爲一個愛好,所以我不知道任何關於PDO,ADOdb,MSQLI等只有一點點的MySQL。 (導入)並創建表格。 謝謝 – NightieeX

+0

因爲我沒有15的聲望我不能投票了,即時通訊:/ – NightieeX

+0

如果你有php 5.1或以上版本,PDO已經在[檢查出來](http://ca2.php.net/PDO) Upvoted your question to unlock you from the evil restrictions of new users :) –

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()); 
      } 
+0

我知道它的失蹤,其有目的的,我只是不知道在哪裏插入: mysql16.000webhost.com(根) a8592246_dbname(數據庫用戶) a8592246_dbuser(數據庫用戶) 輸入mypassword(數據庫密碼) 在腳本 – NightieeX

+0

@ user3320326請嘗試日期代碼 – Girish

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); 
?> 
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); 
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