Home >Backend Development >PHP Tutorial >MYSQL common functions in PHP are necessary to operate the database under PHP
1. mysql_connect()-Establish a database connection
Format:
resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
Example:
$conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
Note: The connection must be closed when using this connection
2. mysql_pconnect()-Establish a database connection
Format:
resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
Example:
$conn = @mysql_pconnect("localhost", "username ", "password") or dir("Cannot connect to Mysql Server");
Explanation: Using this connection function does not require explicit closing of the connection. It is equivalent to using a connection pool
3. mysql_close()-Close the database connection
Example:
$conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
@mysql_select_db("MyDatabase") or die("Cannot select this database, Or the database does not exist");
echo "You have connected to the MyDatabase database";
mysql_close();
4. mysql_select_db()-Select database
Format:
boolean mysql_select_db(string db_name [, resource link_id])
Example:
$conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
@mysql_select_db("MyDatabase") or die("Cannot select this database or database Does not exist");
5. mysql_query()-Query MySQL
Format:
resource mysql_query (string query, [resource link_id])
Example:
$linkId = @mysql_connect("localhost", "username", "password" ) or die("Cannot connect to Mysql Server");
@mysql_select_db("MyDatabase") or die("This database cannot be selected, or the database does not exist");
$query = "select * from MyTable";
$ result = mysql_query($query);
mysql_close();
Note: If the SQL query is executed successfully, the resource identifier is returned, and FALSE is returned if it fails. If the update is executed successfully, TRUE is returned, otherwise FALSE is returned.
6. mysql_db_query()-Query MySQL
Format:
resource mysql_db_query(string database, string query [, resource link_id])
Example:
$linkId = @mysql_connect(" localhost", "username", "password") or die("Cannot connect to MysqlServer");
$query = "select * from MyTable";
$result = mysql_db_query("MyDatabase", $query);
mysql_close( ; query = "select id, name from MyTable order by name";
$result = mysql_query($query);
for($count=0;$count<=mysql_numrows($result);$count++)
{
$c_id = mysql_result($result, 0, "id");
$c_name = mysql_result($result, 0, "name");
echo $c_id,$c_name;
}
Explanation: The simplest and least efficient data Get function
8, mysql_fetch_row() - Get and display data
Format:
array mysql_fetch_row (resource result_set)
Example:
$query = "select id, name from MyTable order by name";
$result = mysql_query($query );
while (list($id, $name) = mysql_fetch_row($result)) {
echo("Name: $name ($id)
");
}
Description: The function is from result_set Get the entire row of data and place the values in an indexed array. Usually the list() function is used to obtain and display data. Format:
array mysql_fetch_array (resource result_set [, int result_type])
Example:
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["id"];
$name = $row["name "];
echo "Name: $name ($id)
";
}
Another example:
$query = "select id, name from MyTable order by name";
$result = mysql_query( $query);
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
$id = $row[0];
$name = $row[1];
echo "Name: $name ($id)
";
}
Explanation:
result_type values are:
MYSQL_ASSOC: The field name represents the key, and the field content is the value
MYSQL_NUM: Numeric index array, the operation is the same as the mysql_fetch_ros() function
MYSQL_BOTH: That is, as Associative arrays are returned as numerically indexed arrays. The default value of result_type.
10. mysql_fetch_assoc()-Get and display data
Format:
array mysql_fetch_assoc (resource result_set)
Equivalent to calling mysql_fetch_array(resource, MYSQL_ASSOC);
11. mysql_fetch_object()-Get and display data
Format:
object mysq l_fetch_object( resource result_set)
Example:
$query = "select id, name from MyTable order by name";
while ($row = mysql_fetch_object($result)) {
$id = $row->id;
$name = $row->name;
echo "Name: $name ($id)
";
}
Description: Returns an object, which is the same as mysql_fetch_array() in operation
12. mysql_num_rows()- The number of selected records
Format:
int mysql_num_rows(resource result_set)
Example:
query = "select id, name from MyTable where id > 65";
$result = mysql_query($query);
echo " There are ".mysql_num_rows($result)." records whose IDs are greater than 65";
Note: This is only useful when determining the number of records obtained by the select query.
13. mysql_affected_rows() - The number of records affected by Insert, update, delete
Format:
int mysql_affected_rows([resource link_id])
Example:
$query = "update MyTable set name='CheneyFu' where id> =5";
$result = mysql_query($query);
echo "The number of updated records with names whose ID is greater than or equal to 5:".mysql_affected_rows();
Description: This function obtains the update statement affected by INSERT, UPDATE or DELETE Number of rows affected
14, mysql_list_dbs()-Get database list information
Format:
resource mysql_list_dbs([resource link_id])
Example:
mysql_connect("localhost", "username", "password");
$dbs = mysql_list_dbs();
echo "Databases:
";
while (list($db) = mysql_fetch_rows($dbs)) {
echo "$db
";
}
Description : Display all database names
15. mysql_db_name()-Get the database name
Format:
string mysql_db_name(resource result_set, integer index)
Description: This function gets the database name located in the specified index index in the result_set returned by mysql_list_dbs()
16. mysql_list_tables()-Get the list of database tables
Format:
resource mysql_list_tables(string database [, resource link_id])
Example:
mysql_connect("localhost", "username", "password");
$tables = mysql_list_tables( "MyDatabase");
while (list($table) = mysql_fetch_row($tables)) {
echo "$table
";
}
Description: This function gets the table names of all tables in the database
17. mysql_tablename()-Get the name of a database table
Format:
string mysql_tablename(resource result_set, integer index)
Example:
mysql_connect("localhost", "username", "password");
$tables = mysql_list_tables( "MyDatabase");
$count = -1;
while (++$count < mysql_numrows($tables)) {
echo mysql_tablename($tables, $count)."
";
}
Description: This function gets the table name located at the specified index in the result_set returned by mysql_list_tables()
18. mysql_fetch_field()-Get field information
Format:
object mysql_fetch_field(resource result [, int field_offset])
Example:
mysql_connect ("localhost", "username", "password");
mysql_select_db("MyDatabase");
$query = "select * from MyTable";
$result = mysql_query($query);
$counts = mysql_num_fields($ result);
for($count = 0; $count < $counts; $count++) {
$field = mysql_fetch_field($result, $count);
echo "
$field->name $field ->type ($field->max_length)
";