CSC220 mysqlSkel.php

From dftwiki3
Revision as of 11:46, 27 October 2010 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut 16:45, 27 October 2010 (UTC)


<?php
// mysqlSkel.php
// D. Thiebaut

include 'accessinfo.php';

//--- connect to mysql server ---
$link = mysql_connect( $hostName, $userName, $password );
if ( ! $link ) 
  die( "Could not connect to server: " . $mysql_error() );

//--- select database ---
$db = mysql_select_db( $database, $link );
if ( ! $db ) 
  die( "Could not connect to database " . $database . ": "
       . mysql_error() );

//--- insert new user in table users ---
$newUser = "Joanna";
$query = sprintf( "INSERT INTO `users` (`userName`) VALUE ('%s')", $newUser );
$result = mysql_query( $query, $link );
if ( ! $result ) 
  printf( "Insertion of user %s unsuccessful\n", $newUser );

//--- list first row of table ---
$query = sprintf( "SELECT * from `users` LIMIT 1" );
$result = mysql_query( $query, $link );
if ( ! $result ) 
  die( "Invalid query (" . $query . "): " . mysql_error() );

if ( $row = mysql_fetch_array( $result, MYSQL_NUM ) ) {
  for ( $i=0; $i<count( $row ); $i++) 
    printf( "%s\t", $row[$i] );
  print "\n";
}

//--- close database ---
mysql_close( $link );

?>