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));
Related
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
I paid someone to develop a website for me. It has been about a week and the website has stopped working. I am trying to get in working on localhost but am confused as to what I should be doing.
I created a database on phpmyadmin called called mike but when I go to check if it works i get the error Access denied for user
define ("DB_HOST", "host.mywebistehub.com"); // set database host
define ("DB_USER", "mike"); // set database user
define ("DB_PASS","aB1287z600!"); // set database password
define ("DB_NAME","mike"); // set database name
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS)
or die("Couldn't make connection."); //this is line 17
$db = mysql_select_db(DB_NAME, $link)
or die("Couldn't select database");
The error message seems clear enough.
The username "mike" isn't allowed to access the database using the password you've given from the hostname of the machine you are running your code on.
You need to find out which of those three things are wrong and either change the permissions on your database or the credentials you are trying to connect with.
I am trying to get in working on localhost
define ("DB_HOST", "host.mywebistehub.com"); // set database host
host.mywebistehub.com is not localhost
I need to show data from another website in my website,
i had control panel for tow site but i need it dynamic way to connect them
website 1 website 2
***** *****
* A * <<<< =Data== * B *
***** *****
so i write
$link = #mysql_connect("Ip Addres to another website","username for mysql 2","passowrd 2 ")or die("Couldn't make connection.");
#mysql_select_db("qatarlab_test",$link)or die("Couldn't select database");
$factorRes = #mysql_query("SELECT count(id) FROM `factor` ");
$factorRow = #mysql_fetch_array($factorRes);
echo $factorRow[0];
but nothing happen
Firstly, you must enable remote database connection in the original site so that the new site can connect to it. you can do this in the control panel->Database(Remote Database Connection) of the original site.
You could specify the IP or domain of the new site to that it allows only the new site to connect or you could just add wildcard (%) to allow any external connection to the database.
when this is done, run ur query again.
hope it helps
Hi Please try code given below,
$link = #mysql_connect("Ip Addres to another website","username for mysql 2","passowrd 2 ")or die("Couldn't make connection.") or die(mysql_error());
mysql_select_db("qatarlab_test",$link)or die("Couldn't select database", $link) or die(mysql_error());
$factorRes = #mysql_query("SELECT count(id) FROM `factor` " , $link) or die(mysql_error());
$factorRow = #mysql_fetch_array($factorRes);
echo "<pre>";
print_r($factorRow);
echo "</pre>";
Here $link will be for specific database connection and die(mysql_error()) for if any error in or query or database connection.
thanks
In your first line: mysql_connect you are passing the IP Address and Username for connecting the DB.
Make sure that the user has the privilege to connect to the DB remotely (i.e. via a machine other than the Localhost). In general, when a user is added in the DB its default access is for the localhost. Check out this link: http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
You basically need to add user using: CREATE USER 'admin'#'IP Address of Website 1'; All the CPanel's have this provision in their interface as well.
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.
So I connect to my MySQL database using the following code:
function dbConnect($h,$u,$p,$n) {
if (!$con = #mysql_connect($h,$u,$p)) {$err = err("There is a problem connecting to the database. ".mysql_error());}
else if (!#mysql_select_db($n,$con)) {$err = err("The database \"{$n}\" could not be found. Check your spelling, make sure the database exists, and that your database credentials allows you access to this database.");}
return (isset($err)) ? $err : "";
}
The problem is, if they put in a wrong username, mysql_connect will not see anything wrong with it and try to connect to the database, which outputs an error for the mysql_select_db().
So I found this link. Now normally removing the "any" user would be doable but I'm creating a phpMyAdmin-like tool and will be connecting to different databases with all types of configurations. How can I tell which username can actually connect? And how can I differentiate between the database name not existing and the username not having access to it?
use mysql_errno()
see error codes here