0

I am trying to occupy an html div of a page with the results of a database query from another page via a php echo command. here is my sample code: This is the page i am performing the query

$content = '<h3>Member Login</h3>
<?php
    //Step 3: Perform a DB Query
    $member_info = mysql_query("SELECT * FROM members", $db_connect);
?>
<?php
    // Step 4: Use Returned Data
    while($row = mysql_fetch_array($member_info)){
        echo "First Name: ".$row[1];
        echo "Last  Name: ".$row[2]."<br>";
        echo "User  Name: ".$row[3]."<br>";
        echo "Password  : ".$row[4]."<br>";
        echo <hr>;
    }
?>
';
include 'foundation.php';

This is the page I am issuing the echo command(foundation.php):

<div id="content">

    <?php 
        echo $content;
    ?>

</div><!--end content div-->

The database connection is well established on another page so I know that is not the issue. I believe it is a problem with my syntax because it displays the following:

"; echo "User Name: ".$row[3]."
"; echo "Password : ".$row[4]."
"; echo ; } ?> 
FevtheDev
  • 113
  • 2
  • 10

2 Answers2

1

Indeed, you have some syntax problems. You can't put PHP inside a string, and then expect the PHP to execute when you echo the string later on. Instead you will just have a static string. Functions and constructs will be interpreted as plain text, and you will lose all the PHP magic.

To create $content, try this:

<?php
$content = '<h3>Member Login</h3>';
$member_info = mysql_query("SELECT * FROM members", $db_connect);
while($row = mysql_fetch_array($member_info)){
    $content .= "First Name: ".$row[1];
    $content .= "Last  Name: ".$row[2]."<br>";
    $content .= "User  Name: ".$row[3]."<br>";
    $content .= "Password  : ".$row[4]."<br>";
    $content .= "<hr>";
}
include 'foundation.php';
?>

Just a note... mysql_* functions are deprecated. Instead, you should use mysqli or pdo. See Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • Thank you Mark M This proved to be the solution to my worries. Thanks for the lesson on the strings too I was not aware. Always learning haha! – FevtheDev Aug 17 '14 at 19:44
1

Syntax error :)

Anlything inside the ' will be treated as string type. Not as php code. My advice to you. Use an editor with syntax highlighting for php. Then you will make 0 misstakes like this again.

A good one is Coda for mac. Or zend studio for pc.

Björn3
  • 297
  • 1
  • 2
  • 8