Search code examples
phpinclude

PHP Include two files


New to PHP and having a problem with a page that has two included statements.

I have one file that has my database connection code and another that has code to list information from the database. The code in the DB connection file runs fine but the code in the second one does not.

To test I have pulled out all the database stuff from the second file and just left a bunch of echo statements but that code still does not run.

I put the "Testing1" and "Testing2" in the main page for troubleshooting. "Connection to MySQL server successfully established" and "Testing 1" are shown when the page is loaded but nothing else.

Main page

  <?php include 'db.php';?>
<html>
<Head>
    <Title>
        Tracked Items
    </title>
</head>
<body>
  Testing1<br>
<?php include 'ListAll.php';?>
  Testing2<br>
</body>
<?php $conn->close(); ?>
</html>

db.php

<?php
  $host_name = 'xxxx';
  $database = 'xxxx';
  $user_name = 'xxxx';
  $password = 'xxxx';

  $link = new mysqli($host_name, $user_name, $password, $database);

  if ($link->connect_error) {
    die('<p>Failed to connect to MySQL: '. $link->connect_error .'</p>');
  } else {
     echo '<p>Connection to MySQL server successfully established.</p>';
  }
?>

ListAll.php


<?php
echo "<b>Current Items</b><br>";
echo "<Table border=""1"">";
echo "  <tr>";
echo "      <td width=""300""><b>Description</b></td>";
echo "      <td width=""80""><b>Date Opened</b></td>";
echo "      <td width=""80""><b>Last Action Date</b></td>";
echo "      <td width=""750""><b>Last Action</b><td>";
echo "</tr>";
?>

Solution

  • Your problem is the double double quotes.

    And it reports the error as

    Parse error: syntax error, unexpected double-quoted string "1", expecting "," or ";" ......

    when you find your error log these should be in there.

    So either use single quotes for the attribute values inside a double quoted string like this

    echo "<b>Current Items</b><br>";
    echo "<Table border='1'>";
    echo "  <tr>";
    echo "      <td width='300'><b>Description</b></td>";
    echo "      <td width='80'><b>Date Opened</b></td>";
    echo "      <td width='80'><b>Last Action Date</b></td>";
    echo "      <td width='750'><b>Last Action</b><td>";
    echo "</tr>";
    

    OR single quote the echo and use double quotes for the attributes, and of course only one set of double quotes

    echo '<b>Current Items</b><br>';
    echo '<Table border="1">';
    echo '  <tr>';
    echo '      <td width="300"><b>Description</b></td>';
    echo '      <td width="80"><b>Date Opened</b></td>';
    echo '      <td width="80"><b>Last Action Date</b></td>';
    echo '      <td width="750"><b>Last Action</b><td>';
    echo '</tr>';
    

    To get errors out of PHP even in a LIVE environment add these 3 lines temporarily, while debugging, to the top of any MYSQLI_ based script you want to debug

    ini_set('display_errors', 1); 
    ini_set('log_errors',1); 
    error_reporting(E_ALL); 
    

    If you are using PHP < 8.0, to get errors from the mysqli_ extension also add

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    

    This will force any MYSQLI_ errors to generate an Exception that you can see on the browser as well as normal PHP errors. In PHP 8 or greater, this is the default for MySQLI calls.