PHP Operation MySQL Summary

Source: Internet
Author: User

<?php

Connecting to a database

Set the document display encoding

Header (' Content-type:text/html;charset=utf-8 ');

if ($con =mysql_connect (' localhost ', ' root ', ' 1234 ')) {//Return connection identifier

Echo ' Connect success <br> ';

}else{

Echo ' Connection failed <br> ';

}

Select Database

if (mysql_select_db (' mydbtest ')) {

echo "Select database Success <br>";

}else{

Echo ' Select database failed <br> ';

}

Set encoding

mysql_query (' Set names UTF8 ');


#插入数据

/*

$sql = ' INSERT into Mytab (name) values (\ ' huge\ ') ';

if (mysql_query ($sql)) {//returns TRUE if SQL statement is inserted data, otherwise false

echo ' Insert data successfully! <br> ';

}else{

Echo ' Insert failed, Reason: '. mysql_error ();

}

*/


#查询

4fetch function Mysql_fetch_row (), mysql_fetch_array (), Mysql_fetch_object (), MYSQL_FETCH_ASSOC ()


#mysql_fetch_row每次获取一条数据, an indexed array is generated, that is, the subscript is a number

/*

$sql = ' select * from Mytab ';

$query =mysql_query ($sql);//When Mysql_query executes a SELECT statement, the resource identifier is returned, and the resource identifier is the address that is returned to the memory

Mysql_fetch_row (), each time it is executed, takes one piece of data from the resource, which is the result set, and returns it as an array, which returns an empty result when the current time is taken to the last piece of data.

Traversing tables with mysql_fetch_row ()

while ($row =mysql_fetch_row ($query)) {

Print_r ($row);

}

*/


#mysql_fetch_array (), fetch a piece of data, IBID., returns the indexed array and associative array by default, although it is more convenient to use, but less efficient than mysql_fetch_now ().

#第一个参数, the resource identifier, the second parameter is the array output form, has the MYSQL_ASSOC, only outputs the associative array; mysql_num, only the output index array; Mysql_both, so the array + associative array

/*

$sql = ' select * from Mytab ';

$query =mysql_query ($sql);


Default

Echo ' <br> default: ';

$row =mysql_fetch_array ($query);

Print_r ($row);


Mysql_num, indexed array

Echo ' <br> indexed array: ';

$row =mysql_fetch_array ($query, mysql_num);

Print_r ($row);


MYSQL_ASSOC, associative array

Echo ' <br> associative array: ';

$row =mysql_fetch_array ($query, MYSQL_ASSOC);

Print_r ($row);

*/


#mysql_fetch_assoc, the second parameter in the same Mysql_fetch_array is selected as Mysql_assoc

/*

$sql = ' select * from Mytab ';

$query =mysql_query ($sql);

$row =mysql_fetch_assoc ($query);

Print_r ($row);

*/


#mysql_fetch_object, returns a row of data and converts it to an object

/*

$sql = ' select * from Mytab ';

$query =mysql_query ($sql);

$obj =mysql_fetch_object ($query);

Print_r ($obj);

Echo ' <br> ';

Echo $obj->name;

*/


#mysql_num_rows, the number of rows that returned the result

/*

$sql = ' select * from Mytab ';

$query =mysql_query ($sql);//sql statement is incorrect returns null

Echo $query;

Echo ' <br> ';

echo mysql_num_rows ($query);

Echo ' <br> ';

if ($query &&mysql_num_rows ($query)) {

Looping through tables

while ($row =mysql_fetch_row ($query)) {

Print_r ($row);

Echo ' <br> ';

}

}

*/


#mysql_result, returns the value of a field in the result set that sets the row and column

/*

$sql = ' SELECT count (*) from Mytab ';

$sql = ' select * from Mytab ';

$query =mysql_query ($sql);

Echo mysql_result ($query, 0);//Total three parameters, first resource variable result set, second row number, third column offset or column name

Echo mysql_result ($query, 0, 1);//The line number and column number are all starting from 0

Echo mysql_result ($query, 3, ' Name ');

*/


Mysql_affected_rows (), returns the number of rows affected after the execution of the SQL, and the parameter is the connection identifier

$sql = ' INSERT into Mytab (name) values (\ ' huge1\ ') ';

if (mysql_query ($sql)) {

echo ' Insert success! ‘;

The number of rows affected in the echo ' <br> database is: ';

echo mysql_affected_rows ($con);//If the function modifies the same data as the original data, the number of rows affected is 0, and the function returns only the number of rows affected by the last term in the mysql_query call database

}


Close a database connection and close a non-persistent database connection

Mysql_close ($con);

?>


This article is from "Tiger Brother's Blog" blog, please be sure to keep this source http://7613577.blog.51cto.com/7603577/1531693

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.