If ($result->NUM_ROWS > 0) not working


#1

hey, i’m curently using html and php to build a litle web site with a data base, i was able to make it work in XAMPP but when i tranfer the code to web host it starts giving wierd erros

<?php error_reporting(E_ALL); ?> <?php $user = '##########'; $pass = '##########'; $db = 'users'; $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); $sql = "SELECT id_user, name, nick FROM user"; $result = $db->query($sql); if ($result->NUM_ROWS > 0) { while($row = $result->fetch_assoc()) { echo "id: " . $row["id_user"]. " - Name: " . $row["name"]. " - Nick: " . $row["nick"]. "
"; } } else { echo "0 results"; } if (isset($_GET['Insert'])) { $name = $_POST["username"]; $nick = $_POST["nick"]; $db = new mysqli('localhost', '#########', '########', 'users') or die("Unable to connect"); $sql = "INSERT INTO user (id_user, name, nick) VALUES (DEFAULT, '$name', '$nick')"; if ($db->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "
" . $db->error; } } ?>

#2

Class properties are usually case sensitive in PHP, so you should use $result->num_rows …
PS: Please, format code properly when posting next time. Thanks! :slight_smile: