PHP MySQL
1. Close MySQL connection
mysql_close($link);
2. Connect MySQL
mysql_connect(“IP address”, “user”, “password”);
mysql_pconnect(IP address”, “user”, “password”);
3. Create new db
$link = mysql_pconnect();
mysql_create_db(“test”,$link);
4. Direct data and get data
//mysql_data_seek() direct data
//mysql_fetch_row() get data and direct to next record
$link = mysql_pconnect(“localhost”,”admin”,”1234”);
$mysql_select_db(“people”, $link);
$result = mysql_query(“select * from male”, $link);
mysql_data_seek($result, 0);
list($name, $birthday) = mysql_fetch_row($result);
5. Free the memory
mysql_free_result(resource result);
6. List all table of the directly database
mysql_list_tables(string database, resource[link_identifier]);
7. Return numbers field
Int mysql_num_fields(resource result);
8. Return numbers row
Int mysql_num_row(resource result);
9. Rum command
mysql_query(string query, resource [ link_identiier]);
10. Connect to last database or directly database connection
$link = mysql_pconnect();
Mysql_select_db(string database_name, resource [ link_identifier]);
11. Return record of last command (insert, update, delete)
//If the command is delete and not has ‘where’ expression, it will return 0.
Mysql_affected_rows(resource [link_identifier]);
12. Directly database run query
mysql_db_query(string database, string query, resource [link_identifier]);
//Notes: PHP 4.0.6 after please use mysql_select_db and mysql_query().
13. Delete database
Mysql_drop_db(string database_name, resource [link_identifier]);
14. Return error number
Int mysql_errno(resource [link_identifier]);
Example
<?
mysql_connecnt();
echo mysql_errno().”: “.mysql_error().”<BR>”;
?>
15. Return error message
string mysql_error(resource [link_identifire]);
16. Get data array
mysql_fetch_array(resource result, int [result_type]);
/* result_type
MYSQL_ASSOC : associative array
MYSQL_NUM : enumerated array
MYSQL_BOTH : associative array and enumerated array
*/
Example
$rows = mysql_fetch_array($result,MYSQL_ASSOC);
Echo $rows[‘name’];
$rows = mysql_fetch_array($result,MYSQL_NUM)l
Echo $rows[1].$rows[2];
17. Return database field info.
Object mysql_fetch_field(resource result, int [field_offset]);
Object properties
Name explain
table database name
max_length max length of row
not_null if that field is not accept null value, it will be return true(1)
primary_key if that field is a primary key, it will be return true
unique_key if that field is a unique key, it will be return true
multiple_key if that field is not a unique key, it will be return true
numeric if that field is number data type(int, float, double), it will be
return true
blob if that field is blob data type, it will be return true
type ……….
unsigned if that field is unsigned property, it will be return true
zerofill if that field is zerofill property, it will be return true
18. Return length of last mysql_fetch_* function
Array mysql_fetch_lengths(resource result);
19. Get data with object
Object mysql_fetch_object(resource result, int [result_type]);
//Result_type(MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH)
Example
<?
while($row = mysql_fetch_object($result))
{
echo $row->user_id;
echo $row->fullname;
}
20. Return data with enumerated array
Array mysql_fetch_row(resource result);
//Notes: it will return a data pointer and go to next data field
21. Return data with associative array
Array mysql_fetch_assoc(resource result);
22. Return field name
String mysql_field_name(resource result, int field_index);
//Notes: field_index present who number field.
23. Directly move pointer
Int mysql_field_seek(resource result, int field_offset);
//field_offset is a result position
24. Return table name
String mysql_field_table(resource result, int field_offset);
25. Return data type with that field
String mysql_field_type(resource result int field_offset);
26. Return field property
String mysql_field_flags(resource result, int field_offset);
Field property:
Not_null, primary_key, unique_key, blob, unsigned, zerofill, binary
Enum, auto_increment, timestamp
27. Return field length
Int mysql_field_len(resource result, int field_offset);
28. Return before insert query of AUTO_INCREMENT field ID
Int mysql_insert_id(resource [link_identifier]);
29. Return a double pointer of direct to data pointer
Resource mysql_list_fields(string database_name, string table_name, resource
[link_identifier]);
//Notes: if fail, it will return -1
30. Return a pointer of require can use database name
Resource mysql_list_dbs(resource [link_identifier]);
//Notes: use mysql_tablename() function to read a data.
31. Return a pointer of require database all table directly
Resource mysql_list_tables(string database, resource [link_identifier]);
//Notes: use mysql_tablename() function to read a data.
32. Get how fields of result
Int mysql_num_fields(resource result);
33. Get how row of result
Int mysql_num_rows(resource result);
34. Get result of one field
Mixed mysql_result(resource result, int row, mixed field);
//Notes: please use mysql_fetch_rows(), mysql_fetch_array(),
mysql_fetch_object() function, there is faster mysql_result() function.
35. Run query but not this time
Resource mysql_unbuffer_query(string query [resource link_identifier]);
Example
<?
$res1 = mysql_unbuffer_query(“SELECT * From mouse_type”);
$res2= mysql_unbuffer_query(“SELECT * From keyboard_type”);
$res3 = mysql_unbuffer_query(“SELECT * From monitor_type”);
Switch($search)
{
Case “mouse”:
$row = mysql_fatch_row($res1);
//…break;
Case “keyboard”:
$row = mysql_fetch_row($res2);
//…break;
Case “monitor”:
$row = mysql_fetch_row($res3);
//……break;
Default: echo “Invalid search paramemter!”; break;
}
36. Get table name
String mysql_tablename(resource result, int i);
Result = mysql_list_tables()
i = table key
Example
<?
Mysql_connect();
$result =mysql_list_tables(“book”);
$i = 0;
While($i < mysql_num_rows($result))
{
$tb_names[$i] = mysql_tablename($result m $i);
Echo $tb_names[$i].”<BR>;
$i++;
}
?>
37. Process escaped character
String mysql_escape_string(string unescaped_string);
Notes: % and _ will not process
38. Get MySQL client library version
String mysql_get_client_info( void);
Example
<?
Echo mysql_get_client_info();
?>
39. Get MYSQL host name and connection type
String mysql_get_host_info([resource link_identifier]);
40. Get MYSQL connection protocol
Int mysql_get_proto_info([resource link_identifier]);
41. Get MYSQL server version
Int mysql_get_server_info([resource link_identifier]);