...

is this OK?

boywonder
09-03-2002, 11:47 PM
Hi I have recently dived into PHP/mysql and have learned much just from browsing these forums. I have been connecting to my database as follows and it seems to be working just fine:

$dbconnect = mysql_connect("localhost", "user", "pass") or die("unable to connect to database");
mysql_select_db("database_name") or die("unable to find database");
$result = mysql_query("SELECT * FROM table") or die ("query failed");
mysql_close($dbconnect);

As I browse the threads here however I see many people doing it a little differently, for instance,
Using the connection variable as the second argument in mysql_select_db(), or using the database name as an argument in mysql_query(). I understand these are optional arguments for those functions but why do many people see the need to include them?

Also some save the query (and just about everything else) as a variable first.
So is the way I'm doing it inefficient or not the best way to go? I would really like to learn this the right way from the start.

PS - I know using @ before a function is supposed to supress errors, but isn't that what die() does... what's the difference?

Thank you kindly for any insight/advice.:thumbsup:

mordred
09-04-2002, 02:53 AM
Okay, you've got a bunch of questions, and I'll try to give my best to confuse err to enlighten you:

1.) Optional arguments
Many people do set them because they make a statement more explicit. There may be situations where the flow of the code in your script could produce two open mysql connections, or resource identifiers. So just relying on the last opend connection in such a case is surely hazardous. And because you never now how much you'll have to adjust your code in the future (if for example the business logic changes dramatically), you'll try take preemptive measures by saying explicitly "work on this connection and only on this connection".

2.) Passing a query string or a variable storing the query string
I'll definitely chose the variable. In my scripts, I'll most often have sth like


$sql = "DELETE FROM politician WHERE corruption = $value";
$result = mysql_query($sql, $conn);


So this snippet makes use of the value in the variable $value. If for some reason this variable is empty, you get an error. And to trace this error back it's often very practical to print out the actual query. So if I have


$valu = 100;
$sql = "DELETE FROM politician WHERE corruption = $value";
$result = mysql_query($sql, $conn);

if (!$result) {
echo "Query: " . $sql;
echo "<br>Errors: " . mysql_error();
}


I would see very quickly that my SQL query ended with " corruption = ", indicating that there's something wrong with exactly the variable $value, and then I'll stumble over my typo. I hope the example was not too bad and shows what benefit you gain from putting your queries into variables instead as putting them in the argument list.

3. Difference between '@' and die()
A '@' prepended to a function simply prevents an error message from getting printed to the screen, while die() stops totally the continuation of your script and prints an error message as the only argument to the string (sort of the "last words of your PHP script").

You're still with me? If not, feel free to ask.

boywonder
09-04-2002, 03:19 AM
mordred, your response was very clear and easy to understand. Thank you kindly for taking the time to spell out the answers to those questions. It's much appreciated. I may have a few more Q's at some point, and hopefully a few answers for the forum as well.
best regards, bw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum