PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




mysql_query

Name

mysql_query — Queries the default MySQL database.

Synopsis

query handle mysql_query(query[, connection]);
string query: Query to use on the database
mysql link connection (optional): Connection handle returned by mysql_connect() or mysql_pconnect()

Returns

Query handle for successful SELECT queries, TRUE/FALSE for other queries; FALSE on error

Description

mysql_query() executes query on the default database, set using mysql_select_db() or by a previous query using mysql_db_query() , on the MySQL server connection referenced by connection . If no connection handle is specified in the connection argument, the last connection opened is used by default. If no connection is open, mysql_query() attempts to connect to a MySQL database by calling mysql_connect() without arguments.

The value returned depends on the query. SELECT, DESCRIBE, EXPLAIN, and SHOW queries return a MySQL result handle if successful and FALSE if they fail. Note that these types of queries are considered to have failed only if they're malformed. Other query types return TRUE on success and FALSE on failure.

Hex values in PHP are 32-bit signed numbers. We can get around this limitation by using MySQL. It supports hex values of up to 64 bits signed precision.

The following example is more whimsical than practical, but it does highlight one very important point: MySQL has a great deal of functionality. A lot of thorny problems can be solved quickly at the database level, before they even reach PHP.

If you need to manipulate large integers in PHP, use the GNU MP support (added in PHP 4.0.4).

Version

PHP 3+, PHP 4+

See also

To find the number of rows affected by a query:

mysql_num_rows()

To set the default database:

mysql_select_db()

To retrieve data from a query:

mysql_fetch_array()

mysql_fetch_assoc()

mysql_fetch_object()

mysql_fetch_row()

mysql_result()

To query a specific database:

mysql_db_query()



Example

Example 824. Update a database using mysql_query()

<?php
// Included code that connects to a MySQL server and sets a default database
// See the MySQL Functions chapter introduction for the source code for the file
include ('mysql_connect.inc.php');

// Change all login names to lower case
$query = "UPDATE user SET login = LCASE(login)";

mysql_query ($query)
    or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');

// Display the number of rows changed by the query
echo mysql_affected_row (), " login names were modified by query <b>$query</b>";
?>

Example 825. Make an EXPLAIN query using mysql_query()

<?php
// Connect to the default MySQL server
mysql_connect ()
   or die ("Could not connect to the default MySQL server.");

// Storing our query in a variable helps us debug more easily
$query = "EXPLAIN user";

$mysql_result = mysql_query ($query)
    or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');

// Display the data returned by the query
while ($temp = mysql_fetch_row ($mysql_result)) {
    echo $temp[0], '<br>';
}
?>

Example 826. Use MySQL to convert a large hex value to a decimal value

<?php
function mysql_hex2dec ($hex) {
    // Ensure that $hex is a valid hex string
    if (! ereg ('^0x[0-9A-Fa-f]+$', $hex)) {
        user_error ("mysql_hex2dec: '$hex' is not a valid hex string.");
        return;
    }

    // Connect to the default MySQL server
    if (! @ mysql_connect ()) {
        user_error ("mysql_hex2dec: Could not connect to the default MySQL server.");
        return;
    }

    // If our value is less than 64 signed bits, then
    // coerce $hex into an integer value by adding 0, else
    // return NULL
    $query = "SELECT IF($hex < 0x7FFFFFFFFFFFFFFF, $hex+0, \"\0\")";
    return @ mysql_result (mysql_query ($query), 0);
}

echo mysql_hex2dec ('0xD53A86133D867C772');
?>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.