PHP打开数据库连接[重复]

This question already has an answer here:

I just wanna ask for your help guys. Im having a problem with my coding which is to open database connection. Here is my code.

<?php

define('MYHOST', 'localhost');
define('MYDATABASE', 'system');
define('MYUSERNAME', 'root');
define('MYPASSWORD', 'root');


$conn = mysql_connect("localhost", "root", "", "")

if ($mydb->connect_error) {

  die("Connection Error Message: ".$mydb->connect_error);
 }

?>  

and it show an error like this

Parse error: syntax error, unexpected 'if' (T_IF) in C:\xampp\htdocs\system\database.php on line 11

thank you guys for your answer..

</div>

try this one

$conn = mysql_connect("localhost", "root", "");

This extension (mysql_) was deprecated in PHP 5.5.0, please use mysqli or PDO(PHP Data Objects) instead.

If your PHP versional lower than 5.5.0, you can edit this line:

$conn = mysql_connect(MYHOST, MYDATABASE, MYUSERNAME, MYPASSWORD);

to replace this:

$conn = mysql_connect("localhost", "root", "", "")

There are two errors in the your code:

  • You are missing the semicolon(;) in the line number 11 .
  • You are using the invalid variable for the connection($mydb), replace it with ($conn).

Try the below script, hope this helps:

<?php

    define('MYHOST', 'localhost');
    define('MYDATABASE', 'system');
    define('MYUSERNAME', 'root');
    define('MYPASSWORD', 'root');


    $conn = mysql_connect("localhost", "root", "", "");

    if ($conn->connect_error) {

      die("Connection Error Message: ".$conn->connect_error);
     }

    ?>