Should we use select_db() in php?
I am wondering whether we need to use selec开发者_JAVA技巧t_db()
function when we query the DB since we are already defining which table we want to use when writing the query like "select * from users"
I tried without it and it worked but I don't know what kind of goodness that function offers?
//$sau_db->select_db("users");
$query = "select * from users";
$results = $sau_db->query($query);
echo "<ul>";
for($i=0;$i < $results->num_rows; $i++)
{
$row = $results->fetch_assoc();
echo "<li>".$row["first_name"].' '.$row["last_name"]."</li>";
}
echo "</ul>";
Selecting a database is different than selecting a table. A database can contain multiple tables and a MySQL server can contain more than one database. I'm not sure what system you're using to access your databases but unless configured elsewhere (which it very well may be) it is normally necessary to select a database.
Usually used as shown in the following link.
select_db is not for table name it's for default database to work your queries with, if you didn't specify db instance in you connection.
Using select_db removes any ambiguity about which database you're using.
Normally a database user is associated with a default database. When they make a connection and then make any queries, it defaults to using that database to select/insert/update/delete/etc.
Generally, it's wise to select_db(), as you could have catastrophic results if the default database is changed. For example, if you have a MYDBDEV, MYDBTEST, and MYDBPROD database, and the user connecting has a default db of MYDBDEV, then if for some reason somebody changes it to have a default db of MYDBPROD, all of a sudden your code is modifying MYDBPROD with no change to the code or any config files.
Very correct answer given above.
Check out your self with an example with default mysql databases (on the same server):
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("cdcol") or die(mysql_error());
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM cds")
or die(mysql_error());
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
echo "coloumn1: ".$row['titel'];
echo " coloumn2: ".$row['interpret'];
////////////////////// this will not work
###################mysql_select_db("mysql") or die(mysql_error());
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM help_category")
or die(mysql_error());
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
echo "<br><br>coloumn1: ".$row['help_category_id'];
echo " coloumn2: ".$row['name'];
?>
Only if you uncomment mysql_select_db("mysql") only then the second query will work, otherwise you get the error below, because the current connection refers to cdcol database:
Table 'cdcol.help_category' doesn't exist
精彩评论