0

Pic

So I'm new to mysqli. All of the examples I find online seem to be the old (procedural) way of doing things. Can someone tell me why my code isn't working below? My db is 'templatedb'. My Table is 'template'. I have one entry in my table, but I'm receiving no output with my echo. I'm not getting any errors with my code.

        <div id="templateSelector">  
        <?php
        $hostname = "localhost";
        $username = "root";
        $password = "";
        $db = "templatedb";
        //connect
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        $database = mysqli_connect($hostname, $username, $password, $db);
        if(!$database){
            die("Could not connect to the database");                
        }

        if ($database->connect_errno) {
            echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        } else {
            $sql = "SELECT * FROM template";
            if (!$result = $database->query($sql)) {
                die('There was an error running the query [' . $db->error . ']');
            } else {

                echo "<label>Select Template</label>";
                echo "<select name='templates'>";
                while ($row = $result->fetch_assoc()) {
                    echo "hello";
                    echo $row['template_name'];
                    // echo "<option value='" . $row['template'] . "'>" . $row['template'] . "</option>";
                }
                echo "</select>";
            }
        }
        ?>
Kaboom
  • 674
  • 6
  • 27
CoupFlu
  • 311
  • 4
  • 20

1 Answers1

-2

Try doing the following, worked for me

<div id="templateSelector">  
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "templatedb";
$mysqli = mysqli_connect($hostname, $username, $password, $db);
if($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
    $sql = "SELECT * FROM template";
    $result = mysqli_query($mysqli, $sql);
    if(!$result = $mysqli->query($sql)) {
        die('There was an error running the query [' . $db->error . ']');
    } else {
        echo "<label>Select Template</label>\n";
        echo "<select name='templates'>\n";
        while($row = $result->fetch_assoc()) {
            echo "<option>id = " . $row['id'] . "</option>\n";
        }
        echo "</select>";
    }
}
?>
</div>

enter image description here

Make sure your DATABASE is called templatedb and the table it is in is called template and there is a row called id. I know that sounds trivial, but spelling mistakes will break your code.

Kaboom
  • 674
  • 6
  • 27
  • I did, still no output from the DB. – CoupFlu Feb 16 '17 at 16:49
  • I did sir, see the pic of the db structure. – CoupFlu Feb 16 '17 at 16:50
  • Check your spelling and make sure there is at least 1 row in the database. This code should work assuming everything is correct (make sure mysql username and password are right, you have permission, etc) – Kaboom Feb 16 '17 at 16:50
  • If the code assumed to be a working one, you shouldn't post the same code as an answer. – Your Common Sense Feb 16 '17 at 16:52
  • I made this same table on my personal test machine, ran this same chunk of code i put in my answer, and printed out a proper value. not sure why its not working for him.. – Kaboom Feb 16 '17 at 16:53
  • I added an image to show that the code above does work. Assuming correct username and password for the database, you should expect the same result. – Kaboom Feb 16 '17 at 17:02
  • I had modified my code to use the fetch_object instead uf fetch_assoc(). I forgot to change it back when testing your code. Your code does in fact work. Thanks – CoupFlu Feb 16 '17 at 18:58