PHP连接到mysql服务器数据库

时间:2015-01-04 12:06:08

标签: php mysql

我只是想从浏览器测试数据库连接。但我得到空页。我没有收到错误消息。

<?php

    $con =  mysqli_connect('http://ec2-54-67-69-153.us-west-1.compute.amazonaws.com/', 'root', 'root') or die(mysqli_error($con));

    if ($con) {
        echo "success";
    } else {
        echo "fail";
    }

    mysqli_close($con);

?>

1 个答案:

答案 0 :(得分:0)

**Here is a handy function that connects you to a Database, using *Object Oriented* style:**
function MySQLi_quickConnect()
{
  $host     = 'somewebsite.db.120327161.hostedresource.com'; //or 'http://localhost'
  $username = '<YOUR USERNAME>';
  $password = '<YOUR PASSWORD>';
  $database = '<YOUR DATABASES NAME>';

  $db = new MySQLi($host,$username,$password,$database);
  $error_message = $db->connect_error;
  if($error_message != NULL){die("Error:" . $error_message . "<br>" . "Occured in function
                                         MySQLi_quickConnect");}
  return $db;   
}

**A simple example on how you would query the Database:**
$db = MySQLi_quickConnect(); //this is your new Database object
$sql = "<YOUR SQL STATEMENT>"; 
$stmt = $db->query($sql);
if(!$stmt){die('MyError : ('. $db->errno .') '. $db->error);}  //kill script, show errors