TIME NOW
World current time now,
CALENDAR
Calendar monthly, yearly
login GOLD PRICE, RATE
login HOROSCOPE
login DICTIONARIES, LISTS
login SCIENCE EDUCATION RELIGION
login FINANCE, BUSINESS
login WORLD COUNTRIES, WIKI

PHP scripts, code, examples, tutorial

Previous articlePage bottomNext article  ALL TOPICS

PHP. MYSQL examples: select insert delete create stats

 
<a href="/<?php echo $_SERVER['PHP_SELF'] ; ?>?op=0"> 0 info   </A><BR>
<a href="/<?php echo $_SERVER['PHP_SELF'] ; ?>?op=1"> 1 select </A><BR>
<a href="/<?php echo $_SERVER['PHP_SELF'] ; ?>?op=2"> 2 insert </A><BR>
<a href="/<?php echo $_SERVER['PHP_SELF'] ; ?>?op=3"> 3 delete </A><BR>
<?php
$db = "test";


$table1 = "test1";
$table2 = "test2";

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';


$db_selected = mysql_select_db($db, $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}

$sql = "CREATE TABLE IF NOT EXISTS `$table1` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`data` VARCHAR(100),
`created` TIMESTAMP DEFAULT NOW()
);";

$result = mysql_query($sql) or die (mysql_error());

$sql = "CREATE TABLE IF NOT EXISTS `$table2` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`data` VARCHAR(100),
`created` TIMESTAMP DEFAULT NOW(),
`id_parent` INT
);";

$result = mysql_query($sql) or die (mysql_error());

echo "<pre>";




if ($_GET['op'] == 1){

echo "select<BR>\n";

$sql = "SELECT * FROM $table1";
$result = mysql_query($sql) or die (mysql_error());

while ($row = mysql_fetch_assoc($result)) {
//print_r($row);

echo "{$row['id']}, {$row['data']}, {$row['created']} <br>\n";

$sql1 = "SELECT * FROM $table2 WHERE `id_parent` = '{$row['id']}'";
$result1 = mysql_query($sql1) or die (mysql_error());
while ($row1 = mysql_fetch_assoc($result1)) {

echo "\t\t{$row1['id']}, {$row1['data']}, {$row1['created']}, {$row1['id_parent']}  <br>\n";

}

} // while

} elseif ($_GET['op'] == 2){ // op1

echo "insert\n";

for ($i = 1; $i < 5 ; $i ++){

$rand = md5 (rand (1, 999));
$sql = "INSERT INTO `$table1` SET `data` = '$rand' ";
echo "$sql\n";
$result = mysql_query($sql) or die (mysql_error());



$id = mysql_insert_id ();
for ($y = 1; $y < 5 ; $y ++){
$sql = "INSERT INTO `$table2` SET `data` = '$rand', `id_parent` = '$id' ";
echo "\t\t $sql \n"  ;
$result = mysql_query($sql) or die (mysql_error());
}

echo "END\n\n"  ;

}      

}elseif ($_GET['op'] == 3){

echo " trynimas\n" ;
$sql = "DELETE FROM $table1";
$result = mysql_query($sql);
echo  mysql_affected_rows ($link) . "\n";
$sql = "DELETE FROM $table2";
$result = mysql_query($sql);
echo  mysql_affected_rows ($link) . "\n";


}else{
echo "stats\n" ;

$sql = "SHOW TABLES FROM $db";
$result = mysql_query($sql);

while ($row = mysql_fetch_row($result)) {
//print_r ($row);
echo "Table: {$row[0]}\n<BR>";
}

echo "<HR>TABLE 1 \n";
$sql = "SHOW COLUMNS FROM $table1";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
print_r($row);  echo "<br>" ;
}

echo "<HR>TABLE 2 \n";
$sql = "SHOW COLUMNS FROM $table2";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
print_r($row);  echo "<br>" ;
}

}

mysql_close($link);

?>
 
Previous articlePage topNext article  ALL TOPICS



 Use username: Guest, Anonymous, Programmer






QUOTES:
I think it a very happy accident.
Miguel de Cervantes
I can't afford a new car. As for used cars, well, I already have one of those.
Paul's Grandfather
Age cannot wither her, nor custom staleHer infinite variety other women cloyThe appetites they feed, but she makes hungryWhere most she satisfies.
William Shakespeare