2015-01-04 98 views
0

我只想从我的浏览器测试数据库连接。但我得到空白页面。我没有收到错误消息。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); 

?> 
+0

你有没有试着用:'localhost'作为东道主?也许你想要db名称的第四个参数 – Rizier123 2015-01-04 12:07:51

+0

'mysqli_connect'不会将URL作为连接。只使用主机名。此外,我们需要知道数据库的位置,实际运行PHP的位置以及网络设置的一些细节(例如,如果您在* EC2 VPC内部,即PHP服务器和数据库服务器都在那里,您可能不想使用数据库的* public * IP地址/名称进行连接。)您还需要确保您在实例本身上为MySQL打开了防火墙,并且为了实例的安全性如果您尝试从外部连接,则可以使用AWS中的策略。 – 2015-01-04 12:10:11

+0

yaa localhost在我的电脑上正常工作。但我托管在亚马逊的Ubuntu服务器。 – rajagopalx 2015-01-04 12:10:21

回答

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