Forum Moderators: coopster

Message Too Old, No Replies

Creating a Database from within browser window-PHP

         

prettypixel

10:05 pm on Oct 9, 2007 (gmt 0)

10+ Year Member



Can any one help please? I am learning the basics of PHP and MySQL and am reading "PHP 5 In Easy Steps". The code below taken from a tutorial in the book, should create a database from within the browser window. However, this page displays as a blank and does not work. I think the script is for an older version of php and MySQL. I have php 5.2.3, MySQL 5.0.45 and Apache 2.2.4 installed and working OK. I would be very grateful if some one could update the code for me. Thank you.

<!-- example for PHP 5.0.0 final release -->

<?php

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

$rs1 =@mysql_create_db( $_REQUEST['db'] );

$rs2= @mysql_list_dbs($conn);

$list = "";

for( $row=0; $row < mysql_num_rows( $rs2 ); $row++ )
{
$list .= mysql_tablename( $rs2, $row ) . " ¦ ";
}

?>

<html>

<head>
<title>Creating databases</title>
</head>

<body>

<form action="<?php echo( $_SERVER['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>

[edited by: eelixduppy at 10:31 pm (utc) on Oct. 9, 2007]
[edit reason] removed specifics [/edit]

eelixduppy

10:42 pm on Oct 9, 2007 (gmt 0)



Hello, prettypixel, and Welcome to WebmasterWorld!

There are a few quick things I'm going to say about your code, which may or may not be the reason you are having issues. Firstly, remove the error suppression operator, @, before your functions so that you can see errors when they occur.

Secondly, the functions you are using are depreciated. You should be using mysql_query() and then the respective mysql query to get what you want. For instance, creating a database would be something like this:


$query = "CREATE DATABASE `".[url=http://www.php.net/mysql-real-escape-string]mysql_real_escape_string[/url]$db_name)."`";
$result = mysql_query($query) or die(mysql_error());
#the 'die' expression is for debugging purposes here

The same would also go for listing the databases.

Further debugging, you may want to check your error logs and see what's going on behind the scenes. A blank screen in php usually means a fatal error somewhere in your code.