PHP doesn't work

http://pastebin.com/YQAE1NEC
This is my current code it outputs nothing to the website, not even an html tag I am using apache2 with phpmyadmin and sql in conjunction with nginx I tested with just <?php echo 'Help!' ?> and that doesnt even work. edit: in the blank space it is supposed to be a table but it is not showing so I will put the code on pastebin instead.

You are using mysqli_* wrong. You are stuffing bind_result() where it doesn’t belong. I assume you are thinking that bind_result() and bind_param are the same thing which they AREN’T.

bind_result() is basically the equivalent of while($row = $query->fetch()) in PDO.

bind_param is only needed when there is client input such as

SELECT forum_id, forum_name FROM forum_table WHERE forum_id = ? LIMIT 1

That’s when you’ll need bind_param. Basically anywhere where there’s the WHERE clause since you need to prepare client input or you can potentially get SQL Injected.

Last but not least, <?php echo 'Help!' ?> is also wrong. You are missing the semi colon. <?php echo 'Help!'; ?> is the correct syntax.

Here’s the correct syntax for your snippet.

<?php
session_start();
require"db_connect.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled</title>
</head>
 
<body>
<div id="container">
    <table>
<?php
$sql = "SELECT forum_id, forum_name FROM forum_tabl"; // SQL String
$query = $db->prepare($sql); // Prepare the SQL String
$query->execute(); // Execute the prepared statement
$query->store_result(); // Store the result for later

// Check if anything exists
// num_rows and rowCount() DOES NOT NEED TO BE COMPARED TO!!!!
// num_rows already returns a 1 (true) or a 0 (false)
if($query->num_rows) {

    // If num_rows returns a 1, it'll automatically go into this statement

    // bind_result should always be kept inside num_rows since you are creating the variables
    // When there is data to display.
    $query->bind_result($_id, $f_name);

    // $row isn't needed in mysqli_* as all the variables are being appended and assigned above.
    while($query->fetch()) {
?>
        <tr>
            <td><?php echo $f_name;?></td>
        </tr>
<?php
    }

} else {

    // If num_rows returns a 0, it'll automatically go into this statement
?>
        <tr>
            <td>The records are empty.</td>
        </tr>
<?php
}

// There's no need to be redundant when using num_rows or rowCount() as it is default in this way.
// If you try and compare it to say 0 or 1, that's really redundant and can be re-factored to just this
// As it will return the EXACT results, but with no redundancy.
?>
    </table>
</div>
</body>
</html>

Tested it and it displays this for me.


Also, if nothing is showing on the page, then that means that there’s an error some where. Most likely from the db_connect.php file.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.