Web EndMemo

PHP Database Operations


PHP has a set of functions that can interact with database such as MySQL.
<?PHP
	$connect=mysql_connect("$host","$user","$passwd") or die (mysql_error());
	$dbs = mysql_select_db($dbs,$connect) or die (mysql_error());
	$sql = "SELECT name,alias,city FROM `colleague` WHERE name='John'";
	$q_result = mysql_query("$sql", $connect) or die (mysql_error());
	if (mysql_num_rows($q_result) == 0)
	{
		echo "NO Record Found.";
	}
	while($row = mysql_fetch_array($q_result,MYSQL_BOTH) )
	{
		$name = $row[0];
		$alias = $row[1];
		$city = $row[2];
	}
	mysql_close($connect)
?>

Before PHP database query, a connection must be successfully created, and database selected.

PHP Mysql functions List:
Function Description
mysql_select_db($dbs[, $connect]) Select database for use
mysql_close($connect) Close the connection
mysql_query($query[, $connect]) Query the database, get results
mysql_num_rows($q_result) Get row number of a query result
mysql_connect($host,$user,$passwd) Opens a connect to server
mysql_drop_db($dbs,$connect) Delete database
mysql_create_db($dbs,$connect) Create database
mysql_affected_rows($connect) Number of rows affected after an operation
mysql_num_fields($q_result) Get field number of a query result
mysql_change_user($user, $passwd[,$dbs,$connect]) Change MySQL user
mysql_data_seek($q_result, $row_number) Fetching data from the specified row
mysql_errno($connect) Get error ID
mysql_error($connect) Get error message
mysql_fetch_array($q_result[,$type]) Get query result as associative array. $type includes MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH (default)
mysql_fetch_lengths($q_result) get each field length of a result set
mysql_fetch_object($q_result[,$type]) Get query result as an object
mysql_field_row($q_result) Get query result as an enumerated array
mysql_field_name($q_result,$field_index) Get name of enumerated field
mysql_fetch_field($q_result,[,$field_offset]) Get a field as an object
mysql_field_seek($q_result,$field_offset) Set result pointer to the field offset
mysql_field_table($q_result,$field_offset Get table name of the field
mysql_field_type($q_result,$field_offset) Get types of the field
mysql_field_flags($q_result,$field_offset) Get flags of the enumerated field, e.g. NULL, AUTO_INCREMENT
mysql_field_len($q_result,$field_offset) Get length of the enumerated field
mysql_free_result($q_result) Release the memory used by result set
mysql_insert_id($connect) get AUTO_INCREMENTED ID of INSERT operation
mysql_list_fields($dbs,$table[,$connect]) Get result ID in mysql_field functions
mysql_list_dbs($connect) Get result pointer of databases on mysqld
mysql_list_tables($dbs[, $connect]) Get result pointer of tables in database
mysql_pconnect($host,$user,$passwd) Create a permanent connect to the server database. mysql_close() can't close it
mysql_result($q_result, $row_id, $field) Get single filed result.