无法连接到godaddy localhost

时间:2018-01-21 17:54:48

标签: php mysql

我在godaddy mysql数据库中设置了一个数据库和表,我知道我有我的用户,传递和数据库信息正确,但我不断收到错误"无法连接数据库&#34 ;没有进一步的信息。 Godaddy说我说。使用' localhost'作为主机名。 我知道我应该使用PDO来保证安全性,但是现在我只想简单地连接并使用最简单的sql语句对其进行测试。我的代码中的某些内容可能是错误的,这使我无法连接吗?或者假设我的登录信息正确,下面的内容是否正常?

//连接数据库

$hostname="localhost";
$username="***";
$password="***";
$dbname="***";


mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);

# Check If Record Exists

$query = "SELECT * from schools where schools_state=WA ";

$result = mysql_query($query);

if($result){
    while($row = mysql_fetch_array($result)){
        $name = $row["$school_name"];
        echo "School: ".$school_name."<br/>";
    }
}

&GT;

1 个答案:

答案 0 :(得分:0)

你可以看看这个 - 它应该有助于开始使用mysqli连接。

$dbhost =   'localhost';
$dbuser =   'xxx'; 
$dbpwd  =   'xxx'; 
$dbname =   'xxx';

$db =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );

$sql='SELECT `school_name` from `schools` where `schools_state`=?';
$stmt=$db->prepare($sql);
if( $stmt ){
    $stmt->bind_param('s',$state);
    $state='WA'; /* a POST variable perhaps ? */

    $result=$stmt->execute();
    if( $result ){
        $stmt->store_result();
        if( $stmt->num_rows > 0 ){
            $stmt->bind_result( $name );
            while( $stmt->fetch() ){
                echo $name;
            }
        }
        $stmt->close();
    }
    $db->close();
}
相关问题