the connection to database via PHP is as:
<?php
$DbHost = 'localhost';
$DbName = 'root';
$DbUser = 'root';
$DbPwd = '';
$dbConn = mysql_connect ($DbHost, $DbUser, $DbPwd)
or die ('MySQL connect failed. ' .mysql_error());
mysql_select_db($DbName,$dbConn) or die('Cannot select database. ' . mysql_error());
?>
Here I've hardcoded my hostname, username and password. Everytime I take my project to another machine, I've to change it.
How can I make this connection code dynamic?
You can put machine specific information in a settings.php file and use
require_once "settings.php";
to get hold of the machine settings. Do this on all your machines and put the file in a specific place so your project(s) is/are able to find it.
When you move your project to another machine, it will use the corresponding settings.php file. (Assuming you are not copying this file as well ;) )
Related
So I'm attempting an assignment for uni through cloud9 and I am struggling to connect to my database. when I run the application this appears instead of connecting. what i see when running the application
I will post my current code, but it has all been approved by the teacher. and my code in my other .php files have been copied pasted from the teachers files (whose worked on her computer but when I cloned it (and also created a new workspace with it copied pasted) with her exact work the same picture appears.
<?php
require 'functions.php';
// Error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);
/*MySQL connection settings */
$dbhost = "localhost";
$dbuser = "bmarino";
$dbpass = "";
$dbname = "movie_rentals";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//test if connection works
if (mysqli_connect_errno()) {
die ("Database connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno . ")"
);
}
echo "connection successful";
?>
i was not the only person in my class to have this appear, and only one student was able to connect successfully (i'm not sure what they did)
Okay I figured it out. It was because I had my .php files in a folder, and for some reason when I took them out of the folder the preview loaded!
I would delete this question but I might leave it up in case anyone else encounters the same issue :)
quick mysql question.
I'm new at php/mysql and followed a tutorial(php/mysql for dummies) so I don't really know what I did wrong or if the tutorial is wrong.
I have a file, "database_connections.inc", that looks like this:
<?php
$user = "username";
$host = "host";
$password = "password";
$database = "database";
?>
With the actual credentials not included for obvious reasons.
Then in another file, login.php, I have:
include("database_connections.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die("Query died: Couldnt connect to server.");
I get an error message with the "or die" text, accompanied by a warning:
host xxxxx.000webhost.com is not allowed to connect to this mysql server in....
Why not? I'm sure my credentials are all correct.
I've read in a few places to run some shell statements...but can't really do that, I'm on Windows.
I'm using phpMyAdmin, so hopefully I can do something from there?
Open "database_connections.inc" and change it to look like this:
<?php
$user = "root";
$host = "localhost";
$password = "";
$database = "test";
?>
MySQL is by default configured to work with localhost (or 127.0.0.1), in order to allow "host xxxxx.000webhost.com" as host, open phpMyAdmin and select "SQL" and execute this query;
GRANT ALL ON your_database_name.* TO your_user#your_host_xx.xxx.xx.xx IDENTIFIED BY 'your_password';
Go into PHPMyAdmin, and edit your user.
Under Login Information, there should be an option for "Host"- try adding xxxxx.000webhost.com.
I am very new to php, and am sorry if this question turns out to be too vague, but if there is any other information that you need from me let me know.
Anyway, basically what my problem is I have some simple php code that should call die() if it can't connect to the xampp server I have set up, however, even if I put in invalid info for the server or user it prints Successful. I really don't know where to go with this, so if anyone has any suggestions that would be great.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if ( !mysql_connect($dbhost, $dbuser, $dbpass) )
{
die(mysql_error());
}
else
echo 'Succesful';
?>
Ok for some reason it was just not working with the root user, so I created a new user who required a password and apparently that connected like it was supposed to. Thanks everyone for the answers.
Your code is correct pistolpete333. For your root user issue, that your not able to connect with root user you can check below file for your phpmyadmin configuration
C:\wamp\apps\phpmyadmin3.5.1\config.inc.php.
In above file you can check your host, username and password of phpmyadmin.
I have found that when trying to connect php to an xampp server there are usually only a few things that could cause the issue you are having.
1. mySQL service is not running
2. no specific database is selected to try to access
3. user does not exist in phpmyadmin.
When you pick a user to connect php to mySQL with you should always make sure that a password is set, that is usually the thing people miss. You can use root if you create another instance of it in the user list and require a password, or you can add a password to the root user already in the list, but the best option is to create a new custom user that only has access to the database you want to access with your php and make sure it has a password required. (make sure any user you use can connect with localhost; root can by default and I think most newly created users can as well).
Below is the php I use for websites I design that need php to access a DB.
<?php
// This configures the variables for database access
$dbhost = 'localhost';
$dbuser = '????';
$dbpass = '????';
$dbname = '????';
// This will connect to the server and then the correct database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
This is working because you have specified valid username, host and password to mysql. If you specify wrong password, you get mysql error. This code works and connects to the mysql server though you have not selected any database.
Try this code instead
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link); //foo is your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
Basically, i'm having trouble connecting to a mysql database using a php web page.
I created the database in C-panel using the wizard
i'm connecting like this
$db_host = "localhost"; //your host Database address
$db_username = "xxxx"; //your account username
$db_pass = "xxxxx"; //your account password
$db_name = "xxxxx"; //your database name
#mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
#mysql_select_db("$db_name") or die ("no database");
But all my page seems to do is trigger the " could not connect to mysql"
my page does have wordpress installed at the minute but I'm planning to get rid of it as I'm creating my site myself. I'm just baffled why it can't connect, because in Phpmyadmin ( a feature on C-panel) it says the database is in localhost.
In C-panel you need to add database user and assign certain roles to it before you can connect to the database.
I have this query:
mysql_select_db('scanner');
$query = "SELECT * FROM scanner.proxy ORDER BY RAND() LIMIT 20";
$result = mysql_query($query) or die(mysql_error());
it tells me:'scanner.proxy ' doesnt exist.. even though I do have it the table with the database. It is weird, cause my other queries work..but this one doesnt.
UPADTE:
Event when I put this:
$user='root';
$user='root'
$password='<removed_password>';
$dbname = 'scanner';
$host = 'localhost';
$link = mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($dbname, $link) or die (mysql_error());
it gives me this...
Unknown database 'scanner'
But I can see the scanner database in the phpmyadmin
Even when I type in phpmyadmin the sql statement
SHOW TABLES FROM 'scanner'
it says it cant find scanner
We solved this problem which occurred when connecting to multiple remote MySQL databases within the same script by adding a re-connect method before every mysql_query statement. The re-connect method invoked both mysql_connect and mysql_select_db functions. We had previously tried setting the new link parameter to true in the connect statement(s) but We still got database errors and all kinds of funny behavior.
make sure the user you use to connect has enough rights to query that table. Perhaps that table was created by another user.
EDIT: Perhaps you need to grant access to scanner from a remote location. Running this sholud help on that.
GRANT ALL ON scanner.* TO your_user#'IP_ofServer_where_PHP_runs' IDENTIFIED BY 'PASSWORD';
You have set the $dbname = 'aws_backlinks', but you are trying to connect to a different database within your query. Change your connection string to include this db instead or just connect to the server and set your database at the time of the query.
$db_host = 'localhost:Port';
Specify port for localhost.
I was facing the same problem and specifying the port solved the problem.
You're missing the argument that specifies the connection (result of mysql_connect()) in your mysql_select_db() call:
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'whatever';
$db_name = 'scanner';
$link = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name, $link);