Page 1 of 1

mysql_create_db doesn't work?? Please help!

Posted: Sat Nov 22, 2003 5:31 am
by lazersam
Hi all
Im learning PHP but am completely stuck on this.... I want to create a database and got this from my book.

Code: Select all

<?php
#Database Control
#Creating a Database.

$conn = @mysql_connect("localhost", "BLA BLA", "BLA BLA") or die ("Sorry - could not connect to MySQL: " .mysql_error());


# THIS BIT DOESNT MAKE A DATABASE(?) <----------------------
$rs1 = @mysql_create_db($db) or die ("didn't work");
     
$rs2 = @mysql_list_dbs( $conn );

for($row=0; $row<mysql_num_rows($rs2); $row++)
&#123; $list .= mysql_tablename($rs2, $row)." | "; &#125;

?>

<html>
<head><title>Creating Databases</title></head>
<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>
Anyone know why this script is unable to make the database?

Lawrence.

Posted: Sat Nov 22, 2003 6:13 am
by infolock
well, #1, you are telling it to create a database with a name of whatever value $db is, but you aren't assigning it any value at all. Next, you are trying to get the rows in the database, without even first selecting it, or creating a table. if you want to create a database, here is the code for it. After that, you are gonna need to create a table, then maybe the rest of the code will work for you after a little tweaking :P

Code: Select all

<?php 

#Database Control 
#Creating a Database. 

$db = 'Some_Database_Name';

@mysql_connect("localhost", "BLA BLA", "BLA BLA") or die ("Sorry - could not connect to MySQL: " .mysql_error()); 

$sql = "create database $db";

$result = mysql_query($sql) or die(MySQL_Error());      

echo 'database created successfully!; 
?>

Posted: Sat Nov 22, 2003 7:55 am
by lazersam
Thanks infolock

I see what your saying now! That makes sense. It's funny cos thats exactly how it is printed in the book.

Thanks for your help - I will try it.

Lawrence.