If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Create database

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-10, 03:42
nadeem14375 nadeem14375 is offline
Registered User
 
Join Date: Oct 2010
Posts: 3
Create database

Dear all,

I new to PHP/MySql.

here i am trying to create a database.


<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
die('connected......'.mysql_error());
}

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

// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query
mysql_query($sql,$con);

mysql_close($con);

?>

1. I couldn't identify that whats done. In PHPMyAdmin there is no database with name "my_db".

2. I could see only one message on my page the "connected......" which is from line no: 9. if the CREATE DATABASE is unsucess then the other message "Error creating database:" should be displayed.

Regards:

Muhammad Nadeem
Reply With Quote
  #2 (permalink)  
Old 10-09-10, 04:31
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
thats because you are using the die construct on both arms of the if statement.
die halts execution of the script.

usually dies is used to report an error message, not a success condition.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 10-09-10, 05:05
nadeem14375 nadeem14375 is offline
Registered User
 
Join Date: Oct 2010
Posts: 3
Create database

Dear,

Please guide me what to do?

I tried the both die and echo but the same address.

I am new to PHP and MySql.


waiting for you.

Muhammad Nadeem
Reply With Quote
  #4 (permalink)  
Old 10-09-10, 05:17
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
so what happened when you used to echo function?

ironically you don't use the or die construct or examine the MySQL error / status whenyou try to create the table

PHP Code:
<?php
$con 
mysql_connect("localhost","root","") or die('Could not connect: ' mysql_error());
// create Database
$qResult mysql_query("CREATE DATABASE my_db",$con)) or die('Database created...... '.mysql_error());
// echo "Database created";

// Create table
mysql_select_db("my_db"$con) or die (MySQL crapped out with:".MySQLErrNo()." ".MySQLError);

$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query
$qResult = mysql_query($sql,$con) or die (Failed to create table: ".MySQLErrNo()." ~ ".MySQLError);

mysql_close($con);
?>
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Tags
mysql, php

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On