0

Full PHP Code:

<?php
        $servername = "localhost";
        $username = "*****";
        $password = "**********";
        $dbname = "x1_1";

        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $dbname);

        // Check connection
        if (!$conn) {
                die("Connection failed: " . mysqli_connect_error());
        }
        // Print out to console success
        echo '<script>console.log("Connected successfully! ")</script>';

        // sql to create table
        $sql = "CREATE TABLE MyGuests (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50),
            reg_date TIMESTAMP
        )";

        // Check if table was created
        if ($conn->query($sql) === TRUE) {
            // Was created
                echo '<script>console.log("Table MyGuests created successfully! ")</script>';
        } else {
            // Was not created
                echo '<script>console.log("Error creating table: ' . $conn->error . '")</script>';
        }

        // Insert data into table
        $sql = "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('John', 'Doe', 'john@example.com');";

        $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('Mary', 'Moe', 'mary@example.com');";

        $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('Julie', 'Dooley', 'julie@example.com')";

        // Check if values were inserted successfully
        if ($conn->multi_query($sql) === TRUE) {
            // Inserted successfully
                echo '<script>console.log("New records created successfully! ")</script>';
        } else {
            // Was not inserted successfully
                echo '<script>console.log("Error: '. $sql .' "<br>" ' . $conn->error . '")</script>';
        }

        // Selects [ID, Firstname, and Lastname] from table
        $sql = "SELECT id, firstname, lastname FROM MyGuests";
        $result = $conn->query($sql);

        // If the number of rows is greater than zero print the data in the row. Else print zero results
        if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
                }
        } else {
                echo "0 results";
        }

        $conn->close();
    ?>

Pointing Towards Specific Problem PHP Code:

        // Selects [ID, Firstname, and Lastname] from table
        $sql = "SELECT id, firstname, lastname FROM MyGuests";
        $result = $conn->query($sql);

        // If the number of rows is greater than zero print the data in the row. Else print zero results
        if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
                }
        } else {
                echo "0 results";
        }

My console prints out:

-Connected successfully!

-Error creating table: 'MyGuests' already exists -This error is normal and not apart of the problem

-New records created successfully!

But on the document it says 0 results.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • add `()` after `num_rows` in if condition as `if ($result->num_rows() > 0)` – Sachin PATIL Apr 13 '17 at 18:48
  • That caused an internal server error. –  Apr 13 '17 at 18:50
  • num_rows is a property, not a method, so no (). Add a line just before the ```if($result```... line to echo the value of $result->num_rows. – Sloan Thrasher Apr 13 '17 at 18:55
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 13 '17 at 18:58
  • @SloanThrasher echo ''; returns internal error –  Apr 13 '17 at 19:54
  • Of course it does. That's not correct syntax. Try ```echo $result->num_rows;``` without the other stuff. You are mixing javascript and php code in what you used. – Sloan Thrasher Apr 13 '17 at 22:40
  • I tried that @SloanThrasher but nothing was echoed so I tried to echo it to console. –  Apr 13 '17 at 23:53
  • That would not echo to the console. Try ```echo "";``` – Sloan Thrasher Apr 14 '17 at 03:04
  • It prints to console just a blank line: http://imgur.com/a/kSBHI –  Apr 14 '17 at 11:47

0 Answers0