php-doc-en/reference/mysql/functions/mysql-query.xml

199 lines
5.5 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.mysql-query" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>mysql_query</refname>
<refpurpose>Send a MySQL query</refpurpose>
</refnamediv>
<refsynopsisdiv>
<warning>
&mysql.alternative.note;
<simplelist role="alternatives">
<member><function>mysqli_query</function></member>
<member><methodname>PDO::query</methodname></member>
</simplelist>
</warning>
</refsynopsisdiv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>mysql_query</methodname>
<methodparam><type>string</type><parameter>query</parameter></methodparam>
<methodparam choice="opt"><type>resource</type><parameter>link_identifier</parameter><initializer>NULL</initializer></methodparam>
</methodsynopsis>
<para>
<function>mysql_query</function> sends a unique query (multiple queries
are not supported) to the currently
active database on the server that's associated with the
specified <parameter>link_identifier</parameter>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>query</parameter></term>
<listitem>
<para>
An SQL query
</para>
<para>
The query string should not end with a semicolon.
Data inside the query should be <link
linkend="function.mysql-real-escape-string">properly escaped</link>.
</para>
</listitem>
</varlistentry>
&mysql.linkid.description;
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset,
<function>mysql_query</function>
returns a <type>resource</type> on success, or &false; on
error.
</para>
<para>
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
<function>mysql_query</function> returns &true; on success
or &false; on error.
</para>
<para>
The returned result resource should be passed to
<function>mysql_fetch_array</function>, and other
functions for dealing with result tables, to access the returned data.
</para>
<para>
Use <function>mysql_num_rows</function> to find out how many rows
were returned for a SELECT statement or
<function>mysql_affected_rows</function> to find out how many
rows were affected by a DELETE, INSERT, REPLACE, or UPDATE
statement.
</para>
<para>
<function>mysql_query</function> will also fail and return &false;
if the user does not have permission to access the table(s) referenced by
the query.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Invalid Query</title>
<para>
The following query is syntactically invalid, so
<function>mysql_query</function> fails and returns &false;.
</para>
<programlisting role="php">
<![CDATA[
<?php
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Valid Query</title>
<para>
The following query is valid, so <function>mysql_query</function>
returns a <type>resource</type>.
</para>
<programlisting role="php">
<![CDATA[
<?php
// This could be supplied by a user, for example
$firstname = 'fred';
$lastname = 'fox';
// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends
WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string($firstname),
mysql_real_escape_string($lastname));
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mysql_connect</function></member>
<member><function>mysql_error</function></member>
<member><function>mysql_real_escape_string</function></member>
<member><function>mysql_result</function></member>
<member><function>mysql_fetch_assoc</function></member>
<member><function>mysql_unbuffered_query</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->