Not doing the if statement PHP

655 views php
3

It does straight to 0 results found on my website instead of doing the echo $row any ideas? I want it to pull from my database(phpmyadmin) the database name is events_table on it.

     <?php

                         $servername = "localhost";
                         $dbname = "events_table";




                        $conn =  new mysqli($servername, $dbname);
                        $sql = "SELECT ID, Title, Description, Month FROM 
                          'events_table'";
                        $result = $conn->query($sql);


                       if (!$conn) {
                        die("Connection failed: " . mysqli_connect_error());

                            }   




                         if ($result = $conn->query($sql)){
                           echo "query success";


         while($row =  $result->fetch_assoc()) {
            echo $row["ID"],  $row["Title"], $row["Description"] ;
       }
                         }
                       else {

                       echo "0 Results";
                       }
                $conn->close();
                         ?>

answered question

2 Answers

2

Do not wrap your table name between '', use backtick ` instead, it's parse syntax error. So your query does not have 0 results, its failing.

You should really turn up your error reporting tho

posted this
13

You have $conn = new mysqli($servername, $dbname); which is missing some parameters

// Hostname: 127.0.0.1, username: your_user, password: your_pass, db: sakila
$mysqli = new mysqli('127.0.0.1', 'your_user', 'your_pass', 'sakila');

http://php.net/manual/en/mysqli.examples-basic.php

posted this

Have an answer?

JD

Please login first before posting an answer.