Difference between revisions of "CSC220 mysqlSkel.php"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- <code><pre> </pre></code> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> Category:CSC220Category:phpCategory:mySql')
 
Line 2: Line 2:
 
----
 
----
 
<code><pre>
 
<code><pre>
 +
<?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 );
 +
 +
?>
 
</pre></code>
 
</pre></code>
  

Revision as of 11:46, 27 October 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')", $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 );

?>