Current Location: Home> Function Categories> mysql_query

mysql_query

Send a MySQL query.
Name:mysql_query
Category:Uncategorized
Programming Language:php
One-line Description:Send a query or execution statement to a MySQL database

Function: mysql_query()

Applicable version: PHP 5.x - PHP 7.0.x (excluding PHP 7.0.x)

Usage: The mysql_query() function is used to send queries or execute statements to a MySQL database.

Syntax: resource mysql_query ( string $query [, resource $link_identifier = NULL ] )

parameter:

  • query: Required, query or statement to be executed.
  • link_identifier: Optional, MySQL connection identifier. If not provided, the most recently opened connection is used.

Return value: Returns a resource identifier (for subsequent operations) when successful, and returns FALSE when failure.

Notes:

  • This function has been deprecated in PHP 5.5.0 and has been removed in PHP 7.0.0. It is recommended to use the mysqli or PDO_MySQL extension instead.
  • When using this function to execute a SELECT query, a result set resource identifier is returned. You can use mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_object() and other functions to obtain the data in the result set.
  • When using this function to perform operations such as INSERT, UPDATE, DELETE, etc., a Boolean value is returned to indicate whether the operation is successful.

Example:

 // 连接到MySQL 数据库$link = mysql_connect("localhost", "username", "password"); if (!$link) { die('连接失败: ' . mysql_error()); } // 选择数据库$db_selected = mysql_select_db("database_name", $link); if (!$db_selected) { die('选择数据库失败: ' . mysql_error()); } // 执行查询$result = mysql_query("SELECT * FROM users"); if ($result) { // 获取结果集中的数据while ($row = mysql_fetch_assoc($result)) { echo "ID: " . $row['id'] . ", 名字: " . $row['name']; } } else { echo "查询失败: " . mysql_error(); } // 关闭连接mysql_close($link);

Note that since the mysql_query() function has been deprecated, it is recommended to use the mysqli or PDO_MySQL extension to connect to and manipulate the MySQL database. The above examples are for reference only and are not recommended for use in production environments.

Similar Functions
Popular Articles