SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Location
    Los Angeles
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question A couple questions on using PHP to connect to MySQL

    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 for your help!
    PHP Video Tutorials - sharing what I learn while I learn PHP.

  2. #2
    SitePoint Evangelist optl's Avatar
    Join Date
    Oct 2004
    Location
    Washington DC
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If mysql_connect() fails then the pages ends because of die()

    What $link is doing is getting the result from mysql_connect which isn't really necessary for the script to work. But if you set the result of the connection to a variable you can test if that variable is false instead of doing "or die(...)"
    For the phrase "Bethesda home architect", my clients
    websites occupy 6 of the first 8 results
    on the 1st page of Google. My Secret SEO Strategy Revealed

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just some quick points to get you on track:

    - functions always return a value. So the bit where it looks like the code is assigning a function to the variable, that is actually the way you 'capture' the return value from the function. In other words, $link is the returned value from calling the mysql_connect() function.'

    - the "or die()" bit. Die() instructs the php script to stop. Die('goodbye') will print 'goodbye' right before ending the script. The 'or' part just means that if a function returns FALSE, then the next part (i.e. die()) will execute.

    - if the connect function returns false, then die() is called. That is why echo 'Connected successfully' will only be called if mysql_connect was successful; otherwise die() would have ended the script.

  4. #4
    SitePoint Member
    Join Date
    Nov 2006
    Location
    Los Angeles
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much! That really cleared things up for me. Now it makes sense!
    PHP Video Tutorials - sharing what I learn while I learn PHP.


Bookmarks

Posting Permissions

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