无法连接到godaddy localhost [英] Can't connect to godaddy localhost

查看:67
本文介绍了无法连接到godaddy localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Godaddy mysql 数据库中设置了一个数据库和表,并且我知道我的用户、pass 和 db 信息是正确的,但是我不断收到错误无法连接到数据库"而没有更多信息.Godaddy 说我确定.使用localhost"作为主机名.我知道我应该使用 PDO 来确保安全,但现在我只想简单地连接并使用最简单的 sql 语句测试它.我的代码中是否有可能是下面的错误导致我无法连接?或者假设我的登录信息正确,下面的内容是否正常?

I set up a database and table in a godaddy mysql database, and I know I have my user, pass and db info correct, but I keep getting the error "Can't connect to database" with no further info. Godaddy said I def. use 'localhost' as the hostname. I know I should be using PDO for security, but for now I simply want to make a simply connection to and test it using the most simple sql statement. Is it possible something in my code is wrong below that is keeping me from connecting? Or does the below look fine assuming my login info is correct?

//连接数据库

$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/>";
    }
}

?>

推荐答案

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

You could have a look at this - it should help get started using a mysqli connection.

$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();
}

这篇关于无法连接到godaddy localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆