Page 1 of 1
Using Select instead of Query
Posted: Mon Aug 02, 2004 4:33 am
by hendy
Hi, i have been told that i can use the mysql_select_db() then use mysql_query() to select the database instead of using mysql_db_query
Here is the code i am using, could someone suugest how i can use the mysql_select_db then the mysql_query ...
<?php
$query = “SELECT * FROM $tblname ORDER BY name”;
$result = mysql_db_query($dbname, $query, $conn);
mysql_close ($conn);
?>
Thanks
Posted: Mon Aug 02, 2004 4:38 am
by Jean-Yves
Code: Select all
$server = "myserver";
$dbName = "myDB";
$dbUser = "myuser";
$dbPassword = "mypassword";
$link = mysql_connect($server, $dbName, $dbPassword) or die(mysql_error() . "<p>Unable to establish connection</p>");
mysql_select_db($dbName) or die(mysql_error() . "<p>Unable to select database</p>");
Code: Select all
$query= "SELECT * ...";
$result = mysql_query($query) or die(mysql_error());
Is that what you meant?
Posted: Mon Aug 02, 2004 4:43 am
by hendy
Could that code be used directly, instead of the initial code?
Posted: Mon Aug 02, 2004 4:51 am
by Jean-Yves
You always need a database connection, so you would need to establish the link to the database somewhere in your code. Is there a particular reason why you would not want to have an explicit connection set up front?
Posted: Mon Aug 02, 2004 5:01 am
by hendy
Its just part of Uni assignment, we have been given a large section of code and been asked to come up with different ways of getting the same result, i was stuck on that little bit, appreciate your help, thanks
Posted: Mon Aug 02, 2004 5:14 am
by Jean-Yves
My pleasure. Good luck with the assignment
