MySQL from PHP

Connecting from PHP to MySQL is quite easy, assuming that you have all the required information.

Assuming that we want to connect to a MySQL database on the localhost by the root user that has a very unsafe password to use the test schema, we could do that by this function:

function myConnect() {
$dbHost = "localhost";
$dbUser = "root";
$dbPassword = "password";
$dbSchema = "test";

($conn = mysql_connect($dbHost, $dbUser, $dbPassword)) or die ("Can't connect");
mysql_select_db($dbSchema, $conn) or die ("Can't select database");

return $conn;

It's not a nice piece of code, in case of error is quite brutal, actually. But it should be clear what it does.

No comments:

Post a Comment