PhpDig.net

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




mysql_db_query

Name

mysql_db_query — Queries a MySQL database.

Synopsis

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

Returns

Query handle for successful SELECT, DESCRIBE, EXPLAIN, and SHOW queries; TRUE/FALSE for other queries; FALSE on error

Description

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

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

Note

This function sets the database that's queried by calling the mysql_select_db() function. Any subsequent calls to mysql_query() use the specified database unless overridden by another call to mysql_db_query() or a call to mysql_select_db() . An excellent alternative is to use the mysql_query() function with queries that specify the absolute name of the table in the form database.table. For example:

mysql_query ("SELECT * FROM database.table");


Version

PHP 3+, PHP 4+

See also

To find the number of rows affected by a query:

mysql_num_rows()

To retrieve data from a query:

mysql_fetch_array()

mysql_fetch_assoc()

mysql_fetch_object()

mysql_fetch_row()

mysql_result()

To query the active database:

mysql_query()



Example

Example 795. Update a database using mysql_db_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
// Change all login names to lowercase
$query = "UPDATE user SET login = LCASE(login)";

mysql_db_query ($db, $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 '$query'";
?>

Example 796. Make a SELECT query using mysql_db_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
// Find all usernames that contain an upper- or lowercase 'a'
$query = "SELECT * FROM user WHERE login LIKE '%a%' ORDER BY login";

$mysql_result = mysql_db_query ($db, $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>';
}
?>



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.