I want to create a database. Why is not the db created with this code?
$dbname = 'regulations_db'; $con = mysql_connect("localhost","root","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_num_rows(mysql_query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '". $dbname ."'"))) { echo "Database $dbname already exists."; } else { mysql_query("CREATE DATABASE '". $dbname ."'",$con); echo "Database $dbname created."; }
This is working, but I think the first one is the best practice:
if (mysql_query("CREATE DATABASE IF NOT EXISTS regulations_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); }