SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Athens, Greece
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help PHP problem

    Hi, I am working on Kevin Yank's DATABASE DRIVEN WEB SITE USING PHP & MYSQL, 4 edition and I have a problem with the following code that appears in page 147 concerning the index.php file. Please have in mind that all the previous examples of the book are running ok so there is not any problem about connecting to mysql.

    Briefly when I run the following code of the index.php file I receive the message "Appache HTTP Server has encountered a problem and needs to close". The strange thing is that if in the code line: $result = mysqli_query($link, 'SELECT id, joketext FROM joke' );
    I omit the "," after the "id" the code runs but without displaying the text that correspond to "joketext".

    Thank you in advance for your reply

    (Following is the code of the index.php file)

    <?php
    if (get_magic_quotes_gpc())
    {
    function stripslashes_deep($value)
    {
    $value = is_array($value) ?
    array_map('stripslashes_deep', $value) :
    stripslashes($value);
    return $value;
    }
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
    }
    if (isset($_GET['addjoke']))
    {
    include 'form.html.php';
    exit();
    }
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
    $error = 'Unable to connect to the database server.';
    include 'error.html.php';
    exit();
    }
    if (!mysqli_set_charset($link, 'utf8'))
    {
    $output = 'Unable to set database connection encoding.';
    include 'output.html.php';
    exit();
    }


    if (!mysqli_select_db($link, 'ijdb'))
    {
    $error = 'Unable to locate the joke database.';
    include 'error.html.php';
    exit();
    }
    if (isset($_POST['joketext']))
    {
    $joketext = mysqli_real_escape_string($link, $_POST['joketext']);
    $sql = 'INSERT INTO joke SET
    joketext="' . $joketext . '",
    jokedate=CURDATE()';
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error adding submitted joke: ' . mysqli_error($link);
    include 'error.html.php';
    exit();
    }
    header('Location: .');
    exit();
    }
    if (isset($_GET['deletejoke']))
    {
    $id = mysqli_real_escape_string($link, $_POST['id']);
    $sql = "DELETE FROM joke WHERE id='$id'";
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error deleting joke: ' . mysqli_error($link);
    include 'error.html.php';
    exit();
    }
    header('Location: .');
    exit();
    }
    $result = mysqli_query($link, 'SELECT id, joketext FROM joke');
    if (!$result)
    {
    $error = 'Error fetching jokes: ' . mysqli_error($link);
    include 'error.html.php';

    exit();
    }
    while ($row = mysqli_fetch_array($result))
    {
    $jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
    }
    include 'jokes.html.php';
    ?>

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,328
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Stephanos View Post
    The strange thing is that if in the code line: $result = mysqli_query($link, 'SELECT id, joketext FROM joke' );
    I omit the "," after the "id" the code runs but without displaying the text that correspond to "joketext".
    yes, it appears strange, but it actually runs and returns a single column called joketext which contains the id values

    this is because without the comma, it conforms to this structure:
    Code:
    SELECT columnname [AS] aliasname
      FROM ...
    so if you write
    Code:
    SELECT id joketext
      FROM ...
    then you are actually returning the id column values with a column alias name of joketext

    note that the AS is optional, but it's a lot more obvious what the intent is if you always write it
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Is this book cursed, or what? Three new members asking the same question (or almost) in 24 hours

    http://www.sitepoint.com/forums/showthread.php?t=629282
    http://www.sitepoint.com/forums/showthread.php?t=629368

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably the MySQL crashes and this in turn crashes PHP module and Apache.

    There may be numerous reasons: from faulty MySQL module to other software fault to hardware fault.

    First thing I'd do is try different version of MySQL module.

  5. #5
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Athens, Greece
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    Probably the MySQL crashes and this in turn crashes PHP module and Apache.

    There may be numerous reasons: from faulty MySQL module to other software fault to hardware fault.

    First thing I'd do is try different version of MySQL module.
    Thanks a lot, I will try wamp to check it

  6. #6
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Athens, Greece
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    yes, it appears strange, but it actually runs and returns a single column called joketext which contains the id values

    this is because without the comma, it conforms to this structure:
    Code:
    SELECT columnname [AS] aliasname
      FROM ...
    so if you write
    Code:
    SELECT id joketext
      FROM ...
    then you are actually returning the id column values with a column alias name of joketext

    note that the AS is optional, but it's a lot more obvious what the intent is if you always write it
    Thanks a lot, your comment helped me to understand better the SELECT command

  7. #7
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Athens, Greece
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=guido2004;4331335]Is this book cursed, or what? Three new members asking the same question (or almost) in 24 hours

    [QUOTE]

    Thanks for the links, it seems that it is a common problem

  8. #8
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Athens, Greece
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good news, I followed fbrill's recommendation and I installed PHP 5.3.0 (instead of the PHP 5.2.10) and now everything works just fine. Many thanks to all of you for your help.


Tags for this Thread

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
  •