Page 1 of 1
Using php script to create tables in Mysql
Posted: Fri Dec 13, 2002 6:27 pm
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
Try this.
Posted: Mon Dec 16, 2002 9:34 pm
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.";
before
Posted: Mon Dec 16, 2002 10:28 pm
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);
?>
Posted: Thu Dec 19, 2002 2:31 pm
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);
?>