Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Ví dụ kết nối MySQL bằng PHP script (mysql_connect)

Script PHP kết nối MySQL với user guest, password guest123:

<html>
  <head>
    <title>Connecting MySQL Server</title>
  </head>
  <body>
    <?php
      $dbhost = 'localhost:3306';
      $dbuser = 'guest';
      $dbpass = 'guest123';
      $conn = mysql_connect($dbhost, $dbuser, $dbpass);
      if(! $conn ) {
        die('Could not connect: ' . mysql_error());
      }
      echo 'Connected successfully';
      mysql_close($conn);
    ?>
  </body>
</html>

Lưu ý: mysql_connect deprecated từ PHP 5.5, xóa PHP 7. Dùng mysqli_connect hoặc PDO.