search for in the  
<mysql_db_namemysql_drop_db>
Last updated: Thu, 19 May 2005

mysql_db_query

(PHP 3, PHP 4, PHP 5)

mysql_db_query -- Send a MySQL query

Description

resource mysql_db_query ( string database, string query [, resource link_identifier] )

mysql_db_query() selects a database, and executes a query on it.

Parameters

database

The name of the database that will be selected.

query

The MySQL query.

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level warning is generated.

Return Values

Returns a positive MySQL result resource to the query result, or FALSE on error. The function also returns TRUE/FALSE for INSERT/UPDATE/DELETE queries to indicate success/failure.

ChangeLog

VersionDescription
4.0.6 This function is deprecated, do not use this function. Instead, use mysql_select_db() and mysql_query() instead.

Examples

Example 1. mysql_db_query() alternative example

<?php

if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
   echo
'Could not connect to mysql';
   exit;
}

if (!
mysql_select_db('mysql_dbname', $link)) {
   echo
'Could not select database';
   exit;
}

$sql    = 'SELECT foo FROM bar WHERE id = 42';
$result = mysql_query($sql, $link);

if (!
$result) {
   echo
"DB Error, could not query the database\n";
   echo
'MySQL Error: ' . mysql_error();
   exit;
}

while (
$row = mysql_fetch_assoc($result)) {
   echo
$row['foo'];
}

mysql_free_result($result);

?>

Notes

Note: Be aware that this function does NOT switch back to the database you were connected before. In other words, you can't use this function to temporarily run a sql query on another database, you would have to manually switch back. Users are strongly encouraged to use the database.table syntax in their sql queries or mysql_select_db() instead of this function.



User Contributed Notes
mysql_db_query
kwillmert a visionaryweb dt com
13-Jan-2005 02:28
mysql_query seems to have some problems running from within a Class structure.  I have to run mysql_select_db() before every time I run certain queries such as "SHOW COLUMNS FROM  mytable"  Doesn't this sort of defeat the purpose of deprecating mysql_db_query()?  The new methods would be fine with me if they worked.  I've had a custom-trimmed down phpmyadmin type program that I've used for a long time that makes use of mysql_db_query() and it worked just fine until I tried converting it to the newly recommended method.
08-Nov-2004 11:06
I wonder, how much users/applications still use mysql_db_query:

Its very slow, cause before every query a mysql_select_db call is executed and its unsafe cause you can't use it with mysql_query in the same script without running in heavy probs.

If you have to use another db, just use "select fields from db.table"

--------------------

What would you then use if mysql_db_query isn't right :-$?

E-mail me:  tgrk35@gmail.com

Thanks a lot!
28-Sep-2004 10:55
selam bu site neden böyle cöküyor anlamis degilizz buna bir cözüm bulunacakmiii
yaroukh at email dot cz
22-May-2003 08:18
MySQL_Db_Query vs. MySQL_Query - MySQL replication pitfall

I'm updating two MySQL db's - "web" and "stats", these two databases are being replicated. I was wondering that the replication worked when sending queries from a console but with queries from a script it did't ...

the wrong way:
MySQL_Query("UPDATE web.users SET ..... WHERE id = 11444", $writeConnection);

the proper way:
MySQL_Db_Query("web", "UPDATE users SET ..... WHERE id = 11444", $writeConnection);

The catch is that only queries written from within a replicated db are being sent to be replicated - when you send a query without "USE <REPLICATED_DB>" (or the MySQL_Db_Query("<REPLICATED_DB>", ...);) the query won't be replicated - even if it "points" to a replicated db (e.g. "UPDATE <REPLICATED_DB>.table ...".

At least this is how it seems to work for me. :o)

Maybe this would help to someone.
phpinfo at t-online dot de
12-Oct-2002 04:12
I wonder, how much users/applications still use mysql_db_query:

Its very slow, cause before every query a mysql_select_db call is executed and its unsafe cause you can't use it with mysql_query in the same script without running in heavy probs.

If you have to use another db, just use "select fields from db.table"

<mysql_db_namemysql_drop_db>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT