in the past I have used smt like:
$con = mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("xxx");
But now, I've noticed everybody started using smt like this:
if (!defined('DB_HOST')) define('DB_HOST','localhost');
if (!defined('DB_USER')) define('DB_USER','root');
if (!defined('DB_PASS')) define('DB_PASS','root');
if (!defined('DB_NAME')) define('DB_NAME','xxx');
What's different about their usage? For example echoing, filtering..
Thanks for help
Well, none of them are wrong, it's your choice. I think the second alternative is more like a good practice. But my statement is valid only if you define it in one file and require_once in the others. Example:
db_config.php:
if (!defined('DB_HOST')) define('DB_HOST','localhost');
if (!defined('DB_USER')) define('DB_USER','root');
if (!defined('DB_PASS')) define('DB_PASS','root');
if (!defined('DB_NAME')) define('DB_NAME','xxx');
query.php:
require_once db_config.php;
$con = mysql_connect(DB_HOST, DB_USER, DB_NAME) or
die("Could not connect: " . mysql_error());
mysql_select_db("xxx");
But why? Imagine that you want to change your database name, user or password. If you have code mysql_connect("localhost", "root", "root") every time you needed it, a change of plans will make you go through a lot of re-coding. But if you define them in one place as the second alternative does, you won't have to rewrite a lot of files.
If you have connection in more than one place(poor design) With constants you would have one place where you can edit connection configuration without need to go through all code and find all the places where you opened connections.
So your connection code would look like:
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS) or
die("Could not connect: " . mysql_error());
mysql_select_db(DB_NAME);
Related
< ?php
$connection = mysql_connect(localhost","user","password") or die ("Couldn't connect to the server!");
mysql_select_db("users", $connection) or die ("Couldn't connect to the database!");
I used the above code above to try and connect to my database but keep getting an error in line 2 where mysql_connect(localhost)---- I've typed in my localhost name correctly why won't it work?
please help this is my first day ever dealing with php and databases.
thanks in advance.
Missing a quote in localhost:
$connection = mysql_connect("localhost","user","password") or die ("Couldn't connect to the server!");
The correct way to do it is:
<?php
$connexion = mysql_connect("localhost", "user", "password")
or die("Couldn't connect to the database! : " . mysql_error());
mysql_close($connexion);
?>
Nota Bene:
Rather use mysqli_ or PDO. What you are using here is deprecacted for security reasons.
I'm in the process of converting some queries to prepared statements and I currently have the following in a connect file:
<?php
DEFINE ('DB_USER' , 'blah');
DEFINE ('DB_PASSWORD' , 'blah');
DEFINE ('DB_HOST' , 'localhost');
DEFINE ('DB_NAME' , 'blah');
//make the connection
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die ('Could not connect to MySQL: ' .
mysqli_connect_error() );
?>
Can I still use $dbc somehow? I don't want to plaintext my connection information into a statement like this:
$mysqli = new mysqli("heres", "all", "my", "sensitive", "information"):
Thanks in advance!
You can put the sensitive information in a file that's not in the webroot.
And you could use SSL to connect to the mysql server
http://dev.mysql.com/doc/refman/5.0/en/ssl-connections.html
When trying to connect to my database using PHP I am getting an error.
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
mysql_select_db("databasename") or die ("Couldnt find database");
When I try running the script it keeps saying 'Couldnt find database' which means that I am connecting to the server but my database won't connect.
Am I doing something wrong here? I have pretty much copied and pasted my database name from Cpanel so I know there aren't any mistakes. But in Cpanel it is displayed as 'mywebsite'_database
Any ideas?
You specify in your question that your database is called:
'mywebsite'_database
If this is the case, then you need to change your code to
// Connect to the database
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
// Select the database
mysql_select_db("mywebsite_databasename",$connect) or die ("Couldnt find database");
If the above method does not work then I would advise debugging your connection by listing the databases for that particular user. Do this like so:
// Connect to the database
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
// Get all the databases for this particular user
$databases = mysql_list_dbs($connect);
// Loop through the databases and write them on the screen
while($database = mysql_fetch_assoc($databases)) {
echo $database['Database']."\n";
}
exit;
mysql_select_db("mywebsite_database")
Print out the names of all the databases in the server (using mysql_list_dbs).
Find the correct database name and replace "databasename" with it.
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
$db_list = mysql_list_dbs($connect);
while ($row = mysql_fetch_object($db_list)) {
echo $row->Database . "\n";
}
mysql_select_db("databasename", $connect) or die ("Couldnt find database");
I recommend using MySQLi extension as mysql_* is currently in a depreciation process.
I hope this helps
I've seen a few errors like this, but I found no answer.
Unable to connect to database: Access denied for user ''#'localhost' to database 'socialdb'
socialdb is my database. The "Unable to connect to database:" part is located here.
$db = mysql_select_db("socialdb",$con);
if(!$db) {
die ('Unable to connect to database: ' . mysql_error());
}
I don't know what's causing this. Here are my mysql_connect details
<?php
$con = mysql_connect("localhost");
if(!$con) {
die ('Error: ' . mysql_error());
}
I need to find the root. Thanks.
I DON'T HAVE A USERNAME OR PASSWORD FOR MySQL
Should it be this?
mysql_connect("localhost","","");
The error is pretty self-explanatory, you're not allowed to connect without specifying some credentials.
Change your call to mysql_connect() to something like this:
mysql_connect("localhost", "user", "password");
Your default credentials are most likely:
$con = mysql_connect("localhost","root","");
You're missing username and password parameter, should be:
$con = mysql_connect("localhost","username","password");
You did not specify a user in neither mysql_connect or your PHP configuration.
Either set mysql.default_user and mysql.default_password in your PHP configuration or use the appropriate arguments for mysql_connect.
mysql_connect takes 3 parameters
$con = mysql_connect("localhost", "dbuser", "password");
even if you are not having a user. there exist 'root' user
so use it
$con = mysql_connect('localhost','root','');
Not wanting this question to be too long, I will skip to an example:
If I have 2 files: paper.php and rock.php, and they contain the following:
paper.php:
include('rock.php');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
and rock.php:
define ("DB_HOST", "localhost");
define ("DB_USER", "foo");
define ("DB_PASS","bar");
define ("DB_NAME","fooDBar");
Eventually, will the user viewing my paper.php file be connected to the DB or not?
Not wanting the answer to be too long:
Yes.
Yes, you define all the appropriate variables in rock.php and are including rock.php, then they will be defined for the whole program execution, including where you do a mysql_connect().