0

I'm trying to connect to my database via php but I keep getting a 500 Internal Server Error. It is hosted on GoDaddy. I'm fairly new to php. Here is my code:

<?php
// Create connection
$conn = mysql_connect('server name', 'username', 'password');
mysql_select_db('users', $conn);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo "Connected successfully";
}
?>

Obviously I have entered my server name, username and password but not showing them here. I can't get it to connect at all. I wanted to be able to just to display a row of data on a page to check it works but I can't even get that far yet! I'm probably missing something obvious. Any help would be great. Thanks!

  • **obligatory note** mysql_* functions are deprecated as of PHP 5.5.0, and removed as of PHP 7.0.0. Switch your code to use [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) or [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – aynber Feb 01 '18 at 16:45
  • 2
    The defunct `mysql_` extension doesn't support OOP syntax, e.g. `$conn->connect_error` you probably want `$conn = new mysqli(...);` (note the **i**) and `$conn->select_db(...);` – CD001 Feb 01 '18 at 16:46
  • @CD001 Why don't you add this as an answer? – Mcload Feb 01 '18 at 16:50
  • Can you edit your question with your PHP version? – Mcload Feb 01 '18 at 16:51
  • @Mcload - because I'm fairly sure this is a duplicate question, I just can't be bothered to find it :) – CD001 Feb 01 '18 at 16:53

2 Answers2

1

There are several reasons for getting 500 error on page and will discuss all of them below.

  1. You have used mysql_* function to establish connection to database which is deprecated in newer version of php.
  2. You may have issue with .htaccess code which is blocking you.

However you can check some points to figure out your issue.

Check your .htaccess file whether it is blocking you or not Check your php version (If you php version is >= 5.5) then try below.

<?php
// Create connection
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_close($con);

Hope this will help you!!

Thanks & Regards

Shishil Patel
  • 3,449
  • 2
  • 12
  • 16
  • I am no longer getting an error message, thank you! I have added "else { echo "Connected successfully"; } and I am getting that message. Thank you! I've tried to retrieve data from my database and echo it onto my screen but I receive the error again when I do so I'm typing something wrong. –  Feb 01 '18 at 20:43
  • $result=mysqli_query("SELECT * FROM users"); while($row=mysqli_fetch_array($result)) { echo $row['id'].' '.$row['firstname'].' '.$row['surname'].'
    '; }
    –  Feb 01 '18 at 20:44
0

you seems to be use the mysql as an object and it is not supported only mysqli that can behave like this . so if you need to use mysql extension and it is not recommend since it is deprecated.

<?php
// Create connection
$conn = mysql_connect('server name', 'username', 'password');
mysql_select_db('users', $conn);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysql_error());
} else {
    echo "Connected successfully";
}
?>
samehanwar
  • 3,280
  • 2
  • 23
  • 26