mysql php, connecting to localhost - php

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

Related

Connecting to a mysql database without keeping details in the script

I am attempting to create a separate login file for database connections as I am not too fond of having all the access details on each page that requires database access.
I have created a separate file on my server that contains the variables required for a successful login and then use the;
include_once('path_to_file/filename.php');
to get the variables and then use;
$dbconnection = mysqli_connect("$hostname","$username","$password","$database") or die ("Could not connect to the server");
but the connection fails every time. I tried including the connection script in the file I am attempting to include but then I get this message:
Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)
I'm not really sure how to fix this, but every page in my server more or less access the database and I think it has to be a security risk having login details replicated everywhere!
Anyone have any suggestions or alternatives?
databaseloging format is:
<?php
# parameters for connection to MySQL database
$hostname="hostname";
$database="databasename";
$username="username";
$password="password";
?>
P.S. I have also tried require and got the same result.
Also when using multiple MySQL connections in PHP, you have to supply a fourth argument telling PHP to actually create new connections like this (this is very important, if you are using two connections to the same host):
$db1 = mysql_connect($host1, $user1, $passwd1, true);
$db2 = mysql_connect($host2, $user2, $passwd2, true);
If the fourth argument is not used, and the parameters are the same, then PHP will return the same link and no new connection will be made.
After this you should use "mysql_query" with an extra parameter than defines which connection to use:
$res1 = mysql_query($sql1, $db1) or die(mysql_error($res1));
$res2 = mysql_query($sql2, $db2) or die(mysql_error($res2));
http://www.php.net/manual/en/function.mysql-connect.php

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.

how i can Import data from Database ( My Sql) to another website

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.

Moving MySQL from localhost to remote server

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.

Categories