0

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

Paul
  • 1,277
  • 5
  • 28
  • 56

1 Answers1

0

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.

Shibin Raju Mathew
  • 920
  • 4
  • 18
  • 41
  • Hi, thanks for this. I have done it and it comes back with: Connection failed: not such file or directory. Is there anything that I need to do in the control panel of my hosting provider (I am using iPage) to connect the database in any way? Also, another thing that is puzzling me is that when I had it on mamp, there were four arguments to the function, the fourth being the name of the database. Thanks – Paul Nov 11 '17 at 14:31
  • connection failed:no such file error only can resolve by your hosting company so please contact them. – Shibin Raju Mathew Nov 12 '17 at 19:36