Toivo Talikka

Total Data Pty Ltd

Computer system installation, support and IT management consultancy

Forestville NSW Australia        tel 0410 532 923       email toivo@totaldata.biz

Toivo's TechTips Corner

submysql.php

The following sections document the MySQL functions in the subroutine library submysql.php. The functions are used in the module maintitem.php. To keep the program code tidy and easy to maintain, these are also the only MySQL routines in the module if one does not count the PHP command which extracts the results returned by SQL query, mysqli_fetch_array. Each function uses the MySQL 5 syntax with mysqli and the MySQL 4 call with mysql is included as a comment. If you want to run the code with MySQL 4, you need to change the references to mysqli_fetch_array to mysql_fetch_array in the module maintitem.php.

The module and the subroutine libraries are free software, licensed under the GPL and LGPL licences and part of the download.

Create Database Connection

The function creates a database connection to the MySQL server running in the localhost machine. The username and password of the MySQL user are hard coded in the call and they need to match an existing user in the database.

<?php
/* submysql.php */
/* mysql functions */
/* Copyright (C) 2005 Toivo Talikka                    */
/* ----------------------------------------------------*/
/* Modification history				  	*/
/* 09/12/05 TJT Initial version			  	*/
/*							*/
/* ----------------------------------------------------*/

/* ----------------------------------------------------*/
/* Functions					  	*/	
/* ---------						*/
/* sub_connect()	   			      	*/
/* sub_select_db($in_db, $in_connection)		*/
/* sub_connect_select($in_db)				*/
/* sub_query($in_query, $in_connection)			*/
/* -----------------------------------------------------*/

/* create database connection */
function sub_connect() {
// v4:  $out_connection = mysql_connect("localhost","sqluser","sssqql") or die ("Could not connect to the server: " . mysql_error());
   $out_connection = mysqli_connect("localhost","sqluser","sssqql") or die ("Could not connect to the server: " . mysqli_connect_error());
   return $out_connection;
}

Select Database

The function selects a database given as the first input parameter. The second input parameter is the database connection created by an earlier call to sub_connect() routine, described above.

/* select database */
function sub_select_db($in_db, $in_connection) {
// v4: $out_db = mysql_select_db($in_db, $in_connection) or die ("Could not select database: " . mysql_error());	
   $out_db = mysqli_select_db($in_connection, $in_db) or die ("Could not select database: " . mysqli_connect_error());	
   return $out_db;
}

Create Connection and Select Database

The function creates a database connection and selects the database given as the input parameter. The function returns the database connection to the calling module.

/* create database connection and select database */
function sub_connect_select($in_db2) {
// v4:  $my_connection = mysql_connect("localhost","sqluser","sssqql") or die ("Could not connect to the server: " . mysql_error());
// default dbname can be provided here as 4th parameter
  $my_connection = mysqli_connect("localhost","sqluser","sssqql") or die ("Could not connect to the server: " . mysqli_connect_error());
  if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
   }
// v4: $dummy_db = mysql_select_db($in_db2, $my_connection) or die ("Could not select database: " . mysql_error());
  $dummy_db = mysqli_select_db($my_connection, $in_db2) or die ("Could not select database: " . mysql_error());
  return $my_connection;
}

SQL Query

The function executes a SQL query and returns the result to the calling module.

/* execute SQL query and get result */
function sub_query($in_query, $in_connection) {
// v4:  $out_result = mysql_query($in_query, $in_connection) or die ("Could not execute query: <br>\n" . mysql_errno() . ": " . mysql_error() .
  $out_result = mysqli_query($in_connection, $in_query) or die ("Could not execute query: <br>\n" . mysql_errno() . ": " . mysql_error() . "<br>\n");
  return $out_result;
}
?>

Made With Cascading Style Sheets Valid CSS! Valid XHTML 1.1!