The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > PHP


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags:

Closed Thread
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 08-17-2007, 05:53 PM
heroine
Posts: n/a
[SOLVED] PHP: Creating tables and timing...

HI All

I have 2 sets of PHP scripts which I combined to execute at once...
- 1...connect to mysql (this is working fine!)
- 2...create tables

Problems:
- when it comes to creating tables (4) only the last one is successfully created and the rest nothing....am i missing something here?

- I would like to include a time out in the script....like once the connection is established, it could print out first something like: Please wait creating tables now...! and then proceed immediately with creating tables.
The timing could be about 5 seconds of delay....how to do that?

here is my script:
<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

$sql = 'CREATE DATABASE test2';
if (mysql_query($sql, $link)) {
echo "Database test2 created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
// Create tables in test2 database

mysql_select_db("test2", $link);
$sql = "CREATE TABLE car (
title varchar(255) NOT NULL,
description varchar(255) NOT NULL,
priority tinyint(2) NOT NULL default '0'
) TYPE=InnoDB;\n";

//Create links table
$sql = "CREATE TABLE links (
type varchar(20) NOT NULL default '',
resource varchar(255) NOT NULL,
) TYPE=InnoDB;\n";

//Create home table
$sql = "CREATE TABLE home (
name varchar(255) NOT NULL,
title varchar(255) NOT NULL default '',
description varchar(255) NOT NULL default '',
) TYPE=InnoDB;\n";

//Create people table
$sql = "CREATE TABLE people (
name varchar(20) NOT NULL default '',
address varchar(255) NOT NULL
resource varchar(255) NOT NULL,

) TYPE=InnoDB;\n";

echo "car,home,links, & people tables in the relational database (".test2.") have been successfully created!\n";
mysql_query($sql, $link);
mysql_close($link);//Close connection
?>


__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #2 (permalink)  
Old 08-17-2007, 05:58 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 442
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Your tables will be created so quickly that a delay would be useless and require a lot of code to look nice.

The reason that it's only creating the last table is that that's all you're telling it to create. Each time you say $sql = "..."; something, you erase the previous work, so try adjusting like this:

PHP Code:
<?php
$link 
mysql_connect('localhost''username''password');
if (!
$link) {
die(
'Could not connect: ' mysql_error());
}

$sql 'CREATE DATABASE test2';
if (
mysql_query($sql$link)) {
echo 
"Database test2 created successfully\n";
} else {
echo 
'Error creating database: ' mysql_error() . "\n";
}
// Create tables in test2 database

mysql_select_db("test2"$link);

$sql "CREATE TABLE car (
title varchar(255) NOT NULL,
description varchar(255) NOT NULL,
priority tinyint(2) NOT NULL default '0'
) TYPE=InnoDB\n"
;

mysql_query($sql$link)
if (
mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create links table
$sql "CREATE TABLE links (
type varchar(20) NOT NULL default '',
resource varchar(255) NOT NULL,
) TYPE=InnoDB\n"
;

mysql_query($sql$link)
if (
mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create home table
$sql "CREATE TABLE home (
name varchar(255) NOT NULL,
title varchar(255) NOT NULL default '',
description varchar(255) NOT NULL default '',
) TYPE=InnoDB\n"
;

mysql_query($sql$link)
if (
mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create people table
$sql "CREATE TABLE people (
name varchar(20) NOT NULL default '',
address varchar(255) NOT NULL
resource varchar(255) NOT NULL,

) TYPE=InnoDB\n"
;

echo 
"car,home,links, & people tables in the relational database (".test2.") have been successfully created!\n";
mysql_query($sql$link)
if (
mysql_error()) echo __line__.") Error creating table: ".mysql_error();
mysql_close($link);//Close connection
?>
NOTE: I also took away the semi-colon at the end of all your statements as you don't need them in PHP and then threw in some error reporting.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #3 (permalink)  
Old 08-17-2007, 07:21 PM
heroine
Posts: n/a
thank you for the clarification...

I tried running the modified script but I keep getting the following error:

Parse error
: syntax error, unexpected T_IF in c:\wamp\test2.php

is it script problem or my side...?
thanks again...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #4 (permalink)  
Old 08-17-2007, 07:25 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 442
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Just a typo. BTW: line numbers matter, so if your system outputs them it's a great help to helping you.

PHP Code:
<?php
$link 
mysql_connect('localhost''username''password');
if (!
$link) {
die(
'Could not connect: ' mysql_error());
}

$sql 'CREATE DATABASE test2';
if (
mysql_query($sql$link)) {
echo 
"Database test2 created successfully\n";
} else {
echo 
'Error creating database: ' mysql_error() . "\n";
}
// Create tables in test2 database

mysql_select_db("test2"$link);

$sql "CREATE TABLE car (
title varchar(255) NOT NULL,
description varchar(255) NOT NULL,
priority tinyint(2) NOT NULL default '0'
) TYPE=InnoDB\n"
;

mysql_query($sql$link);
if (
mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create links table
$sql "CREATE TABLE links (
type varchar(20) NOT NULL default '',
resource varchar(255) NOT NULL,
) TYPE=InnoDB\n"
;

mysql_query($sql$link);
if (
mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create home table
$sql "CREATE TABLE home (
name varchar(255) NOT NULL,
title varchar(255) NOT NULL default '',
description varchar(255) NOT NULL default '',
) TYPE=InnoDB\n"
;

mysql_query($sql$link);
if (
mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create people table
$sql "CREATE TABLE people (
name varchar(20) NOT NULL default '',
address varchar(255) NOT NULL
resource varchar(255) NOT NULL,

) TYPE=InnoDB\n"
;

echo 
"car,home,links, & people tables in the relational database (".test2.") have been successfully created!\n";
mysql_query($sql$link);
if (
mysql_error()) echo __line__.") Error creating table: ".mysql_error();
mysql_close($link);//Close connection
?>

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
The Following User Says Thank You to TeraTask For This Useful Post:
HelloWorld (08-17-2007)
  #5 (permalink)  
Old 08-23-2007, 09:30 AM
molotov molotov is offline
Novice
Join Date: Jul 2007
Posts: 37
iTrader: (0)
molotov is on a distinguished road
It would be more efficient to concatenate SQL queries and only make one call to the DB. Instead of $sql = "whatever";, use
PHP Code:
$sql .= "query"

Notice the period (.) before the equals sign (.=). Also! Make double-sure that your queries are complete! Since they are being concatenated, you'll have to make sure to end each query with ';'.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #6 (permalink)  
Old 08-23-2007, 09:40 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
I'd also recommend to use:

Code:
begin;
-- Your code --
commit;
as part of your MySQL query

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #7 (permalink)  
Old 08-23-2007, 12:44 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 442
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Quote:
Originally Posted by HelloWorld View Post
I'd also recommend to use:

Code:
begin;
-- Your code --
commit;
as part of your MySQL query
Why? I'm not familiar with that.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #8 (permalink)  
Old 08-23-2007, 01:18 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
Originally Posted by TeraTask View Post
Why? I'm not familiar with that.
That's useful there's transaction... probably it shouldn't really matter in this case..?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
The Following User Says Thank You to HelloWorld For This Useful Post:
TeraTask (08-23-2007)
  #9 (permalink)  
Old 08-23-2007, 01:21 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 442
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Oh, for InnoDB -- ic now. I never work with InnoDB, just MyISAM.

Thanks.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
  #10 (permalink)  
Old 08-23-2007, 01:26 PM
molotov molotov is offline
Novice
Join Date: Jul 2007
Posts: 37
iTrader: (0)
molotov is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
That's useful there's transaction... probably it shouldn't really matter in this case..?

For one time uses such as creating these tables, it probably won't matter. It's a good habit to get into though.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Closed Thread


Thread Tools
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

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



All times are GMT -7. The time now is 07:38 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50