I am a php noob and have a quick question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Computer Guy

    I am a php noob and have a quick question

    Hi I have recently started working with PHP and am making a
    neighborhood website. I am connecting to my Mysql database and am
    having difficulty understanding what is happening in the example on
    the PHP website. It connects fine but I do not understand the code.
    I do not just like to take other people's code so if someone can
    explain this to me that would be great.

    <?php
    $link = mysql_connect(' localhost', 'mysql_user', 'mysql_password ');
    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_close($li nk);
    ?>

    I do not understand the if (!$link) part
    what does the ! mean and what is the value of link?

    thank you

  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: I am a php noob and have a quick question

    Computer Guy wrote:
    I do not understand the if (!$link) part
    If no connection has been set to the database, for any reason, throw an
    error and die gracefully.
    what does the ! mean and what is the value of link?

    http://php.net/mysql_connect (the part about "return values")

    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Siempre y nunca, es tan largo el uno como el otro.- Elsa Triolet.

    Comment

    • Ivan Marsh

      #3
      Re: I am a php noob and have a quick question

      On Mon, 18 Jun 2007 15:21:34 -0700, Computer Guy wrote:
      Hi I have recently started working with PHP and am making a neighborhood
      website. I am connecting to my Mysql database and am having difficulty
      understanding what is happening in the example on the PHP website. It
      connects fine but I do not understand the code. I do not just like to
      take other people's code so if someone can explain this to me that would
      be great.
      >
      <?php
      $link = mysql_connect(' localhost', 'mysql_user', 'mysql_password '); if
      (!$link) {
      die('Could not connect: ' . mysql_error());
      }
      echo 'Connected successfully';
      mysql_close($li nk);
      ?>
      >
      I do not understand the if (!$link) part what does the ! mean and what
      is the value of link?
      It's checking to see if $link has been assigned a valid connection. The
      exclamation point means "not" or "false" however you'd like to turn it
      into English.

      Comment

      • shubeer

        #4
        Re: I am a php noob and have a quick question

        It's pretty simple. The PHP manual says this about the mysql_connect
        function:

        "Returns a MySQL link identifier on success, or FALSE on failure."

        So, if the connection to the database does not succeeds then the
        condition !$link will equate to TRUE because the ! means NOT i.e. NOT
        FALSE meaning TRUE. If you had to translate this to English
        therefore, the test !$link tests whether the database connection has
        been made and if not, then it will run the code:

        die('Could not connect: ' . mysql_error());

        which causes the script to terminate giving an appropriate error
        message.

        Hope this helps.

        Shubeer

        Comment

        • Lars Eighner

          #5
          Re: I am a php noob and have a quick question

          In our last episode,
          <1182205294.671 165.187750@m36g 2000hse.googleg roups.com>, the lovely and
          talented Computer Guy broadcast on comp.lang.php:
          Hi I have recently started working with PHP and am making a
          neighborhood website. I am connecting to my Mysql database and am
          having difficulty understanding what is happening in the example on
          the PHP website. It connects fine but I do not understand the code.
          I do not just like to take other people's code so if someone can
          explain this to me that would be great.
          ><?php
          $link = mysql_connect(' localhost', 'mysql_user', 'mysql_password ');
          if (!$link) {
          die('Could not connect: ' . mysql_error());
          }
          echo 'Connected successfully';
          mysql_close($li nk);
          ?>
          I do not understand the if (!$link) part
          what does the ! mean and what is the value of link?
          ! means not (in PHP and many other programming languages).

          !$link is true if $link is false, and !$link is false if $link is true.

          In PHP variables which do not exist, are empty, or are equal to zero are
          false, and otherwise are true. If mysql_connect fails it returns the
          value false, so $link = FALSE, but if it succeeds it returns something
          else which is not false (and therefore is true).

          So essentially "if (!$link)" means "if $link does not exist." In some
          contexts you must tell the difference between "does not exist" and
          "exists but is zero" (which this test cannot distinguish), but this is not
          one of those contexts.

          $link, when a connection is established, is a resource id. You could echo
          it out to look at but it would only look something like Resource id #11 (or
          some other number). It is meaningless to you and is really a sort of handle
          for you to refer to this particular database connection. Don't try to do
          stuff with resources directly. That is, trying to set $link equal to the
          string "Resource id #11" is likely to be disappointing. You can return
          $link and in some other respects treat it like a variable if you don't muck
          with its innards. And indeed, this is a case in point: if(!$link) treats
          $link just like a boolean variable. You could OR it or AND it or assign it.
          Just do not expect things to turn out well if you try to perform arithmetic
          or string functions on it.

          If you look up the mysql functions you will discover that some of them have
          an optional argument [resource_link_i dentifier]. Well, it isn't really
          optional, but if you leave it out, it reverts to the last link reference.
          In other words, you will see code with a sequence of database operations and
          you seldom see the link resource (almost always called $link) except for
          when it is opened, tested, and closed. But it actually is there (as the
          default) in mysql_selectdb, mysql_query, and so forth (and they will fail if
          you have not previously established a link).

          If $link was the last link you referred to, mysql_query($qu ery) really
          means mysql_query($qu ery,$link) --- and vice versa, you always can
          make the link resource explicit.

          The link resource doesn't participate in mysql functions that act on
          results, but some functions do not produce a result resource and so tests on
          the link must be done (such as mysql_affected_ rows) to determine the effect.

          You could have several links open at once. You might select one database
          for one link and a different one for another. Most of the time that would
          be a sloppy and careless thing to do, but it is there when you really need
          it or even if you don't. If you do open several links at once, you almost
          certainly should make the link explicit for your own sanity, otherwise the
          Law of General Cussedness will ensure that the default will always be to the
          wrong link.

          --
          Lars Eighner <http://larseighner.com/ <http://myspace.com/larseighner>
          Countdown: 581 days to go.
          An amazing thing about Christians: people who doubt being related to monkeys,
          but are certain they belong to the same species as Paris Hilton or Karl Rove.

          Comment

          Working...