CPanel Database connection error - php

I recently switched to CPanel, and ever since then, I've had issues. I'm having an issue connecting to a database. I created it in CPanel, with the hypothetical username 'root' and password '123'. I have a file /scripts/dbh.php and a file /index.php. Here are the contents.
dbh.php:
<?php
// Database Handler
$db_name = 'hughchalmers';
$servername = "localhost";
$username = "root";
$password = "123";
$table_name = 'accounts';
$conn = mysqli_connect($servername, $username, $password, $db_name) or die('Cannot connect to database. Contact an admin');
?>
(hughchalmers is a database, accounts is a table)
index.php:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/memedictionary/scripts/header.php');
include($_SERVER['DOCUMENT_ROOT']. '/memedictionary/scripts/dbh.php')
?>
<head>
<title>Login</title>
</head>
<body>
</body>
What's the issue here? It just dies with that message I set (Cannot connect to database. Contact an admin). Apologies, I'm very new to MySQL and CPanel

Check your last parameter for mysqli_connect(). Looks like you're referring to a table when instead it must be the database name or is it just a variable name but you're passing a database name actually?

Related

How to link up a database in a HTML base website in offline server (Xamp)?

I have created a website for my versity project...But I am asked to add a database in it..That's why I need to know how to add a database via xamp in a website.
Use this code to create a connection to your database from php
<?php
$servername = "localhost";
$username = "root";
$password ="db-password";
$dbname = "db-name";
//create connection
$conn = new mysqli($servername,$username,$password,$dbname);
//check connection
if ($conn->connect_error) {
die("connection failed" .$conn->$connect_error);
}
?>
use this link to understand how to create and execute queries.
https://www.w3schools.com/php/php_mysql_connect.asp

What user and password in phpmyadmin will I use in cpanel?

This is my very first time uploading a website. now I'm having a problem with my hosting which is CPANEL.
I'm using the code
<?php
date_default_timezone_set("Asia/Manila");
$servername = "localhost";
$username = "root";
$password = "";
$db = "exampledb";
// Create connection
$conn = new mysqli($servername, $username, $password,$db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
for my database connection.
Now my problem is what code should I put in servername? instead of localhost?
and same to the username root and password?
On the hosting site in cpanel they normally give you the server name that you can use, the password you will need to create a db user also on cpanel, this will be your user name and password.
// localhost is fine or else you need to keep your server IP.you can find in cpanel.
$servername = "localhost";
// if you are using database in some other server you should provide IP
// you need to create a user with required privileges then need to provide username/password
// root also fine but better create a user
$username = "root";
$password = "";
// Here you need to mention which DB you want use for your application.
$db = "exampledb";

Cannot change php from localhost details to my servers to connect sql database

I am new to php/sql and i have a login system which runs on my local host using xammp and it all works fine. I now want to upload it to my website but the code no longer works... I have created a sql db on my hosting service and tried to change the code.
the code that is used on the local host is
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "loginsystem";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
and this is the code that i have got from my hosting.
<?php
$host_name = 'db682827654.db.1and1.com';
$database = 'db682827654';
$user_name = 'dbo682827654';
$password = '<Enter your password here.>';
$conn = mysql_connect($host_name, $user_name, $password, $database);
if (mysql_errno()) {
die('<p>Failed to connect to MySQL: '.mysql_error().'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p >';
}
?>
however this brings up an errror message. I have changed the password to the password for my database but its still not connecting.
This is the error message.
Failed to connect to MySQL: Access denied for user 'dbo706265806'#'217.160.62.78' (using password: YES)
Any help would be greatly appreciated
use mysqli_connect (not mysql_connect) like localhost:
<?php
$dbServername = "db682827654.db.1and1.com";
$dbUsername = "dbo682827654";
$dbPassword = "<Enter your password here.=)>";
$dbName = "db682827654";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>
On your computer you are using mysqli_connect, but on the server you are trying to use mysql_connect. Just use the same file from your computer and simply change $dbServername, $dbUsername, $dbPassword and $dbName to match those that your hosting provided.

How to connect to phpmyadmin database?

I'm trying to connect to a database in phpmyadmin. I'm new to the process and unsure how to connect to it.I'm using godaddy to host. I have this line of code:
$db = mysqli_connect("https://.....secureserver.net", "...", "....", "authenticationdb_");
I went to the table on phpmyadmin and copied the url, then copied the username (the first "...") and password (the second "...") that I used to login in to godaddy, and the name of the database is authenticationdb_.
This is not working and I'm not sure why. I was unsure if the username and password were the ones that I used to login in to godaddy but I don't know what else they would be since i accessed phpmyadmin thru godaddy.
<?php
$servername = "localhost";
$username = "username"; //your user name for php my admin if in local most probaly it will be "root"
$password = ""; //password probably it will be empty
$databasename = ""; //Your db nane
// Create connection
$conn = new mysqli($servername, $username, $password,$databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Interact with webmatrix database using PHP

I am having trouble using my Webmatrix database. I would like to insert data into the database with a registration page written in PHP.
When I created my database the "web.config" file was created. How can I use the connectionString from the "web.config" file in my PHP to be able to connect to the database?
Connection string from my web.config file:
<connectionStrings>
<add connectionString="server=localhost;uid=root;database=sitedetest;Pwd=123" name="sitedetest" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
I tried this:
config.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '123';
$dbname = 'sitedetest';
?>
In the registration page:
include 'config.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
or die ('Error connecting to mysql');
mysql_select_db($dbname);
But when I try using the registration form, I always get the 'Error connecting to mysql' message
Thank you for your time

Categories