SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    jokes.php, p77-79 PHP/MySQL book

    I was studying the code line by line for jokes.php on pages 77-79 of your PHP/MySQL book.

    I notice in one place you made the query into a variable $sql. Later on you did the query straight without a variable. Is there a reason you made the query a variable the first time?

    Query is a variable:

    Code:
      if (isset($_POST['joketext'])) {
        $joketext = $_POST['joketext'];
        $sql = "INSERT INTO joke SET
            joketext='$joketext',
            jokedate=CURDATE()";
        if (@mysql_query($sql)) {
          echo '<p>Your joke has been added.</p>';
    Query is not a variable:

    Code:
     $result = @mysql_query('SELECT joketext FROM joke');
      if (!$result) {
        exit('<p>Error performing query: ' .
            mysql_error() . '</p>');
      }
    Thanks!
    Steve

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as far as i know if you define a variable you can use it later on without redefining it. As long as the variable is defined before you make a call for it then it should work

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I agree. However, both queries were at the end of the code. $sql was not referenced after it was introduced the first time. I suspect it was coded that way out of habit.

    Could this $sql be accessed from another .php page? There's no $_POST linked to it, so I don't think so.

    I'm going over everything the third or fourth time because I'm trying to understand every line of code I put down.

    Thanks!
    Steve

  4. #4
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There is no particular reason to choose one over the other, Steve, although technically the approach that does not use a variable will be ever so slightly faster.

    In the examples you cited, I'd say it just came down to the length of the SQL query—if I had tried to cram the first SQL query into the mysql_query call, it would have made the code more difficult to read, whereas the second query was short enough to fit comfortably.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Thanks, Kevin! You opted in favor of the reader!

    Best wishes,
    Steve


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
  •