Difference between revisions of "CSC220 mysqlSkel.php"

From dftwiki3
Jump to: navigation, search
 
Line 21: Line 21:
 
//--- insert new user in table users ---
 
//--- insert new user in table users ---
 
$newUser = "Joanna";
 
$newUser = "Joanna";
$query = sprintf( "INSERT INTO `users` (`userName`) VALUE ('%s')", $newUser );
+
$query = sprintf( "INSERT INTO `users` (`userName`) VALUE ('%s')", addslashes( $newUser ) );
 
$result = mysql_query( $query, $link );
 
$result = mysql_query( $query, $link );
 
if ( ! $result )  
 
if ( ! $result )  

Latest revision as of 09:16, 19 November 2010

--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')", addslashes( $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 );

?>