Getting 500 Error when trying to access localhost my_sql database - php

I have a small page that I'm trying to building a journal with using PHP and My_SQL for both practice and recreation. I have a solid understanding of PHP but less so on My_SQL. Based on what I viewed it seemed that I can implement it almost like looking up a function and applying data to the corresponding areas. I tried doing the most basic thing and connect to my localhost server, but I cannot figure out why I am getting a 500 error. What is wrong with my server???
$servername = "http://127.0.0.1/";
$username = "root";
$password = "***";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else echo "Connected successfully";
`

Related

How can I bypass error 111 MySQL in PHP connection?

I have bought a database MySQL to use on my domain/host.
I can connect to the database via wampserver (localhost) without problems.
But when I am trying to connect to the host website it gives me an error.
Connection failed: Can't connect to MySQL server on 'mysql-******.net' (111 "Connection refused")
My code:
<?php
$servername = "mysql-*********.net";
$username = "username8954";
$password = "dolphyn1235";
$dbname = "animals";
$dbServerPort = "26313";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $dbServerPort);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Any ideas how to solve this?
Check in your MySQL Server configuration files (my.cnf), find bind-address = 127.0.0.1 then comment them with # at the begining of the line. and then restar your MySQL.

PHP connecting to database fails without error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to connect to databse on my server by using this code:
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But code after $conn = new mysqli($servername, $username, $password); is not executing. When i try to echo anything after it I do not get output. Code before that line works as expected. I do not get any errors from php.
I am not sure what is problem. Could it be something server related? I did try to add:
ini_set('display_errors',1);
error_reporting(E_ALL);
but it didn't help, no errors have been displayed.
I have been trying many things from stackoverflow (and other places) but they didnt help, some examples:
Connecting to a mysql database from php, error but no error shown?
Connection of MySQL with PHP not working
There are multiple ways to handle errors
Simplest of all, use try catch while connecting
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
try {
$conn = new mysqli($servername, $username, $password); } catch(\Exception $e) { var_dump ('oopss... this is the error', $e)}
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected

How to fix error in dbconn file when upload php mysql project to free webhosting site?

I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1

Error 500 when I try to connect MySql with php's mysqli

I have written below php code to connect MySQL DB:
<?php
// Connect to MySQL
$servername = "localhost";
$username = "user";
$password = "A11b22c33&";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
However this code fails to make any connection. All I am getting is below error:
currently unable to handle this request.
HTTP ERROR 500
However if I remove the connection portion of my code then my php file working perfectly, i.e. I am getting echo as 'Connected successfully'
<?php
// Connect to MySQL
$servername = "localhost";
$username = "user";
$password = "A11b22c33&";
echo "Connected successfully";
?>
Can somebody help me to understand what went wrong? I have checked username and password, and they are perfect
I just installed mysqlnd using apt-get install php-mysqlnd, and it works fine.

Need help connecting PHP to MySQL database

I am building a website for a friend and he would like some statistics on the website. The statistics are on a dedicated MySQL server, and the website is on another. I have been trying to keep linking the database to the website but it keeps failing. I am using PHP to attempt this.
The code I have been trying to use is here:
<?php
$servername = "ns303998.ip-x-x-x.eu";
$username = "x";
$password = "x";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
The database name is the issue. Any ideas about this?
mysqli_connect takes 4 arguments, not 3 as you are trying. The fourth one is database name. Documentation is here.
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
Edit: The argument is optional, but if it's is a shared hosting, your credentials are probably working for the one specific database, not the whole server. So specifying the database could help you with this issue.
mysqli_connect needs database name to execute
<?php
$servername = "ns303998.ip-x-x-x.eu";
$username = "x";
$password = "x";
$dbname ="x";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
use
$dbname='yourdbname';
mysqli_select_db($conn,$dbname);
to select the database

Categories