SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict hurricanedan's Avatar
    Join Date
    Feb 2006
    Location
    North Carolina
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Page is displaying php instead of output

    Hey,

    I am really new to php and when I enter this code it actually displays the php after the print command on line 5 in the browser and not the output. Any ideas?

    <?
    $username="Physician";
    $password="doctor";
    $database="jmh";

    print"<br>after variables before mysql_connect</br>";

    mysql_connect("localhost",$username,$password);
    mysql_select_db($database) or die("Unable to select database");

    $query="SELECT * FROM dr_directory";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    print"Database Output";

    $i=0;

    while ($i<$num) {

    $last=mysql_result($result,$i,"LastName");
    $first=mysql_result($result,$i,"FirstName");
    $phone=mysql_result($result,$i,"HomePhone");

    print"<br>$first $last</br><br>$phone</br>";

    $i++
    }

    ?>

    thanks,

    Dan

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Server might not be configured to use short tags, try opening with <?php instead of just <?

    EDIT: ignore that I didn't fully read your post

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That was my first thought. My second was ... the file is actually called filename.php isn't it? (Stranger things are known to have been done).

    Edit:

    On re-reading the question, I'm not actually sure what the problem is you are trying to describe. Does it show the WHOLE FILE including ALL PHP code, or only the code after the print() function?
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Addict I don`t mind's Avatar
    Join Date
    Feb 2006
    Location
    LA
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hurricanedan
    Hey,

    I am really new to php and when I enter this code it actually displays the php after the print command on line 5 in the browser and not the output. Any ideas?

    <?
    $username="Physician";
    $password="doctor";
    $database="jmh";

    print"<br>after variables before mysql_connect</br>";

    mysql_connect("localhost",$username,$password);
    mysql_select_db($database) or die("Unable to select database");

    $query="SELECT * FROM dr_directory";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    print"Database Output";

    $i=0;

    while ($i<$num) {

    $last=mysql_result($result,$i,"LastName");
    $first=mysql_result($result,$i,"FirstName");
    $phone=mysql_result($result,$i,"HomePhone");

    print"<br>$first $last</br><br>$phone</br>";

    $i++
    }

    ?>

    thanks,

    Dan
    By the way there's a semicolon missing after the $i++ at the end
    Words are weapons of Mass Destruction.

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Good spot - so why didn't a PHP parsing error occur? (Or did it and the poster hasn't said so?)
    Ian Anderson
    www.siteguru.co.uk

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Every print statement should cause a parse error as there's no space between the word print and the string, nor a parentheses.

  7. #7
    SitePoint Addict hurricanedan's Avatar
    Join Date
    Feb 2006
    Location
    North Carolina
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    no parsing errors are showing

    Guys, looking over your posts and making the corrections that were listed I have revised the code and now it looks like this. I added the ()s and the semicolon. Also there have been no parsing errors.

    <?
    $username="Physician";
    $password="doctor";
    $database="jmh";
    print("<br>after variables before mysql_connect</br>");
    mysql_connect("localhost",$username,$password);
    mysql_select_db($database) or die("Unable to select database");

    $query="SELECT * FROM dr_directory";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    print("Database Output");

    $i=0;

    while ($i<$num) {

    $last=mysql_result($result,$i,"LastName");
    $first=mysql_result($result,$i,"FirstName");
    $phone=mysql_result($result,$i,"HomePhone");

    print("<br>$first $last</br><br>$phone</br>");

    $i++;
    }

    ?>

    This is the output:

    after variables before mysql_connect"); mysql_connect("localhost",$username,$password); mysql_select_db($database) or die("Unable to select database"); $query="SELECT * FROM dr_directory"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); print("Database Output"); $i=0; while ($i<$num) { $last=mysql_result($result,$i,"LastName"); $first=mysql_result($result,$i,"FirstName"); $phone=mysql_result($result,$i,"HomePhone"); print("
    $first $last
    $phone"); $i++; } ?>
    Last edited by hurricanedan; Apr 26, 2006 at 08:32. Reason: adding info

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Whilst it shouldn't cause the error you are seeing, it is <br> or <br /> ... there is no </br> tag related to the <br> tag.

    print ("<br />after variables before mysql_connect<br />");

    Also, where's your link identifier?

    $conn = mysql_connect ("localhost",$username,$password);

    I think you have something screwed up with your PHP installation - you should be getting error messages trying to run this code.
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Addict hurricanedan's Avatar
    Join Date
    Feb 2006
    Location
    North Carolina
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just as a testing idea I removed the <br></br> tags. I do not get any errors but I also get no outputs from my print() statements either.

  10. #10
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PS - you can use the print function with or without ( ) - they are not critical. (The space after the print command is critical though).
    Ian Anderson
    www.siteguru.co.uk

  11. #11
    SitePoint Addict hurricanedan's Avatar
    Join Date
    Feb 2006
    Location
    North Carolina
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, siteguru, I was not sure but I wanted to be sure that it worked. I also tried to strip the code down and this code will actually work:

    <?php

    $username="Physician";
    $password="doctor";
    $database="jmh";
    print("after variables before mysql_connect");
    print($username);
    print($password);
    print($database);
    print("$username $password $database");
    ?>

    Note: see how the opening will work with <?php and the close will work as ?>, I thought they had to be the same. Also I just want to let everyone know that I appreciate the quick responses.

    When I modify the code to read as follows the screen comes back blank, do you think it could be an error in my connect and select_db statements?:
    <?php

    $username="Physician";
    $password="doctor";
    $database="jmh";

    mysql_connect("localhost",$username,$password);
    mysql_select_db($database) or die("Unable to select database");

    print("after variables before mysql_connect");
    print("<br>$username");

    print("<br>$password");
    print("<br>$database");
    print("<br>$username $password $database");

    ?>
    Dan
    Last edited by hurricanedan; Apr 26, 2006 at 09:04. Reason: adding more code

  12. #12
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are just learning PHP why don't you first get away from the print and use echo? I guess it's more of personal choice but echo (I believe) is more widely used.
    PHP Code:
    $username 'Physician';
    $password 'doctor';
    $database 'jmh';
    $host 'localhost';

    mysql_connect($host$username$password);
    mysql_select_db($database) or trigger_error(mysql_error());
    // the above will give you a better clue as to what is
    // going wrong but you probably don't want to use it on
    // a live site just for testing purposes.
    echo 'after variables before connect';
    echo 
    '<br>'.$username;
    echo 
    '<br>'.$password;
    echo 
    '<br>'.$database;
    echo 
    '<br>'.$username.' '.$password.' '.$database
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  13. #13
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Also, if you look at the source in your browser, does it display the whole file?
    Or just from after the print statement?

    Do you have access to the server's error log?

  14. #14
    SitePoint Addict hurricanedan's Avatar
    Join Date
    Feb 2006
    Location
    North Carolina
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Immerse,

    It seems to be a server error. It has a small list of errors for innoDB so maybe after a reboot it will work correctly.

    Thanks for that point.

  15. #15
    SitePoint Addict hurricanedan's Avatar
    Join Date
    Feb 2006
    Location
    North Carolina
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Guys,

    Thanks again for all of your help. It was an error on my part. I missed uncommenting my mysql extension. I had set the extension_dir but had forgotten to uncomment the mysql ext.

    Thanks again,

    Dan

  16. #16
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Glad you got it working!


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
  •