brock
brock

Reputation: 365

PHP block after html not executing

I have two blocks of code in the body section of an html ( .php) document. The code block before my heading executes, but the block after the heading does not. I've tried phpinfo(); in the first block and it runs fine. Try it in the second block and it doesn't. I have no idea why this could be. If anyone has suggestions I'm all ears.

I'm currently running xampp on a windows machine.

<body>
<?php
if (isset($_SESSION['ProfileID'])){
    echo "<div style='text-align: right'>" 
    . "<a href='CZSN_Login.php'>Log " 
    . "Out</a></div>\n";
}
//here phpinfo() executes
?>
<h1>Chinese Zodiac Social Network</h1>
<?php
require_once("Includes/inc_ChineseZodiacDB.php");
//here phpinfo() will not execute
if (isset($_SESSION['ProfileID'])){
    echo "<h2>Member Pages</h2>\n"; 
echo"<p>Below is a list of the members of the Chinese Zodiac Social"
        . "Network. Click on a member's name to view that member's detailed information" 
        . "You may also choose to <a href='CZSN_MyProfile.php'>update your profile</a>.</p>\n";
    $SQLQuery="select first_name, last_name, user_name "
        . "from zodiac_profiles order by "
        . "last_name, first name, user_name;";
    $result=$DBConnect->query($SQLQuery);
    if ($result===false){
        echo $ErrorMsgs[];  
    }
    else{
        //This should never happen, but we can check anyway.
        if ($result->num_rows==0){
            echo "<p>There are no members to show.</p>\n";  
        }

Here is the code in my inc_ChineseZodiacDB.php file:

<?php
$ErrorMsgs = array();
$DBConnect = @new msqli("localhost", "root", "password", "chinese_zodiac");
if ($DBConnect->connect_error)
    $ErrorMsgs[] = "The database server is not available." 
        . "Connect Error is " 
        . $mysqli->connect_errno 
        . " " . $mysqli->connect_error . ".";
?>

Upvotes: 0

Views: 388

Answers (4)

brock
brock

Reputation: 365

I figured out the ultimate problem. in the include file it reads:

<?php
$ErrorMsgs = array();
$DBConnect = @new msqli("localhost", "root", "password", "chinese_zodiac");
if ($DBConnect->connect_error)
    $ErrorMsgs[] = "The database server is not available." 
        . "Connect Error is " 
        . $mysqli->connect_errno 
        . " " . $mysqli->connect_error . ".";

?>

where it should read:

<?php
$ErrorMsgs = array();
$DBConnect = @new mysqli("localhost", "root", "password", "chinese_zodiac");
if ($DBConnect->connect_error)
    $ErrorMsgs[] = "The database server is not available." 
        . "Connect Error is " 
        . $mysqli->connect_errno 
        . " " . $mysqli->connect_error . ".";

?>

Note, I was calling msqli rather than mysqli. One simple typo...I'm still not understanding why the error messages were not being thrown for calling an undefined function. I added error_reporting(E_ALL); and ini_set('display_errors', 1); to the top of the file as well, and still no errors thrown. Either way, it works now. Thank you to everyone for the assistance.

Upvotes: 0

aashnisshah
aashnisshah

Reputation: 476

You are closing the php tag in the inc_ChineseZodiacDB file (i.e. you have ?> at the end of that file).

When your first file reads in the inc_ChineseZodiacDB file and sees the ?>, it interprets that as the end of your php section of code, and all the other code after require_once("Includes/inc_ChineseZodiacDB.php"); is ignored and read as regular html.

Remove that line, and your problem should be fixed:

<?php
$ErrorMsgs = array();
$DBConnect = @new msqli("localhost", "root", "password", "chinese_zodiac");
if ($DBConnect->connect_error)
    $ErrorMsgs[] = "The database server is not available." 
        . "Connect Error is " 
        . $mysqli->connect_errno 
        . " " . $mysqli->connect_error . ".";

Upvotes: 0

Tessmore
Tessmore

Reputation: 1039

I guess you get "Error headers already sent", because of the title <h1>Chinese Zodiac Social Network</h1>. For phpinfo() to work there shouldn't be any output yet.

Upvotes: 0

user3383999
user3383999

Reputation: 78

Try searching in require_once ("Includes / inc_ChineseZodiacDB.php"); there can be triggered die() or exit();

Upvotes: 1

Related Questions