array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'pt_BR', ), 'this' => array ( 0 => 'function.mysql-db-query.php', 1 => 'mysql_db_query', ), 'up' => array ( 0 => 'ref.mysql.php', 1 => 'Funções da MySQL', ), 'prev' => array ( 0 => 'function.mysql-db-name.php', 1 => 'mysql_db_name', ), 'next' => array ( 0 => 'function.mysql-drop-db.php', 1 => 'mysql_drop_db', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/mysql/functions/mysql-db-query.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4, PHP 5)
mysql_db_query — Selects a database and executes a query on it
Esta função tornou-se defasada a partir do PHP 5.3.0 e ela e a extensão MySQL original foram removidas a partir do PHP 7.0.0. Em vez disso, use as extensões MySQLi ou PDO_MySQL, que são desenvolvidas ativamente. Veja também o guia MySQL: escolhendo uma API. Alternativas a esta função incluem:
mysql_db_query() selects a database, and executes a query on it.
database
The name of the database that will be selected.
query
The MySQL query.
Data inside the query should be properly escaped.
link_identifier
A conexão MySQL. Se o
identificador da conexão não for especificado, a última conexão aberta por
mysql_connect() será usada. Se não houver uma conexão anterior,
haverá uma tentativa de criar uma como se mysql_connect() tivesse sido chamada
sem argumentos. Se nenhuma conexão for encontrada ou estabelecida, um erro de nível
E_WARNING
será gerado.
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.
Exemplo #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);
?>
Nota:
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.