Ausghostdog Ausghostdog - 7 months ago 16
SQL Question

(solved) Create a database with php, blank pages?

I was running the file from my desktop, not from htdoc ..... really stupid mistake

So trying to make a database with php, however if I run my code I get just blank pages in both Firefox and Chrome?

Is there something I have to do in order to make php work?

<html>
<head>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

mysql_close($con);
?>


</body>
</html>

Answer

Use mysqli_connect(). Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed.

Try below.

<?php
$con=mysqli_connect("localhost","root","");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

if (mysqli_query($con,"CREATE DATABASE my_db1"))
  {
  echo "Database created";
  }


mysqli_close($con);
?>
Comments