connect to mysql DB using php - php

I tried to connect to my DB server using the hostname and the port in the file config.php but still give me error, I won't use the localhost server, the database is in other server.
I get the hostname using : show variables like '%hostname%'
and get the port using : SHOW VARIABLES WHERE Variable_name = 'port'
define('DB_USER', "test");
define('DB_PASSWORD', "test");
define('DB_DATABASE', "test");
define('DB_SERVER', "My problem is here");
someone know what I did wrong?
thank you

Some hosts, you can't connect to their database from your localhost, but only from their webserver.
Maybe thats your problem ?

Related

How do I use my host server info instead of localhost on XAMPP?

I have been building a website with PHP functionality in XAMPP, and everything works perfectly within the localhost. Although, I know that in order to have the same functionality on a live hosted server I will need to change the server info in my config.php file that is used:
<?php
define('ROOT_URL', 'http://localhost/newkellumws/');
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'austink01');
define('DB_NAME', 'kellumws');
I've tried changing the DB_HOST to my hosting profile's nameserver, but that did not work. Any help is much appreciated and thank you for your time. I apologize if the is a newbie question...
Assuming you are also moving the site as well the hostname will remain as localhost (as is the case with most shared hosting)
Assuming it's cPanel hosting you will first need to create the database.
You can then create an SQL account and grant it access to the DB (recommended) or use your cPanel credentials (Not Recommended)
So your config would look something like this:
define('ROOT_URL', 'http://example.com/newkellumws/');
define('DB_HOST', 'localhost'); // Website and SQL ruinning on the same server
define('DB_USER', 'exampl_kellumws');
define('DB_PASS', 'aBc*63oie8wfq');
define('DB_NAME', 'exampl_kellumws');
See https://documentation.cpanel.net/display/68Docs/MySQL+Databases
if the site is still going to be run via XAMPP (For whatever reason) you would also need to allow Remote MySQL
See https://documentation.cpanel.net/display/68Docs/Remote+MySQL
So your config would instead look something like this:
define('ROOT_URL', 'http://example.com/newkellumws/');
define('DB_HOST', 'c01.example.host'); //Address the SQL Server
define('DB_USER', 'exampl_kellumws');
define('DB_PASS', 'aBc*63oie8wfq');
define('DB_NAME', 'exampl_kellumws');
If you still have issues, contact your hosting provider as they will know the server setup and requirements.
You can also create testConnection.php with the following to help diagnose errors
<?php
require_once('path/to/file/with/config.php');
//Step-1 : Create a database connection
$connection=mysql_connect(DB_HOST,DB_USER,DB_PASS);
if(!$connection) {
die(“Database Connection error: ” . mysql_error());
}
//Step-2 : Select a database to use
$db=mysql_select_db(DB_NAME,$connection);
if(!$db) {
die(“Database Selection error” . mysql_error());
}
echo('Connected to Database');
Your server infos and local XAMP are different localhost works like following.
You dont need url part as its localhost/your_folder
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', ''); //Xamp doesnt have a password for root as default leave it empty if you didnt set one.
define('DB_NAME', 'kellumws');
define('PORT', '3306');
latest version of XAMP request port for MySql port, add it into your connection. mostly its 3306 as default but sometimes it changes to 3307 or 3308 because of mariaDB comes as default you can see real path on XAMP tool.
Than just call http://localhost/newkellumws/ if you created a folder.

mysql database connects only if i run my local wamp server even though all the files are in server site

Its not connecting to my web hosting server database even though all the files are in server and all the credentials( username, password, database and host-name ) are updated! why its still connecting to my local machine database? even though i changed the username and password, its connecting to my local database itself! how's that possible?
I created another new database and tables in my server database. everything is done! but connecting to my local database!!
<?php
header("Access-Control-Allow-Origin: *");
define('DB_NAME', 'asknow');
define('DB_USER', 'myself');
define('DB_PASSWORD', '*****');
define('DB_HOST', '134.345.**');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
?>
That is because you set your host as localhost, which means your computer here.
define('DB_HOST', 'localhost');
That is why your data still goes to your local database.
Instead, you should set your database's ip on your shared hosting as your host.
---- Edited ---- (because the question is edited)
You should use your dns like www.yourdns/connect.php to link to your connect.php file in your shared hosting.

Can't login to my wordpress, phpmyadmin database

I'm looking into my local wp-config and I see this:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_HOST', 'localhost');
But no matter which combination I use I can't login to phpmyadmin, how can I troubleshoot this so I can see my database tables?
You wont be able to login to your phpmyadmin using localhost. You'll need to ask your hosts what the actual IP or Address is.

Error when connecting in phpMyAdmin

This may be a stupid question, but I've been following a tutorial online for learning php and I'm using an exercise file from it that is a sql file. The instructor set up a username and password when the database was set up in the MySQL console in WAMP. I know it works this way, but now I'm trying to test it out in a different way by importing it into phpMyAdmin on my server, but I get an error when trying to connect.
It says the database connection failed because I don't have access to the "local MySQL server", and mentions a socket that it's trying to connect through (I'm not sure what that means). I'm wondering if it has something to do with the username and password I am using. Would that carry over in the file I uploaded from the lesson? I tried using the username and password the instructor used, and then I tried using the one I use for logging on to the phpMyAdmin page, but I got the same error both times. Is there another way to set a username and password or I am completely off and the problem lies somewhere else?
Here's the code I use to establish the connection:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
?>
Thanks for your help!!
Edit: the exact error I get is
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"

Problem to connect on MySql database on the remote server

I have been developing my php mysql application locally and it worked fine with this connection parameters:
// Database connection params
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'some_db');
at some point I wanted to put application online with connection parameters:
define('HOST', 'somedomain.com');
define('USERNAME', 'some_db_admin');
define('PASSWORD', 'Password1!');
define('DATABASE', 'some_db');
When I try to connect to remote database like this:
function db_connect()
{
$connection = #mysql_connect(HOST, USERNAME, PASSWORD);
if(!$connection)
{
return false;
}
if(!mysql_select_db(DATABASE))
{
return false;
}
return $connection;
}
I get error:
Access denied for user 'some_db'#'localhost' (using password: NO)
How can it possibly be localhost?
I have regularly created database and user that database using using database wizard in cpanel.
What can be the problem?
Thanks a lot!
If your PHP code is at the same host where the MySQL database is then setting HOST to localhost should solve your problem
define('HOST', 'localhost');
It also seems that you swapped your constants, because some_db is not likely to be a user but rather a database. Try debugging and output the constants' values just before the 'mysql_connect' call.
If this does not work, try debug_backtrace().

Categories