can't connect databse in my cpanel - php

i already purchase a web hosting and a domaine to my portfolio,but the connection with the contact form and the database is not set on in my Cpanel,i m trying to test the connection by simple file .
i don t know whats the problem i guess "mysqli" is not set on ,i checked the connection information (user,pass...) they are correct,any suggestions ?
Sponsor hosting : namecheap.com
<?php
$host = 'localhost';
$user = 'user1';
$pass = 'pass1';
$db = 'db1';
$con = mysqli_connect($host, $user, $pass,$db);
if($con)
{
print 'CONNECTED';
}
else
{
print 'NOT CONNECTED';
}
?>

to debug and check what happened , use this :
else
{
print 'NOT CONNECTED';
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
instead of :
else
{
print 'NOT CONNECTED';
}

Related

Fatal error: Uncaught mysqli_sql_exception: Unknown database 'test_db'. I keep getting this error even though I programatically created the database

Here is the code
<?php
$servername = "localhost";
$usrname = "root";
$pwd = "";
$db = "test_db";
// connect to the database
$conn= mysqli_connect($servername, $usrname, $pwd,$db);
if (!$conn){
die('connection failed' . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS test_db";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
?>
I am not sure what is going on as I am sure i made no errors while typing. As it keeps showing me that there is an unknown database despite the fact i made a CREATE DATABASE statement.
I do not know if there is something else i need to do but by all measures the code should work.
It is supposed to echo the "Database created successfully" or the error message.
`<?php
$servername = "localhost";
$usrname = "root";
$pwd = "";
//$db = "test_db";
// connect to the database
$conn= mysqli_connect($servername, $usrname, $pwd);
if (!$conn){
die('connection failed' . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS test_db";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
?>`
Your code is fine, You just have to remove $db from mysqli_connect().
Because you are requesting to connect "test_db" to this database which already not exists.
<?php
// Connect to MySQL
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make my_db the current database
$db_selected = mysql_select_db('my_db', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
Reference
How do I create a database if it doesn't exist, using PHP?
$conn= mysqli_connect($servername, $usrname, $pwd,$db);
You are trying to connect to a database that did not initially exist, so you get a fatal error.
Try creating new database using try catch blocks
try{
$conn= mysqli_connect($servername, $usrname, $pwd,$db);
}cath(Exception $e){
//your code
}

PHP Error "mysqli::mysqli(): (HY000/2002)" [duplicate]

This question already has answers here:
mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)
(5 answers)
Closed 3 years ago.
I'm having a problem running php code. The error only appears for this php file, other simple ones work fine. I'm using xampp. apache and mysql running green.
<?php
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "test";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '. mysqli_connect_error());
}
else{
$sql = "INSERT INTO account (username, password)
values ('$username','$password')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ." ". $conn->error;
}
$conn->close();
}
}
else{
echo "Password should not be empty";
die();
}
}
else{
echo "Username should not be empty";
die();
}
?>
Finally got the solution to this problem, I'm sharing it because I can't find a solution anywhere else. The $host should be linked to "127.0.0.1" but for an unknown reason my host only worked when it was "127.0.0.1:3307" or the name of your port for mysql at the end, don't forget the ":". PS I changed my db connection back to using the procedural way despite what the Ashu said and it worked.
When you are using Object-oriented mysqli connection then you have to use all Object-oriented method.
In below code you used procedural way:
if (mysqli_connect_error)
{
die('Connect Error ('. mysqli_connect_errno.') '. mysqli_connect_error);
}
Replace with :
if ($conn->connect_error)
{
die('Connect Error ('. $conn->connect_errno.') '. $conn->connect_error);
}
Use 127.0.0.1 instead of localhost

How to connect to heroku postgres database?

I'm trying to connect to postgres databse on heroku but it doesn't work for me.
This is the code in index.php
<?php
$host = "my host";
$dbname = "my database name";
$user = "my username";
$password = "my password";
$port = "5432";
$dsn = "pgsql:host=$host;dbname=$dbname;user=$user;port=$port;password=$password";
$db = new PDO($dsn);
if($db){
echo "Connected <br />".$db;
}else {
echo "Not connected";
}
?>
But nothing appear on the screen it should print not connected if the credentials are wrong.
Help Please
Thanks all
You need to get your DATABASE_URL variable. You can get it going here and press 'View Credentials'. Then fill your DB connection with the info provided.
Regards
Try next code, it works with me.
<?php
$con = "dbname=fgsfg10pdq host=ghfghfh4654.amazonaws.com port=5432 user=gafasduyiu password=435346af8493196 sslmode=require";
if (!$con)
{
echo "Database connection failed.";
}
else
{
echo "Database connection success.";
}
?>

Can't connect to MySQL server using PHP

I have MySQL server running on my local computer. I am trying to connect to it
through PHP from my website host. I get the following error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2).
Here is the php I am running with the userid and password wiped out for security:
if ($dbc = #mysql_connect('localhost', 'userid', 'password')) {
print '<p>Successfully connected to MySQL!</p>';
// Try to create the database:
if (#mysql_query('CREATE DATABASE myblog',$dbc)) {
print '<p>The database has been created!</p>';
} else { // Could not create it.
print '<p style="color: red;">Could not create the database because:<br />' . mysql_error() . '.</p>';
}
// Try to select the database:
if ($x == #mysql_select_db('myblog,$dbc')) {
print '<p>The database has been selected.</p>';
} else {
print '<p style="color: red;">Could not select the database because:<br />' . mysql_error() . '.</p>';
}
mysql_close(); // Close the connection.
} else {
print '<p style="color: red;">Could not connect to MySQL:<br />' . mysql_error() . '.</p>';
Can someone help me with this connection?
It seems that your code should work unless your MySQL server is not properly configured... but you may try the MySQLi Route.
$servername = "computername";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

mysql_connect error message

The mysql error message "Database connection cannot be established" is intermittently showing whenever I call the config file in my page. What seems to be the problem in my code?
///-- config.php --////
$hostname = "localhost";
$database = "db";
$db_user = "root";
$db_pass = "pass";
session_start();
try {
$conn = mysql_connect($hostname,$db_user,$db_pass) or die("Database connection cannot be established.");
if(!$conn)
{
$_SESSION['errormsg'] = "Connection failed.";
exit();
}
else
{
mysql_select_db($database) or die(mysql_error());
}
} catch (Exception $e) {
echo "Exception: ", $e->getMessage(), "\n";
}
die("Database connection cannot be established."); // big deal?
Only tells you what you already know.
die(mysql_error());
Tells you what you need to know.
P.S: intermittently and whenever are contradictory, please update your question to remove the confusion and stress on one.

Categories