Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: is this OK?

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    New York, USA
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    is this OK?

    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.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    PHP Code:
    $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

    PHP Code:
    $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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    New York, USA
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •