Development PHP Code issue

Discussion in 'Software' started by deathtaker27, 9 Feb 2013.

  1. deathtaker27

    deathtaker27 #noob

    Joined:
    17 Apr 2010
    Posts:
    2,196
    Likes Received:
    155
    Hi,

    Im learning php, and I cannot get the following code bellow to work:

    PHP:
    <?php

    //Connect to mysql
    $odbc mysql_connect ('localhost''root''') or die("Could not connect to database");

    //connect to database
    mysql_select_db('IDBBooks',$odbc) or die("Can not find database");

    //Run query
    $sql "SELECT * FROM books";

    //Write data to screen
    $result mysql_query($sql,$odbc)or die("Can not run query");
    if(
    mysql_num_rows($result)==0)
    {
        
    //If no results
        
    echo "no records returned";    
    }
    else
    {
        
    //If results
        
    while($row=mysql_fetch_object($result))
        {
            echo 
    "<H1>";
            echo 
    $row->Title;
            echo 
    "</h1>";
            echo 
    "<p>";
            echo 
    $row->Image;
            echo 
    ",";
            echo 
    $row->Format;
            echo 
    ",";
            echo 
    $row->Price;
            echo 
    "</p>";
            
            echo 
    "<BR>";


        }
    }


    ?>
    The response i have is:
    Code:
    "; echo '$row->Title'; echo ""; echo "
    
    "; echo $row->Image; echo ","; echo $row->Format; echo ","; echo $row->Price; echo "
    "; echo "
    "; } } ?> 
    This is copied straight from the lecturer's notes essentially with small changes to look at my database, im just very confused (And yes I know I need to password root.)

    Thanks in advanced for the help

    DT.
     
  2. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,943
    Likes Received:
    268
    You need to run the script through PHP interpreter. You should install one of the preconfigured Apache+MySQL+PHP packages.
     
    deathtaker27 likes this.
  3. deathtaker27

    deathtaker27 #noob

    Joined:
    17 Apr 2010
    Posts:
    2,196
    Likes Received:
    155
    I have xampp with MYSQL and APACHE running currently, so unless I have broke xampp i doubt it will be that

    :S Now its suddenly working, and i haven't touched anything, anyone have any ideas why this would happen?
     
  4. tehBoris

    tehBoris What's a Dremel?

    Joined:
    30 Jan 2011
    Posts:
    616
    Likes Received:
    25
    If you actually copied it straight from the lecturers notes my guess is that some of the double quotes weren't actually double quotes but special characters that look like double quotes.
     
  5. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,943
    Likes Received:
    268
    Actually no tehBoris, because if you look at the output, then it is simply the browser trying to interpret the PHP file itself as-is, not interpreted by the server.

    Everything from <?php to echo "<H1> was interpreted as one tag, not rendered by the browser (because it doesn't know such tag). Then the following code was rendered as text :
    Code:
    "; 
            echo $row->Title; 
            echo "
    The "errorneus" end-tag </h1> was ignored, the rest was rendered according to HTML rules.

    Now the question is why didn't the server interpreted the PHP file, and i have no answer to that. The only thing i could think of is if your PHP file didn't had a .php file extension (or registered to the PHP interpreter) or if the PHP module didn't start due bad configuration, but that wouldn't explain why it did work when he tried it for second time.
     
    Last edited: 9 Feb 2013
  6. Matticus

    Matticus ...

    Joined:
    23 Feb 2008
    Posts:
    3,347
    Likes Received:
    117
    faugusztin is correct. It wasn't being served up by xampp as a php file, instead just being displayed by the browser. You can confirm this by copying the code, putting it into notepad, saving as (all files) whatever.html and double clicking.

    I would have to guess that either xampp hadn't started correctly, or instead of navigating to localhost:80/whatever.php (or .html) deathtaker just double clicked on the file.
     
  7. roundyz

    roundyz What's a Dremel?

    Joined:
    16 Jan 2002
    Posts:
    153
    Likes Received:
    2
    lint

    in the future you can do php -l filename.php to highlight syntax errors, make sure the code is good before altering your environment ;)
     

Share This Page