Can't connect to database with php? [duplicate] - php

This question already has answers here:
How to make mysqli connect function?
(2 answers)
Closed 5 years ago.
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!

There are several reasons for getting 500 error on page and will discuss all of them below.
You have used mysql_* function to establish connection to database which is deprecated in newer version of php.
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

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";
}
?>

Related

Cant get php to connect to sql. Get error Connection failed: php_network_getaddresses: getaddrinfo failed: No such host is known.

I have scoured google, and stackover flow, and just cant get to the bottom of this issue. I cannot get the following php code to connect to SQL. Its a simple php web document, that i am using to test out some things. SQL is sqlexpress 2016, and its running on IIS with php 7.x installed. PHP code executes fine, so its something with the code or the database is my guess. Things I've tried:
I've ran an echo in php to resolve the name, and it resolves it fine.
I've connected from a separate server to the sql server using tcp, and it connects fine.
I've tried both PDO connection, and mysqli and both come back with same error.
The PDO code ive used is:
<?php
$servername = 'RemoteServerName\SqlInstance';
$username = 'iislogon';
$password = 'password';
try {
$conn = new PDO("mysql:host=$servername;dbname=netdata", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
The mysqli code is:
<?php
$servername = 'RemoteServerName\SqlInstance';
$username = 'iislogin';
$password = 'password';
$dbname = 'netdata';
?>
<?php $conn = new mysqli($servername, $username, $password, $dbname); ?>
<?php
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";?>
Both return the same error of host not found. What other issues could be causing this? Im new to coding php so any help would be appreciated.
mysqli and PDO starting with mysql: are supposed to connect to MySQL, not SQLExpress.
If you want to use SQLExpress you should use something like sqlsrv_connect or adjust your pdo string to a SQLExpress compatible one.
Take a look at this thread too.
Look at this description http://php.net/manual/en/pdo.construct.php and specifically:
In general, a DSN consists of the PDO driver name, followed by a colon, followed by the PDO driver-specific connection syntax. Further information is available from the PDO driver-specific documentation.
Are you sure your dsn is correct and you have the PHP module enabled? See http://php.net/manual/en/ref.pdo-mysql.php
I think you didn't escape your backslash with another backslash. Try this:
<?php
$servername = 'RemoteServerName\\SqlInstance';
?>

mysqli_connect not working on shared server

Beginner question here. I am learning php and mysql. I have a website that I created using a local server. The database was linked to it fine on MAMP. I am now trying to upload it to a shared server using iPage. It will not connect.
Here is my code
<?php
$connect = mysqli_connect('host','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
I know the user, the password and the database name are correct. My only doubt is regarding the host. My website is in a subdomain. Should my code be:
<?php
$connect = mysqli_connect('subdomainName.domainName','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
or
<?php
$connect = mysqli_connect('domainName','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
or even
<?php
$connect = mysqli_connect('localhost','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
Do I need to do anything in my hosting page to link the database in any way. I have tried all options and nothing works. Thanks
Before implementing code you have to make new database in your website server,for this you have to log in to cpanel and make db in it, use that details and try the third one and use mysqli_connect_error() for getting the error details. like below.
Note: Better to use double quotes here because of string.
// Create connection
$conn = mysqli_connect("localhost", "username", "password");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if you are getting Connection failed: no such file or directory. then refer another question PHP - MySQL connection not working: 2002 No such file or directory
Better to contact your hosting provider if you are in shared hosting.

Can't connect my database on my live server

Hello I've been making a site on my local server and I finished it so I'm moving everything over to my live server. I've made a database in phpmyadmin on it and I would like to connect to it. I feel like I have the wrong inputs though because it gives me this error.
This is my code for my database connection.
<?php
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'user_data');
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error);
}
I didn't actually enter password I just think I shouldn't show it anyway I think I just have something mixed up since I'm new at this.
Oh and the database is located within a database grouping should i input the path to it?
You need to tell it to connect to gener105_user_data instead of just user_data
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'gener105_user_data');

Blank page when trying to run my php code to connect to my phpmyadmin

I have a database on phpmyadmin with a table that contains some information. I am trying to check the connection to it by writing this code:
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
but it just shows me a blank page when I'm running it in browser. I named it conn.php and I'm running it with localhost/conn.php, but nothing happens. What am I doing wrong?
Check logs in /var/log/apache/error.log to see what is wrong.
Have you enabled mysqli module? It can be a reason.
You can add error_reporting(E_ALL); to check for all errors you encounter. This might also give information about why you see empty page. Good luck.

PHP error finding database when trying to connect to it with mysqli_connect()

I am reviewing my old work and have started to change the mysql syntax to mysqli however I am having issues trying to connect to my database. It throws the following error: No database selected.
This is my code:
$con = mysqli_connect("localhost","root","password","db1");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Your assistance would be greatly appreciated.
Your code is correct, probably you are giving a database name that is wrong, check if that database actually exists.

Categories