Using php script to create tables in Mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jamal
Forum Commoner
Posts: 57
Joined: Sat Oct 26, 2002 7:53 pm
Location: London

Using php script to create tables in Mysql

Post by jamal »

Hi,
Please I am just a newbie in Mysql.
I wondered if anyone could help me create a table using php script.
Assuming the name of the table is tracker and the fields are id, host, browser, count.
Please write the script for me.
Thanks
Jamal
Vaporizing_Boxes
Forum Newbie
Posts: 1
Joined: Mon Dec 16, 2002 9:34 pm

Try this.

Post by Vaporizing_Boxes »

I ran into this problem myself and I couldn't find a tutorial so I downloaded a few free scripts and sure enough, one of them had a simple add table code block. I modified it for my needs and I'm sure that you can do the same for yourself. Give a shout if you need more of an explanation.

Code: Select all

mysql_query("CREATE TABLE teachers (
user VARCHAR (20) not null,
pass VARCHAR (20) BINARY not null,
name VARCHAR (20),
ID INT (11) not null AUTO_INCREMENT,
email VARCHAR (255),
filename VARCHAR (255),
grade VARCHAR (20),
level CHAR(1),
PRIMARY KEY (ID))")OR DIE
("There is a problem with creating table teachers.  This script has stopped.");
echo "<BR>Table teachers created.";
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

before

Post by AVATAr »

You have to:
1. open the conection
2. select the Data Base
3. make SQL statement "CREATE..."
4. Execute the SQL statement
5. close the conection

Code: Select all

<?php
$connection = mysql_connect("host", "login", "pass");
mysql_select_db("database",$connection);
$sql="CREATE TABLE.... ";
mysql_query($sql, $connection);
mysql_close($connection);
?>
jamal
Forum Commoner
Posts: 57
Joined: Sat Oct 26, 2002 7:53 pm
Location: London

Post by jamal »

hi guys,
I am having a problem in inserting some data into my database table.
Please I wondered if you could help me fix the error??
Here is the error message I got:
Could not add data to the table.
Thanks

Code: Select all

<?php // 4.0.5 FINAL


require_once ("db.inc");

$database = "jamDB";

// select active db
if(!mysql_select_db($database,$link)) {
 printError(sprintf("Error in selecting %s database",$database));
 printError(sprintf("error: %d %s",mysql_errno($link),mysql_error($link)));
 exit();
}

// this is the name of the table you want to create
$tablename = "users";

// create my table
if(!mysql_query(sprintf($mysql_query,$tablename), $link)) {
 printError(sprintf("Error in executing %s stmt",$mysql_query));
 printError(sprintf("error: %d %s",mysql_errno($link),mysql_error($link)));
 exit();
}

// change this statement to reflect
// the database you'd like to create


$insert = "INSERT INTO users ('firstname','surname','address','citycode','city','country','email') 
    VALUES ('Carl','Newton','Molenway','3600','Brussel','Belgium','me@mydomain.com')"; 
    mysql_query($insert) or die ("Could not add data to the table");
    
$query = "SELECT * FROM users";  
$result = mysql_query($query); 
$numrows = mysql_num_rows($result); 
while($row = mysql_fetch_array($result)){ 
    echo "You have $numrows user(s) in the database"; 
    echo "First Name: $row[firstname]"; 
    echo "Surname: $row[surname]";
    echo "Address: $row[address]";
    echo "City_Code: $row[citycode]"; 
    echo "City: $row[city]";
    echo "Country: $row[country]";    
    echo "Email_Address: $row[email]"; 
}  

if(!mysql_query(sprintf($insert,$tablename), $link)) {
 printError(sprintf("Error in executing %s stmt",$mysql_query));
 printError(sprintf("error: %d %s",mysql_errno($link),mysql_error($link)));
 exit();
}
printf("<br> Created Table %s.%s <br>\n",$database,$tablename);

?>
Post Reply