PHP Database Operations

PHP has a set of functions that can interact with database such as MySQL. In order to use these functions, you need first uncomment the ";php_mysql.dll" line in the file "php.ini".
   ...
   ;extension=php_exif.dll
   extension=php_mysql.dll
   extension=php_mysqli.dll
   ;extension=php_oci8.dll
   ...

<?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.

:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • str_replace • strcmp
 • strpos • strrev
 • strrpos • strtr
 • substr • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expression • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2020  Terms of Use | Home