Using Select instead of Query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
hendy
Forum Newbie
Posts: 24
Joined: Sat Nov 29, 2003 6:35 am
Location: UK

Using Select instead of Query

Post 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
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post 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?
hendy
Forum Newbie
Posts: 24
Joined: Sat Nov 29, 2003 6:35 am
Location: UK

Post by hendy »

Could that code be used directly, instead of the initial code?
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post 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?
hendy
Forum Newbie
Posts: 24
Joined: Sat Nov 29, 2003 6:35 am
Location: UK

Post 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
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

My pleasure. Good luck with the assignment :)
Post Reply