0

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?

//Connect To Database

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

?>

Taylor Jeff
  • 27
  • 1
  • 1
  • 7
  • You should try to use your database server ip address instead of "localhost" – Herry Potei Jan 21 '18 at 17:59
  • You can use `mysql_error()` to get the error message why the connect failed. What error message do you get when you use this function? – Progman Jan 21 '18 at 18:01
  • The goddady help said that for how I am set up, the only option is to use "localhost" and not an ip. the error simply says "can't connect to db" – Taylor Jeff Jan 21 '18 at 18:06
  • 1
    the `mysql` api is deprecated - what version of php is running? If it is 7+ then change to `mysqli` or `PDO` – Professor Abronsius Jan 21 '18 at 18:08
  • It is running 7.1. Wouldn't mysql still work even if deprecated? Or do you think changing to mysqli would make the connection work? – Taylor Jeff Jan 21 '18 at 18:11
  • 1
    the `mysql` api was removed from php in v7 I believe so it simply does not exist in your environment. Changing to mysqli or PDO should make things all ticketyboo again but the syntax of `mysqli` is a little different to `mysql` so, as they say, read the manual – Professor Abronsius Jan 21 '18 at 18:14
  • @TaylorJeff "can't connect to db" is not an error message you get from `mysql_error()`. What is the output you get when you replace your `... or die(...)` code with `... or die('MySQL error:'.mysql_error());`? – Progman Jan 21 '18 at 18:20
  • Check if mysql server is up and running. – Kosh Jan 21 '18 at 18:31
  • It is. But perhaps the issue is that php isn't allowed on godaddy pages and I need to get some sort of plugin to allow/parse php? – Taylor Jeff Jan 21 '18 at 18:45
  • I try mysqli and pdo solutions, and I notice that the code stops whenever it runs into a "->" in a code, such as: $conn->setAttribute – Taylor Jeff Jan 21 '18 at 18:46
  • @TaylorJeff What is the exact complete error message you get when you output the result of `mysql_error()`? – Progman Jan 21 '18 at 19:09
  • It's looking like the issue is that php isn't being parsed properly or at all in general. Back to godaddy to find out why. – Taylor Jeff Jan 21 '18 at 19:14
  • wordpress page probably needs some sort of setting to allow php – Taylor Jeff Jan 21 '18 at 19:17
  • Does 127.0.0.1 as hostname works? – harisdev Jan 21 '18 at 20:07

1 Answers1

0

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();
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46