Friday, July 31, 2009

How do I connect to a mySQL database using a PHP script?

I know how to read/write/change table values. I just need the initial connection info.

How do I connect to a mySQL database using a PHP script?
http://www.php-mysql-tutorial.com/connec...
Reply:http://localhost/phpmyadmin/





Is that what you mean?
Reply:It depends on what version of PHP you have.





If you are lucky, you have PHP 5.1+ that has PDO built in.





If you do, all you need to do is create a new PDO object:





$dbh = new PDO( 'mysql:host=localhost;dbname=test', $user, $pass);





if you aren't as lucky, you will need to use either the mysqli or mysql functions:





$dbh = mysqli_connect ( [string host [, string username [, string passwd [, string dbname [, int port [, string socket]]]]]] );





OR





$dbh = mysql_connect( 'mysql_host', 'mysql_user', 'mysql_password');


No comments:

Post a Comment