PHP and MySQL interactive function table Learning notes

Source: Internet
Author: User
Tags count numeric mysql numeric value php and php and mysql socket table name

Recently has been studying PHP and MySQL, the feeling of PHP and MySQL interaction functions are procedural, of course, there are mysqli extensions, object-oriented, Java and C # after writing more, then write PHP, some do not adapt, feeling back to the era of the school C. Today, I learned some functions and recorded them so that we can forget them later.

$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) <br/> ";

}

mysql_connect ("localhost", "username", "password" );

$tables = mysql_list_tables ("MyDatabase");

while (list ($table) = Mysql_fetch_row ($tables)) {

echo $table <br/>;

}

TD style= "BORDER:0;PADDING:0;" >array mysql_fetch_array (Resource result_set [, int result_type]) The values for TD style= "BORDER:0;PADDING:0;" >resource mysql_list_tables (String database [, Resource link_id])
Description The name of the function function detail Function description
Establishing a database connection Mysql_connect () Resource mysql_connect ([string hostname [:p ort] [:/path/to/socket] [, string username] [, string password]])

Example: $conn = @mysql_connect ("localhost", "username", "password") or dir ("Cannot connect to MySQL Server");

Close connection that must be displayed with this connection
Establishing a database connection Mysql_pconnect () Resource Mysql_pconnect ([string hostname [:p ort] [:/path/to/socket] [, string username] [, string password]])

Example: $conn = @mysql_pconnect ("localhost", "username", "password") or dir ("Cannot connect to MySQL Server");

You do not need to display a closed connection using this connection function, which is equivalent to using a connection pool
To close a database connection Mysql_close () $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 ();

 
Select database mysql_select_db () boolean mysql_select_db (String db_name [, Resource link_id])

$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");

query mysql mysql_query () resource mysql_query (string query, [resource link_id])

$linkId = @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");

$query = "SELECT * from MyTable";

$result = mysql_query ($query);

Mysql_close ();

If the SQL query executes successfully, the resource identifier is returned and false when it fails. Returns true if the update succeeds, or false
query mysql mysql_db_query () resource mysql_db_query (String database, string query [, Resource link_id])

$linkId = @mysql_connect ("localhost", "us Ername "," password ") or Die (" Cannot connect to MySQLServer ");

$query = "SELECT * from MyTable";

$result = mysql_db_query ("MyDatabase", $query);

Mysql_close ();

to make the code clear, it is not recommended to use this function to call
mysql_result () mixed mysql_result (Resource result_set, int row [, mixed field])

$query = "SELECT ID, name from MyTable order by name ";

$result = mysql_query ($query);

$c _id = mysql_result ($result, 0, "id");

$c _name = mysql_result ($result, 0, "name");

The simplest and least efficient data-fetching function
mysql_fetch_row () array mysql_fetch_row (Resource result_set)

$query = "SELECT ID, name from MyTable order by name";

$result = mysql_query ($query);

while ($id, $name) = Mysql_fetch_row ($result)) {

Echo ("Name: $name ($id) <br/>");

The

}

mysql_fetch_array () result_type are:

Mysql_assoc: Field names represent keys, field contents are values

Mysql_num: A numeric index array that operates as

Mysql_both as the Mysql_fetch_ros () function: That is, as an associative array and returned as a numeric index array. The default value of the Result_type.

Getting and displaying data MYSQL_FETCH_ASSOC () Array Mysql_fetch_assoc (Resource result_set)

Equivalent to calling Mysql_fetch_array (resource, MYSQL_ASSOC);

 
Getting and displaying data Mysql_fetch_object () Object Mysql_fetch_object (Resource Result_set)

$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) <br/>";

}

Same as Mysql_fetch_array () in operation
The selected record Mysql_num_rows () int mysql_num_rows (Resource result_set)

#query = "SELECT ID, name from MyTable where ID > 65";

$result = mysql_query ($query);

echo "has". Mysql_num_rows ($result). " The ID of the record is greater than 65 ";

is only useful when determining the number of records that a select query obtains.
Affected Records Mysql_affected_rows () int Mysql_affected_rows ([resource link_id])

$query = "Update MyTable set name= ' Cheneyfu ' where id>=5";

$result = mysql_query ($query);

The number of records with the echo "id greater than or equal to 5 is updated:". Mysql_affected_rows ();

This function gets the number of rows affected by the Insert,update or delete update statement
Get Database list information Mysql_list_dbs () Resource Mysql_list_dbs ([Resource link_id])

mysql_connect ("localhost", "username", "password");

$dbs = Mysql_list_dbs ();

echo "Databases: <br/>";

while (list ($db) = Mysql_fetch_rows ($dbs)) {

echo "$db <br/>";

}

 
Get database name Mysql_db_name () String Mysql_db_name (Resource Result_set, integer index) This function gets the name of the database at the specified index index in the returned result_set of Mysql_list_dbs ()
mysql_list_tables ()
Get database table name Mysql_tablename () String Mysql_tablename (Resource Result_set, integer index)

mysql_connect ("localhost", "username", "password");

$tables = Mysql_list_tables ("MyDatabase");

$count =-1;

while (+ + $count < Mysql_numrows ($tables)) {

Echo Mysql_tablename ($tables, $count). " <br/> ";

}

This function gets the table name at the specified index index in the returned result_set of Mysql_list_tables ()
Get field information Mysql_fetch_field () Object Mysql_fetch_field (resource result [, int field_offset])

mysql_connect ("localhost", "username", "password");

mysql_select_db ("MyDatabase");

$query = "SELECT * from MyTable";

$result = mysql_query ($query);

$fields = Mysql_num_fields ($result);

for ($count = 0; $count < $fieds; $count + +) {

$field = Mysql_fetch_field ($result, $count);

echo "<p> $field->name $field->type ($field->max_length) </p>";

}

A total of 12 object properties were returned for the object:

Name: Field name

Table: Tables in which the fields are located

Max_length: Maximum length of field

Not_null: 1 If the field cannot be null, otherwise 0

Primary_key: 1 If the field is the primary key, or 0

Unique_key: 1 If the field is a unique key, or 0

Multiple_key: 1 If the field is not unique, or 0

Numeric: If the field is a numeric value of 1, otherwise 0

Blob: If the field blob is 1, otherwise 0

Type: Data type of field

Unsigned: 1 If the field is unsigned, or 0

Zerofill: If the field is "0 fills" then 1, otherwise 0

Get the number of fields for a query Mysql_num_fields () Integer mysql_num_fields (Resource result_set)

$query = "SELECT ID, name from MyTable order by name";

$result = mysql_query ($query);

echo "The number of fields in this query is:". Mysql_num_fields ($result). <br/> ";

Returns the number of fields in query Result_set
Gets the field names of all the fields in the specified table Mysql_list_fields () Resource Mysql_list_fields (string database_name, string table_name [, Resource link_id])

$fields = Mysql_list_fields ("MyDatabase", "MyTable");

The number of fields in the table MyTable in the Echo database mydatabase: ". Mysql_num_fields ($fields)." <br/> ";

 
Gets the specified field options Mysql_field_flags () String Mysql_field_flags (Resource Result_set, integer field_offset)  
Gets the maximum length of the specified field Mysql_field_len () Integer Mysql_field_len (Resource result_set, integer field_offset)

$query = "SELECT name from MyTable";

$result = mysql_query ($query);

$row = Mysql_fetch_row ($result);

Echo Mysql_field_len ($result, 0). " <br/> ";

If Mysql_field_len ($reseult, 0) = 16777215

Then Numer_format (Mysql_field_len ($result)) equals 16,777,215

Get field Name Mysql_field_name () String Mysql_field_name (Resource result_set, int field_offset)

$query = "SELECT ID as Pkid, name from MyTable order by name";

$result = mysql_query ($query);

$row = Mysql_fetch_row ($result);

Echo mysql_field_name ($result, 0); Result:pkid

 
Get field type Mysql_field_type () String Mysql_field_type (Resource result_set, int field_offset)

$query = "SELECT ID, name from MyTable order by name";

$result = mysql_query ($query);

$row = Mysql_fetch_row ($result);

Echo Mysql_field_type ($result, 0); Result:int

 
Get the table name of the field Mysql_field_table () String mysql_field_table (Resource result_set, int field_offset)

$query = "SELECT ID as Pkid, name from MyTable order by name";

$result = mysql_query ($query);

$row = Mysql_fetch_row ($result);

Echo mysql_field_table ($result, 0); //

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.