Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Junior Member
    Join Date
    Nov 2006
    Posts
    6
    Member #
    14378
    The PHP manual has an example script. I'm confused by this first part:

    PHP Code:
    // Connecting, selecting database
    $link mysql_connect('mysql_host''mysql_user''mysql_password')
        or die(
    'Could not connect: ' mysql_error());
    echo 
    'Connected successfully';
    mysql_select_db('my_database') or die('Could not select database'); 
    It looks like they're just setting the $link variable equal to a function, not actually executing the function. So how/why does this code work to connect to the database?

    I'm also confused about how they're using "or die(...)" -- you can set variables equal to "function() or function()"? I've never seen this before!

    And then where it echos 'Connected successfully', why would that only happen if the connection works? I don't see any form of an if statement or anything.

    In short, I'm just confused by this whole thing, lol. Why does this work? Is this how you connect to MySQL, or do you do something different? Thanks in advance!

  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    It looks like they're just setting the $link variable equal to a function, not actually executing the function. So how/why does this code work to connect to the database?
    - $link is the identifier to the connection. That line of code does call the function mysql_connect which creates a connection.

    I'm also confused about how they're using "or die(...)" -- you can set variables equal to "function() or function()"? I've never seen this before!
    - What die() does is if theres a fatal PHP error, you can give PHP some relevant text to output, rather than the fatal error's message that occured.

    And then where it echos 'Connected successfully', why would that only happen if the connection works? I don't see any form of an if statement or anything.
    - Furthermore to die, it stops the execution of the PHP code. So if the function call caused a fatal error, the die message will be displayed. Otherwise, the execution of the source code will continue. That is why there is no if statement.

  4. #3
    Junior Member
    Join Date
    Nov 2006
    Posts
    6
    Member #
    14378
    Thanks, bfsog! Makes a lot more sense now.

  5. #4
    Senior Member raspberryh's Avatar
    Join Date
    Aug 2004
    Location
    Erie, PA
    Posts
    882
    Member #
    7208
    Liked
    1 times
    Quote Originally Posted by LearningNerd
    It looks like they're just setting the $link variable equal to a function, not actually executing the function.
    Hey just wanted to mention that they're not setting it equal to the function, they're setting it equal to the OUTPUT of the function. So it DOES execute the function, and then puts the output (the return variable) into $link.
    choosy developers choose gif!
    website | paintings | blog

  6. #5
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    and answer to initial question the mysql functions are executed becasue they are use in a variable, not another function which would mean they wouldnt be excecuted untill the function was called, the mysql functions are put into variables to make it easier for using them later on in the script.

    one thing i wouldnt put the connect string in every page but rather 1 page and include it where need like this.

    PHP Code:
    <?php

    // Change all of the values on the right

    define("DB_SERVER","localhost"); // Defines Constant Called DB_SERVER value localhost
    define("DB_USER","root"); // Defines Constant Called DB_USER value root
    define("DB_PASS","password");  // Defines Constant Called DB_PASS value password
    define("DB_NAME","somedatabase");  // Defines Constant Called DB_NAME value somedatabase

    $connection mysql_connect(DB_SERVERDB_USERDB_PASS); /// Connects to the data base with the constants define earlier

    mysql_select_db(DB_NAME$connection); // Selects the database defined in the constant and with the database connection $connection.
    ?>
    then just include that files where needed

    hope this helps
    Regards Phil,


  7. #6
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Another thing you should keep in mind, is that what appears to be a variable ($link in your question) is not actually containing anything readable. It is a resource identifier!

    In many other PHP constructs, you'll run into error messages like "illegal resource identifier".
    You will also run into problems accessing "variables" that are resultsets.
    A resultset is a refence to a data set, that can be accessed by a variety of abstracted functions to iterate through and present tabular data, or load arrays.

    They are basically internal pointers to the scripting engine. They are used to propagate actions in other bulit in functions.
    You could of course ask why such detours are neccesary, but the reason is that a lot of functionality depends on internal reference to other data objects. Instead of writing a thousand functions that covers all bases, the language gives you the option of using a resultset in various forms to instantiate different results, depending on what you want to achieve.

    A connection link can be used for a variety of purposes, if you want to use it to execute a query, then that is one of the options you have with that resultset.
    S. Rosland

  8. #7
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    And as a final note, to address your original question of:
    Quote Originally Posted by LearningNerd
    I'm also confused about how they're using "or die(...)" -- you can set variables equal to "function() or function()"? I've never seen this before!
    Or is an operator, and it's what we call `short-circuiting' operator. Basically, saying `true or false' will return true, but it will return true without even *looking* at false, because you don't need to -- the result of an or is true if *any* of its parts are true.

    Now, in PHP, `null' is equivalent to `false'. So, if I say `true or null', we get the same result (again, because we only look at true), and if I say `null or true', we also get `true'. Typically, anything that *isn't* null or false or 0 (I believe those are all of the false values in PHP) evaluates as `true' for the purposes of ors or conditionals. Moreover, what `or' does isn't return `true' or `false'. It returns the first value that doesn't evaluate to false. So `5 or false' would return 5, `6 or 7 or 8 or 9' would return `6', and so on.

    Progressing to the next step, mysql_connect will return a null pointer, or a null value, when it fails to connect. We just mentioned that `or' won't process anything past the first value that evaluates to not-false. So, when we do this:
    PHP Code:
    mysql_connect/* ... */ ) or die( "Got an error, stopping script..." 
    We know that if mysql_connect returns something that isn't null (i.e., it succesfully connects), then die() won't even run, and the value of that entire expression will be the value returned by mysql_connect.

    Similarly, when mysql_connect returns null (i.e., it fails to connect), then die() will run, and it will kill the output with the error message you pass it, and execution of the PHP will stop there.

    Hope that made sense :-)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:41 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com