Archive for the ‘Programming Languages’ Category

How to Create Multiple Tables in One Statement with MySQL & PHP

Monday, August 17th, 2009

If you are working on a project that force you to create multiple tables in one statement with MySQL & PHP because it is more efficient with your web server, you can try this code out:

PHP Code for Making Multiple Tables

1
2
3
4
5
6
7
8
<?php
$qry = "
CREATE TABLE tablename(fieldname fieldproperties) TABLE TYPE;
CREATE TABLE tablename2(fieldname fieldproperties) TABLE TYPE;
CREATE TABLE tablename3(fieldname fieldproperties) TABLE TYPE;
CREATE TABLE tablename4(fieldname fieldproperties) TABLE TYPE;
";
?>

(more…)

Insert Into Select PHP

Sunday, July 26th, 2009

INSERT INTO SELECT php statement is used to insert new records into a database table from php scripts.

INSERT INTO SELECT Syntax

insert into table (col1, col2) select 1, field.id from table 2

In PHP, you just have to insert the above statement into mysql_query command just like below:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("my_db", $con);
 
mysql_query("insert into i1(pin,dist2uc, dist2hw) select e.pin, d.dist2uc,d.dist2hw from d1 e, dist2unh d where e.pin = d.pin;");
 
mysql_close($con);
?>

Hope it helps.