<title>Managing a mySQL database table</title>
// Simple testscript for managing a mySQL database table.
// v0.21 13-Aug-2002, by G. Knauf <efash@gmx.net>
// Connecting to MySQL database
$conn = mysql_connect($host, $user, $pass) or die("Could not connect to MySQL server $host!\n");
// To use the database select it
mysql_select_db($dbase) or die("Could not select database $dbase!\n");

if (isset($_POST['submit'])) { 
    $result = mysql_query("INSERT INTO $table (name,pass) ".
            "VALUES ('".$_POST['username']."','".$_POST['userpass']."')");

$result = mysql_query("SELECT * FROM $table");
if (!$result) {
    echo "Error occured.\n";

$num = mysql_num_rows($result); 
$nfields = mysql_num_fields($result); 

print "<h3>Database <font color=red>$dbase</font></h3>\n";
print "<h3>Table <font color=green>$table</font> contains <font color=blue>$num</font> records with <font color=blue>$nfields</font> fields:</h3>\n";
print "<table border>\n<tr>";

for ($i=0; $i < $nfields; $i++) {
  echo '<th>'.mysql_field_name($result, $i).'</th>';
print "</tr>\n";

for ($i=0; $i < $num; $i++) {
  echo '<tr>';
  $r = mysql_fetch_row($result);

  for ($j=0; $j < count($r); $j++) {
    print "<td>$r[$j]&nbsp;</td>";

  print "</tr>\n";
print "</table>\n";

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<b>Add a new user:</b><br>
<input type="text" name="username" size="30"><br>
<b>Enter password for new user:</b><br>
<input type="password" name="userpass" size="30">
<p><input type="submit" name="submit" value="submit">
Published in: MySQL

Related snippets