Internal server error while connecting to my database online - php

I'm using the following code to connect to a database that I have setup on godaddy.com. But when I try to run the script I get "Internal Server Error".
<?php
$db_conx = mysqli_connnect('MY_WEBSITE.secureserver.net', 'MY_USERNAME',
'MY_PASSWORD', 'DATABASE_NAME');
// Evaluate the connection
if (mysqli_connect_errno()) {
echo "cannot connect to database";
echo mysqli_connect_error();
exit();
}
else {
echo "Successful database connection, happy coding!!!";
}
?>
I'm not sure what i'm doing wrong

Related

Connection failed: No such file or directory (IONOS) [duplicate]

I am working on a payroll system. Everything is working fine on localhost (xampp) but when I upload it to ionos hosting, it is loading the admin login page fine but when i enter the username & password it is giving "Connection failed: No such file or directory".
DB name: dbs4868780
and here is the code:
<?php
$conn = new mysqli('localhost', 'root', '', 'xxxxxxxxx');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
EDIT:
I tried it but it is still giving the same error.
I also tried to connect DB with PHP code provided by ionos but it is still giving "Connection failed. File or Directory not found"
Here is the php code from ionos:
<?php
$host_name = 'db5005797255.hosting-data.io';
$database = 'dbs4868780';
$user_name = 'xxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxx';
$link = new mysqli($host_name, $user_name, $password, $database);
if ($link->connect_error) {
die('<p>Failed to connect to MySQL: '. $link->connect_error .'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p>';
}
?>
See attached fake database example to get your data

Connection refused when I try to connect via php to mysql database over android app

I have a mysql database running on my local machine and I use wamp. I can login with phpmyadmin. Everything looks fine, create tables and users.
When I try to connect with php I get the message connection refused. The same when I try 127.0.0.1/connect.php in my browser. If I have a wrong user in my php file I get the message that user ist wrong.
Here my connect.php
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "users");
if ($conn) {
die('no connection: '. mysqli_connect_error());
}
echo 'connected';
mysqli_close($conn);
?
I tried everything, localhost, IP, 10.0.2.2, but nothing works. I also checked mysql settings and I read most of the posts here.
I solved the problem.
With this code it works perfect
<?php
$conn = new mysqli("127.0.0.1", "root", "", "users");
if (mysqli_connect_errno()) {
echo "no connection " . mysqli_connect_error();
}
echo 'conneted';
mysqli_close($conn);
?>

simple test php with postgresql database

I am using this php to test if I am connected in a postgree database, works very well, but how can I insert a error Message and a Message showing the database is connected and not connected?
Example:
like: You are connect to:database_name
or:
You could not connect to:database_name
That is my code:
<?php
$connection = pg_connect ("host=localhost dbname=site user=postgres password=root");
?>
Just test the truthiness of the connection:
<?php
$connection = pg_connect ("host=localhost dbname=site user=postgres password=root");
if($connection) {
echo 'connected';
} else {
echo 'there has been an error connecting';
}
?>
Return value of pg_connect() is
PostgreSQL connection resource on success, FALSE on failure.
so check this value:
if (!$connection = pg_connect ("host=localhost dbname=site user=postgres password=root")) {
$error = error_get_last();
echo "Connection failed. Error was: ". $error['message']. "\n";
} else {
echo "Connection succesful.\n";
}

cannot get response from the function mysqli_connect()

I'm trying to connect php with mysql so I write the code like this:
<?php
// Create connection
echo "1";
$con=mysqli_connect("localhost","root","pwd");
echo "2";
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
but i dont get any response, there is only a "1" on the page. I'm sure that the uname and pwd is correct. Any advices?
Use mysqli_connect function instead of mysql_connect,
<?php
// Create connection
$con=mysqli_connect("example.com","root","pwd","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Seems like the mysqli extension not enabled in your php.ini configuration.
After enabling the extension restart the Apache server and try again the running the code.

Connecting PHP TO MYSQL ERROR message

When connecting to localhost with this code, I get an error message. I'm unable to connect to MYSQL and I'm not sure why:
<?php
//connect
$link = mysqli_connect("$localhost", "$root", "$password", "$database");
if (!$link)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}
if (!mysqli_set_charset($link, 'utf8'))
$output = 'Unable to set database connection encoding.';
include 'output.html.php';
exit();
}
if (!mysqli_select_db($link, '$database'))
{
$output = 'Unable to locate the Teamster database.';
include 'output.html.php';
exit();
}
$output = 'Database connection established. ';
include 'output.html.php';
?>
I get this error message. This is what populates when I refresh the page, above a form that I've created. Is this a problem of connectivity or coding?
//check connection //if (mysqli_connect_errno($con)) // { echo "Failed to connect to MySQL: " . mysql_connect_error(); // } //
if you are getting:
//check connection //if (mysqli_connect_errno($con)) // { echo "Failed to connect to MySQL: " . mysql_connect_error(); // } //
then this is your problem:
$link = mysqli_connect("$localhost", "$root", "$password", "$database");
where loclhost, username, password, database combination is not correct.
also make sure you are on the right port and that you are allowed to connect with those credential.
A "cannot connect to db" is always due to some combination of that line and not because of code since you failed on if (!$link)
I (don't think I can) post comments, but it would help if you posted the exact error message.
eg the output of
mysql_connect_error()
From what I can tell though, you should make sure that the $localhost is set to something (probably "localhost", and that you're using the correct username and password.
never output a fixed error message, when the system could TELL you what's wrong:
$output = 'Unable to connect to the database server:' . mysqli_connect_error();

Categories