mysql_query

2011-04-01 21:47 / no comment / 17 views /

(PHP 4, PHP 5)

_query() 函数执行一条 查询。

函数声明

resource mysql_query ( string $query [, resource $link_identifier ] )

mysql_query sends a unique query (multiple queries are not supported) to the currently active database on the server that’s associated with the specified link_identifier.

参数

query 必需。规定要发送的 SQL 查询。注释:查询字符串不应以分号结束。

connection 可选。规定 SQL 连接标识符。如果未规定,则使用上一个打开的连接。

返回值

对SELECT, SHOW, DESCRIBE, EXPLAIN等查询语句,mysql_query()在查询成功时返回一个资源标识符,失败时返回FALSE。

对INSERT, UPDATE, DELETE, DROP等更新和插入语句, mysql_query()在成功时返回TRUE,失败时返回FALSE。

mysql_fetch_array()解析资源标识符得到查询结果。通过mysql_num_rows()得到返回结果的条数。通过mysql_affected_rows()得到更新的结果条数。当用户没有足够权限时mysql_query()也返回FALSE。

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

说明

如果没有打开的连接,本函数会尝试无参数调用mysql_connect() 函数来建立一个连接并使用之。非 FALSE 的返回值意味着查询是合法的并能够被服务器执行。这并不说明任何有关影响到的或返回的行数。很有可能一条查询执行成功了但并未影响到或并未返回任何行。该函数自动对记录集进行读取和缓存。如需运行非缓存查询,请使用mysql_unbuffered_query()。

例子

例子1,查询数据库

<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con) {
die(‘Could not connect: ‘ . mysql_error());
}
$sql = "SELECT * FROM Person where 1=1"; mysql_query($sql,$con); // 一些代码
mysql_close($con);
?>

例子2,通过 mysql_query() 函数创建一个新数据库

<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con) {
die(‘Could not connect: ‘ . mysql_error());
}
$sql = "CREATE DATABASE my_db";
if (mysql_query($sql,$con)) {
echo "Database my_db created";
} else {
echo "Error creating database: " . mysql_error();
}
?>

Tags: .

Get a Trackback link

No Comments Yet

You can be the first to comment!

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>