Walkthrough of creating a database and some tables

(only first time, enables 'db_user' on a local connection to do anything)
GRANT ALL PRIVILEGES ON . TO 'db_user'@'localhost';

create database junk; show databases;
= ( shows the database created)

connect junk;
CREATE TABLE `foo` ( `id` INT UNSIGNED NOT NULL, `name` CHAR(32), PRIMARY KEY (`id`));
show tables;
( shows we now have the characteristic_sets )
insert into foo values ("some name");
select * from foo


Now try this php script...
<html>
<?php
$user="db_user";
$password="";
$database="junk";
$junk_table = "bar";

$link = mysql_connect("localhost", $user, $password)
    or die('Could not connect: ' . mysql_error());

@mysql_select_db($database)
    or die( "Unable to select database" . mysql_error());

$result = @mysql_query("drop table $junk_table");

if (!$result) {
   echo("no table to drop<BR>");
}

$result = @mysql_query("CREATE TABLE $junk_table ( `id` INT UNSIGNED NOT NULL, `name` CHAR(32), PRIMARY KEY (`id`))")
    or die("can't create table $junk_table" . mysql_error());

$result = @mysql_query("show columns from $junk_table")
    or die('Query failed: ' . mysql_error());
?>
Columns in '<?echo $junk_table?>'
<TABLE border>
<?

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "<tr>";
   foreach ($line as $col_value) {
       echo "<td>$col_value</td>";
   }
   echo "</tr>";
}
?>
</TABLE>
<?
/* insert some values in
 */
for ($i = 0; $i < 10; $i++) {
   $result = @mysql_query("INSERT INTO $junk_table VALUES (\"$i\", \"Entry $i\")")
     or die ('insert failed: ' .mysql_error());
}

/* read them back out
 */
   $result = @mysql_query("select * from $junk_table")
     or die ('select failed: ' .mysql_error());
?>

Data in <?echo $junk_table?>
<table border>
<?
   /* dump out the table
    */
   while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "<tr>";
   foreach ($line as $col_value) {
       echo "<td>$col_value</td>";
   }
   echo "</tr>";
}
?>
</TABLE>
</html>

-- MattWalsh - 01 Oct 2006

Topic revision: r2 - 16 Feb 2007 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback