<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Send a MySQL query</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mysql-ping.html">mysql_ping</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mysql-real-escape-string.html">mysql_real_escape_string</a></div> <div class="up"><a href="ref.mysql.html">MySQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.mysql-query" class="refentry"> <div class="refnamediv"> <h1 class="refname">mysql_query</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_query</span> — <span class="dc-title">Send a MySQL query</span></p> </div> <div id="function.mysql-query-refsynopsisdiv"> <div class="warning"><strong class="warning">Warning</strong> <p class="para">This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the <a href="book.mysqli.html" class="link">MySQLi</a> or <a href="ref.pdo-mysql.html" class="link">PDO_MySQL</a> extension should be used. See also <a href="mysqlinfo.api.choosing.html" class="link">MySQL: choosing an API</a> guide and <a href="faq.databases.html#faq.databases.mysql.deprecated" class="link">related FAQ</a> for more information. Alternatives to this function include:</p> <ul class="simplelist"> <li class="member"> <span class="function"><a href="mysqli.query.html" class="function">mysqli_query()</a></span></li> <li class="member"> <span class="methodname"><a href="pdo.query.html" class="methodname">PDO::query()</a></span></li> </ul> </div> </div> <div class="refsect1 description" id="refsect1-function.mysql-query-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>mysql_query</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$link_identifier</code><span class="initializer"> = NULL</span></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>mysql_query()</strong></span> sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified <em><code class="parameter">link_identifier</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.mysql-query-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">query</code></em></span> <dd> <p class="para"> An SQL query </p> <p class="para"> The query string should not end with a semicolon. Data inside the query should be <a href="function.mysql-real-escape-string.html" class="link">properly escaped</a>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter"> link_identifier</code></em></span><dd> <p class="para">The MySQL connection. If the link identifier is not specified, the last link opened by <span class="function"><a href="function.mysql-connect.html" class="function">mysql_connect()</a></span> is assumed. If no such link is found, it will try to create one as if <span class="function"><a href="function.mysql-connect.html" class="function">mysql_connect()</a></span> was called with no arguments. If no connection is found or established, an <strong><code>E_WARNING</code></strong> level error is generated.</p></dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.mysql-query-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, <span class="function"><strong>mysql_query()</strong></span> returns a <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> on success, or <strong><code>FALSE</code></strong> on error. </p> <p class="para"> For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, <span class="function"><strong>mysql_query()</strong></span> returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on error. </p> <p class="para"> The returned result resource should be passed to <span class="function"><a href="function.mysql-fetch-array.html" class="function">mysql_fetch_array()</a></span>, and other functions for dealing with result tables, to access the returned data. </p> <p class="para"> Use <span class="function"><a href="function.mysql-num-rows.html" class="function">mysql_num_rows()</a></span> to find out how many rows were returned for a SELECT statement or <span class="function"><a href="function.mysql-affected-rows.html" class="function">mysql_affected_rows()</a></span> to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement. </p> <p class="para"> <span class="function"><strong>mysql_query()</strong></span> will also fail and return <strong><code>FALSE</code></strong> if the user does not have permission to access the table(s) referenced by the query. </p> </div> <div class="refsect1 examples" id="refsect1-function.mysql-query-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-1608"> <p><strong>Example #1 Invalid Query</strong></p> <div class="example-contents"><p> The following query is syntactically invalid, so <span class="function"><strong>mysql_query()</strong></span> fails and returns <strong><code>FALSE</code></strong>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$result </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT * WHERE 1=1'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">'Invalid query: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-1609"> <p><strong>Example #2 Valid Query</strong></p> <div class="example-contents"><p> The following query is valid, so <span class="function"><strong>mysql_query()</strong></span> returns a <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// This could be supplied by a user, for example<br /></span><span style="color: #0000BB">$firstname </span><span style="color: #007700">= </span><span style="color: #DD0000">'fred'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$lastname </span><span style="color: #007700">= </span><span style="color: #DD0000">'fox'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Formulate Query<br />// This is the best way to perform an SQL query<br />// For more examples, see mysql_real_escape_string()<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT firstname, lastname, address, age FROM friends <br /> WHERE firstname='%s' AND lastname='%s'"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">mysql_real_escape_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$firstname</span><span style="color: #007700">),<br /> </span><span style="color: #0000BB">mysql_real_escape_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$lastname</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Perform Query<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Check result<br />// This shows the actual query sent to MySQL, and the error. Useful for debugging.<br /></span><span style="color: #007700">if (!</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #DD0000">'Invalid query: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'Whole query: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$query</span><span style="color: #007700">;<br /> die(</span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">// Use result<br />// Attempting to print $result won't allow access to information in the resource<br />// One of the mysql result functions must be used<br />// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.<br /></span><span style="color: #007700">while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'firstname'</span><span style="color: #007700">];<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'lastname'</span><span style="color: #007700">];<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'address'</span><span style="color: #007700">];<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'age'</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #FF8000">// Free the resources associated with the result set<br />// This is done automatically at the end of the script<br /></span><span style="color: #0000BB">mysql_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.mysql-query-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.mysql-connect.html" class="function" rel="rdfs-seeAlso">mysql_connect()</a> - Open a connection to a MySQL Server</span></li> <li class="member"> <span class="function"><a href="function.mysql-error.html" class="function" rel="rdfs-seeAlso">mysql_error()</a> - Returns the text of the error message from previous MySQL operation</span></li> <li class="member"> <span class="function"><a href="function.mysql-real-escape-string.html" class="function" rel="rdfs-seeAlso">mysql_real_escape_string()</a> - Escapes special characters in a string for use in an SQL statement</span></li> <li class="member"> <span class="function"><a href="function.mysql-result.html" class="function" rel="rdfs-seeAlso">mysql_result()</a> - Get result data</span></li> <li class="member"> <span class="function"><a href="function.mysql-fetch-assoc.html" class="function" rel="rdfs-seeAlso">mysql_fetch_assoc()</a> - Fetch a result row as an associative array</span></li> <li class="member"> <span class="function"><a href="function.mysql-unbuffered-query.html" class="function" rel="rdfs-seeAlso">mysql_unbuffered_query()</a> - Send an SQL query to MySQL without fetching and buffering the result rows.</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mysql-ping.html">mysql_ping</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mysql-real-escape-string.html">mysql_real_escape_string</a></div> <div class="up"><a href="ref.mysql.html">MySQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>