PDA

View Full Version : Why doesn't this work


Steve Leader
05-01-2005, 11:27 PM
This routine is supposed to list all presentr databases and give you the oipertunity to start a new one.

Mysql, php and Apache all installed ok. Asume that user=steve and password=leader and routine is being saved to apache's htdoc dir. Here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
$conn=@mysql_connect("localhost","steve","leader")
or die ("Sorry no connection to MySQL".mysql_error( ));
$rs1=@mysql_create_db($db);
$rs2=@mysql_list_dbs($conn);
for ($row=0; $row <mysql_num_rows($rs2); $row++)
{$list .= mysql_tablename($rs2, $row)." | ";}
?>

<html>
<head><title>Create Database Using PHP</title></head>
<body>
<form action="<?php echo($PHP_SELF); ?>" method="post">Current Databases:
<?php echo ($list); ?><hr> Name: <input type="text" name="db"><input type="submit" value="Create database">
</form>
</body>
</html>

Tarmithius
06-08-2005, 01:30 PM
From the official PHP site: The function mysql_create_db() is deprecated. It is preferable to use mysql_query() to issue a sql CREATE DATABASE statement instead.

Try the below code but change the parameters.

<?php

// set your infomation.
$dbhost='localhost';
$dbusername='david';
$dbuserpass='mypassword';
$dbname='test';

// connect to the mysql database server.
$link_id = mysql_connect ($dbhost, $dbusername, $dbuserpass);
echo "success in database connection.";

// create the database.
$dbname=$dbusername."_".$dbname;
if (!mysql_query("CREATE DATABASE $dbname")) die(mysql_error());
echo "success in database creation.";

?>