mysqli_connect not working on shared server - php

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.

Related

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

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

Warning: mysqli_connect(): (HY000/1044): Access denied for user ''#'localhost' to database 'dbtest' [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 2 years ago.
Improve this question
When i try to connect to my sql server, it gives me the error:
Warning: mysqli_connect(): (HY000/1044): Access denied for user ''#'localhost' to database 'dbtest'.
I just created the user and gave it all permissions, but i still dosent work. Before i used another account, but i couldn't change the database perssions for it. Anybody who have a potential fix?
Here is the connect code.
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE);
// but I strongly suggest you to use PDO or MySQLi.
$DBHOST= "localhost";
$DBUSER= "testadmin";
$DBPASS= "";
$DBNAME= 'dbtest';
$conn = mysqli_connect(localhost, testadmin, $DBPASS, dbtest);
// $dbcon = mysql_select_db($DBNAME);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}
?>
I know this thread is old, but Ahmad Sharif is correct. The best way to quickly resolve this issue, especially if you're with a new hosting company, is to just create a new database using the "MySQL Database Wizard" in cPanel, then grant ALL privileges to everyone to ensure the remote access will work. Just make sure that ALL check boxes are checked while creating the new database from the "MySQL Database Wizard".
In my case grant all privileges to 'my_user' solved the problem
GRANT ALL PRIVILEGES ON *.* TO 'my_user'#'%' ;
You can check the following link for further information
https://mariadb.com/kb/en/grant/
There is no host, no user and no db-name in your mysqli_connect call. Do:
$conn = mysqli_connect($DBHOST, $DBUSER, $DBPASS, $DBNAME);
or
$conn = mysqli_connect('localhost', 'testadmin', $DBPASS, 'dbtest');
And if you wouldn't suppress E_NOTICEs, you'd see that php is looking for undefined constants.
Please check that you check all grant privileges. If you do not then delete the previous DB and create a new Db from Mysql Database Wizard (I guess it is shared hosting). In the last step, you can check all the privileges.
I have got the same error and I could solve my problem by grant all privileges.
I don't know if anyone had a solution to this I have just been trying to create a separate table for a list of emails with a same key column. I got exactly the same error message but solved it by writing a php file which i can run on the server.... The php file creates the table ! And it had permissions to let me write to it as well !
Here is the file i used to let the server make the table i needed. And let me access it from my other php files. I will call it here "LetServerMakeTable.php" ha! When i uploaded it to my 'ricky' heliohost server i ran it on the browser thus:
http://virowiz.heliohost.org/LetServerMakeTable.php
Result:... I had a php file which collecter email addresses and a used ID code and the wrote it successfully to my new Emails table within my database.
<html>
<head>
<?php
$conn = mysqli_connect("localhost", "virowiz_Kevin", "password", "virowiz_Covid3a");
// Check connection
if (!$conn) //<---- ! conn = NOT conn
{
echo "Failed";
die("Connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
//---------------------------------- Create the table.
$sql = "CREATE TABLE Emails
(
CVTRn BIGINT(12),
Email char(254),
Mobile char(13)
)";
//----------------------------------
mysqli_error($conn);
if (mysqli_query($conn, $sql))
{
echo "Table created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
</head>
</body>
</html>

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');

Having Trouble Connecting to MySQL Database

The following code successfully connects me to my mySQL database.
$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");
I have been experimenting on localhost using XAMPP and phpmyadmin, and everything works correctly. However, today I uploaded my website to my domain (I have bought a domain and web hosting through GoDaddy) and I keep getting "Failed to Connect!" whenever this code runs. The HTML/CSS work correctly, but I cannot connect to mySQL. How can I connect to my database on the server?
You'll need to change your connection information here:
mysql_connect("localhost", "root", "")
to include your GoDaddy database details instead. Contact GoDaddy for more information on what to use for your account.
You have not mentioned anything about MySQL database hosting.
If you are hosting the database also in godaddy, you should be able to get the connection string and host name from information mentioned here
First, You need to create database in your hosting server (phpmyadmin cpanel) and supply details in your database connect file.
**Looking at your database credentials I can say that you haven't created one yet since
the username is definitely not 'root' and password will be required.**
Sample db_connect file:
<?php
$hostname = 'e.g: internal-ra.db-server-123';
$username = 'e.g: serverone1234d4';
$password = 'e.g: your_own_password';
try {
$pdo = new PDO("mysql:host=$hostname;dbname=database_name_here", $username, $password);
/*** echo a message saying we have connected ***/
}
catch(PDOException $e){
echo 'Exception -> ';
var_dump($e->getMessage());
}
?>
And try to use so less as possible the key DIE()

Website - Connecting to MySQL database

I am aware this is a stupid question, but to connect to a database that is on my local server, do i connect via this code:
mysql_connect("localhost","user","pass")
so using localhost connection. This seems to make sense, as it is sending a message to the host computer to connect to it's local database.
or do i use this piece of code:
mysql_connect("Ipaddress","user","pass")
This also makes sense to me somehow. Which one do i use for my website which i am hosting at home.
EDIT: obviously I want it so that people from all over the world enter information, and it is sent to a database. It isn't meant to be used by me.
A better way to do this is:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
};
?>
This is what I personally use, there are other ways to do this.
You should use localhost:
You could something like this:
<?php
$mysql = mysql_connect("localhost", "user", "pass");
if(!mysql){
die('Could not connect: ' . mysql_error());
}
//code her
mysql_close(mysql) //stops the connection
?>
Your database will most likely be stored on the same server as your program, you should put localhost.
Anyway, take some time to learn about PDO. It's more secure, cleaner and actually easier to use.

Categories