Moving MySQL from localhost to remote server - php

I’ve just finished writing a website which is working very well on my local server (xampp).
The following is my connection database definitions for the local server:
<?php
$host="localhost";
$username="root";
$password="1234";
$db_name="partnership";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
I’ve loaded my files successfully and put them on the remote server (www.mydomain.com) and I’ve created the relevant database ‘partnership’ (successfully imported all Tables from phpMySQL local server to the remote server).
I can view the website (www.mydomain.com) but with no connection to the database (MySQL).
The error message is of course "cannot connect".
I’ve changed $host="localhost" to $host=”www.mydomain.com” but still getting the same error message.
Any assistance on the above issue would be greatly appreciated.

Shouldn't you still use localhost as $host if you're trying to connect via the application? The script is on the same server as the DB.

If you are using cPanel, when you create the MySQL table, make sure you assign your user to it (trivial, I know, but Ive forgotten about it a few times myself.)
If your script is running from your remote server, just stick with localhost. Will work fine.
Last, my only suggestion until a better answer is said, try using
$host = 'localhost';
$user = 'username'; // Im pretty sure your username isn't root.
$pass = '1234';
$db = 'partnership';
/* Alot of hosts like to append your cPanel login to your db and username fields.
Check to see what your table is. It might actually be 'youruser_partnership' */
$mysqli = new mysqli($host, $user , $pass, $db);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
If your host supports mysqli (most do). If not, when you do your die statement, still use die(mysql_error()); to get the exact error. It will more than likely be your username/password you created and assigned to your database

Ah, this is always the most annoying part of the process. Try this and see if it works:
$host="127.0.0.1"; // "localhost" should work, but don't count on it
$username="user"; // username you use to log into phpMyAdmin
$password="password"; // password you use to log into phpMyAdmin
$db_name="dbname"; // database you want to connect to
You'll need to have created this user/password pair and given them the appropriate permissions to access the database. Also note that the mysql PHP functions are deprecated and should not be used. PDO or mysqli are the preferred ones going forward.

Related

Cannot connect to MYSQL in MAMP using PHP

I just installed MAMP and have created a MYSQL database. I can access it via PHPMYADMIN.
In my php page I have this, pasted directly from the MAMP webstart page--
$user = 'root';
$password = 'root';
$db = 'local_db';
$host = 'localhost';
$port = 3306;
$link = mysql_connect(
"$host:$port",
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
The resulting page stops at this point, won't print anything below these instructions.
I've tried changing the port in the MAMP preferences. I also included or die("Could not connect"); after the first line, but still don't get any text after the link data in the page.
I checked online, and others with the problem at least see the die text. I don't get that.
I haven't changed any passwords or data other than mess with the port number.
Any help would be appreciated!
Please give the following a try, I have developed and tested it locally, functionality within has been documented to help you understand what is going on in every step.
/**
*
* Modern method of connecting to a MySQL database and keeping it simple.
*
* If you would like to learn more about PDO,
* please visit http://php.net/manual/en/book.pdo.php
*
*/
//Set up database connection constants, so they cannot be changed.
define('DBHOST','127.0.0.1'); //Change this to the ip address of your database
define('DBNAME','test'); // Change this to the database name you are trying to connect to.
define('DBUSER','databaseuser'); // Insure this user is not the root user!!!!
define('DBPASS','databasepassword'); // Insure this is not the root password!!!!
//Let's try to connect to the database first.
try {
//Initiate a new PDO object called $MYDB and pass it the proper information to make
//the connection
$MYDB = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME."", DBUSER, DBPASS);
//If we are successful show it :D for the test page, if this is for production you should not show this.
echo "Database connection was successful.";
//If this does not worth catch the exception thrown by PDO so we can use it.
} catch(PDOException $e) {
//Show that there was an issue connecting to the database. Do not be specific because,
//user's do not need to know the specific error that is causing a problem for security
//reasons.
echo "Oh, sorry there was an issue with your request please try again.";
//Since we had an issue connecting to the database we should log it, so we can review it.
error_log("Database Error" . $e->getMessage());
}
//Since this is 100% php code we do not need to add a closing php tag
//Visit http://php.net/manual/en/language.basic-syntax.phptags.php for more information.
If you have any issues with this please attempt to break it up into smaller pieces while reviewing the PDO documentation.

Database connection phpmyadmin

I'm working for an e-commerce that has the db on phpmyadmin. In another website I'd like to connect to that database. I have password, username and db name, so I'm using this "connection string":
<?php
$nomehost = "localhost";
$nomeuser = "user";
$password = "pass";
// connection
$conn=mysql_connect($nomehost,$nomeuser,$password);
if (!$conn) exit ("Error connection<br>");
// db name
if (!mysql_select_db("db_name",$conn)) exit("Error db name<br>");
?>
The result is "Error db name". What can I do? Have I to set some oprion in the phpmyadmin?
First of all:
this error is caused by the fact that you are selecting the wrong database in your MySql server. Is your db called db_name???
EDIT: based on the comments you are making: is the server that hosts the php page the same as the mysql server?
Then:
phpmyadmin is just a tool to connect and handle MySql databases and is not a database server itself.
Last but most important:
you are using a deprecated library (mysql) in php to connect to a MySql server. Please consider moving to mysqli or better to PDO

connecting a mysql database using php to a host

Okay I'm having trouble connecting my database to my host..
Im using myPHPAdmin and I made a database on my host
the problem is in my php code where I define my connection to the host
everything worked fine when I did this on the localhost.
But now that I want to use it on my personal domain it wont work
it doesnt access the data in the database.
Im new at this so dont shoot me ;)
$connect = mysql_connect("host", "user", "pass");
so for my host/domain I enter my domain the-bloodgod.com ?
what do I enter for user and pass? is it just the login I normally use to access the Cpanel? Or do I have to create special permissions in myphpadmin?
also on my hosts myphpadmin it shows that it stores all created tables in the_bloodgod_com database collection
so would this be correct if I put it the code bellow?
mysql_select_db("the_bloodgod_com");
$sql="SELECT * FROM tablename";
MOST hosting companies you have to use "localhost" as the host name for your database connection code. PHP:
$link = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($link));

PHP - Cannot connect to MySQL database

I am trying to connect to my MySQL database through php, I am managing my Database with phpmyadmin. To login the username is root and I dont have a password. My problem is I cant connect, I get the "Could not connect to mySQL database" message when I try to
Below is my Code
<?php
session_start();
$server = 'localhost';
$db_usernmae = 'root';
$db_password = '';
$database = 'househockey';
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
if (mysql_select_db($database)) {
# code...
die('couldnt connect to database');
}
?>
Im not sure if it matters, but I am using WAMP and I put my phpmyadmin folder into my htdocs folder.
Thanks
As you have written your code :
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
This will when connection is true print the following: die('Could not connect to mySQL database'); I think what you need to test your connection, which sounds like it should work:
if(!mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
The ! will negate the returned value of your mysql_connect and tell you if you're connected.
As far as I know, PMA explicitly needs a username and password. Set a root password through
mysqladmin -u root password NEWPASSWORD and then change your PMA config, followed by a server restart. Alternatively, use MySQL workbench. It does more than create entity relationship diagrams (ERDs).
You may run into this problem if you have an anonymous user defined on your database.
"When a client tries to connect to the database, the MySQL server looks through the rows in the user table in a sorted order.
The server uses the first row that matches the most specific username and hostname."
Delete the anonymous user and try again.

PHP remote login to mysql db: "cannot select db"

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$host="host.org"; // Host name
$username="myServer_guest"; // Mysql username
$password="password"; // Mysql password
$db_name="myServer_JMblog"; // Database name
$tbl_name="blog"; // Table name
// Connect to server and select databse.
$link=mysql_connect("$host", "$username", "$password")or die("cannot connect");
echo $link.'<br>';
mysql_select_db("$db_name")or die("cannot select DB");
?>
I am getting a successful connection, but now I cannot select the db. All login credentials are correct. Any help would be amazing.
Good sugegstions in comments already.
Another thing to understand is that credentials can be different
from differnt boxes. A easy test is to try simple login using mysql-shell directly,
preferably if possible from same machin/IP as the php is running on.
Try cmd> mysql -hhost.org -umyServer_guest -ppassword Server_JMblog
You will probably get an error since your php isn't working,
post this error and it can tell you more on teh actual problem.
issue was adding not only remote access for site "abc.com" but also adding the remote site IP. Once this was completed it worked! Thank you Marc B for the great pointers.

Categories