Common extension functions for PHP to link to MySQL, php to link to mysql_PHP tutorial

WBOY
Release: 2016-07-13 10:16:14
Original
829 people have browsed it

Common extension functions for PHP to link to MySQL, PHP to link to mysql

1. PHP database connection and basic operations

MySQL uses a 'client/server' architecture. Using the MySQL extension function installed by PHP and directly using the client software area to access the MySQL database server have the same principle. You need to send SQL commands to the MySQL management system and then return the results to the user.

In PHP, SQL is divided into two categories (see SQL statement classification): one is a DQL statement that returns a result set, such as select/desc table name. After execution, PHP needs to process the result set; the other is no result. Set, such as DML, DDL, etc., but the successful execution of the DML statement will have an impact on the records in the data table.

<&#63;php<br />//连接数据库,常用参数是主机名、用户名和密码<br />$link = mysql_connect('localhost','root','123456');<br />//判断是否连接成功<br />if(!$link)<br />{<br />die('连接失败'.mysql.error()); //连接成功返回资源标识符,失败返回false,mysql_error显示错误信息<br />}<br /><br />//选择数据库,mysql_error()只在调试中使用,再部署项目时就不要了,不然会泄露数据库信息<br />mysql_select_db('test') or die('选择数据库失败'.mysql_error());<br /><br />//mysql_query()可以设置字符集和执行SQL语句<br />mysql_query('set names utf-8');<br />$sql = 'insert into test(id,name) values("1","dwqs")';<br />$result = mysql_query($sql);  //执行sql返回结果集<br /><br />//处理结果集,insert属于DML,会对表的记录有影响<br />if($result && mysql_affected_rows() > 0)<br>{<br>//mysql_insert_id()返回最后一条新纪录的auto_increment值<br>echo '插入数据成功'.mysql_insert_id().'<br/>';<br>}<br>else<br>{<br>echo '插入数据失败,错误号:'.mysql_errno().'错误信息:'.mysql_error().'<br/>';<br>}<br><br>//关闭连接<br>mysql_close($link);<br>?>
Copy after login

2. PHP processing select query result set

Executing the select statement in PHP returns a result set, which can be used to process each field

$result = mysql_query('select * from test');<br>//获取记录行的个数<br>$rows = mysql_num_rows($result);<br>//获取字段个数,即数据列<br>$cols = mysql_num_fields($result);
Copy after login

If you need to access the data in the result set, you can use one of the following four functions (all take the result set resource identifier as a parameter, and automatically return the next record, and return false at the end of the table)

1. mysql_fetch_row(): This function returns a result record and saves it as an ordinary index data

2. mysql_fetch_assoc(): Get a row from the result set and save it as related data

3. mysql_fetch_array(): Get a row from the result set as an associative array, a numeric array, or both. You can use MYSQL_ASSOC (associative array form), MYSQL_NUM (index array form) and MYSQL_BOTH as the second parameter to specify the returned data form.

4. mysql_fetch_object(): Get a row from the result set as an object, and each field is accessed in object mode.

Recommendation: Do not use mysql_fetch_array() without special requirements. You can use mysql_fetch_row() or mysql_fetch_assoc() to achieve the same function with high efficiency.

There are also three commonly used functions related to result sets

5. mysql_data_seek(int $num): Move the pointer of the internal result. $num is the number of rows of the new result set pointer you want to set.

6. mysql_fetch_lengths(resource <font face="NSimsun">$result</font>): Get the length of each output in the result set

7. mysql_result(resource <font face="NSimsun">$result</font>, int <font face="NSimsun">$row[,mixed $field]</font>): Returns the contents of a unit in the MySQL result set. The field parameter can be the offset or field name of the field, or the field table point field name (tablename.fieldname). If a column is given an alias ('select foo as bar from...'), the alias is used instead of the column name. Calling mysql_result() cannot be mixed with other functions that process result sets.

php application for connecting mysql function

The mysql_fetch_array() function fetches a row from the result set as an associative array, a numeric array, or both.
Returns an array based on the rows taken from the result set, or false if there are no more rows.

mysql_fetch_array(data,array_type)
Parameter data: optional. The specification specifies the data pointer to be used. This data pointer is the result of the mysql_query() function.
Parameter: array_type optional. Specifies what kind of results are returned. Optional values ​​for this parameter: MYSQL_ASSOC - associative array
MYSQL_NUM - numeric array
MYSQL_BOTH - default. Produces both associative and numeric arrays.
Note: mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing data in an array as a numerical index, you can also store data as an associative index, using the field name as the key.

Example:
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * from Person WHERE Lastname='Adams'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_array($result));

mysql_close($con);
?>
The output is similar to:
Array
(
[0] => Adams
[LastName] => Adams
[1] => John
[FirstName] => John
[2] => London
[City] => London
)
//////////////// ///////
The mysql_fetch_assoc() function fetches a row from the result set as an associative array.
Returns an associative array based on the rows taken from the result set, or false if there are no more rows.
mysql_fetch_assoc(data)
Parameter: data (required) The data pointer to be used. The data pointer is the result returned from mysql_query().

Note: mysql_fetch_assoc() is exactly the same as using mysql_fetch_array() plus the second optional parameter MYSQL_ASSOC. It just returns an associative array. This is also how mysql_fetch_array() initially works.
Tip: If you need a numeric index in addition to a relational index, use mysql_fetch_array().
Note: The field names returned by this function are case-sensitive.

An example is as follows:
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
{
die('Could not connect: ' . mysq...the rest of the text>>

For php connection mysql function

Since I didn’t see the complete code, I only tried to answer the code I saw as follows:

1. Notice: Undefined variable: db in C:\xampp\htdocs\shop\files\mysql. php on line 5
Warning: Undefined variable db (it’s not clear which line of code it is on line 5).

From the known code point of view, the reason for this error message should be that you referenced a variable (db) defined outside the function body in the function body. From the code point of view, you actually did not notice it. For the variable The problem of incorrect application of scope (global, local).

To put it simply, db cannot be found in the function select_mycx.

Solution:

(1). Pass it in with parameters.

function select_mycx($table,$by,$select_str,$number,$db)
{
.....
}

(2). Define global variable references in the parameter body:

function select_mycx($table,$by,$select_str,$number)
{
global $db;

... .
}

2.Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\shop\files\mysql.php on line 5

This error is actually caused by the above error, because $db is not introduced correctly, so of course the query cannot be executed correctly.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/898292.htmlTechArticleCommon extension functions for PHP to link to MySQL, php to link to mysql 1. PHP connects to the database and basic operations MySQL uses ' client/server' architecture. Use the MySQL extension function installed by PHP, and directly...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template