Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to learn mysqli, none of my statements are actually returning results
    primarykey
    data
    text
    <p>Exactly what it sounds like, none of my retrieval statements retrieve the data as it should. This is peculiar, as if I copy the exact query and put it into phpMyAdmin and run it there, it returns results as it should.</p> <p>Insertion statements and the like work, such as this:</p> <pre><code> // On first connect to database, create a user to hold data for users not logged in $result = $db-&gt;query("SELECT id FROM users WHERE id = 1"); if ($result-&gt;num_rows == 0) { $db-&gt;query("INSERT INTO users (id, username, email, password) VALUES (1, 'anonymous', 'anonymous', '" . password_hash("...", PASSWORD_BCRYPT) . "')"); } </code></pre> <p>And the user is entered as intended.</p> <p>But if I run a statement such as this:</p> <pre><code> $result = $db-&gt;query("SELECT id, username, password FROM users WHERE email = $email"); $row = $result-&gt;fetch(); </code></pre> <p>I get the following error: "<strong>Fatal error:</strong> Call to a member function fetch() on a non-object"</p> <p>Running the exact same statement in phpMyAdmin returns the row as intended.</p> <p>It only works in one area, however:</p> <pre><code> if ($result = $db-&gt;query("SELECT username, wins, losses FROM users ORDER BY wins DESC LIMIT 10")) { while ($row = $result-&gt;fetch_assoc()) { echo "&lt;tr&gt;&lt;td&gt;" . $row["username"] . "&lt;/td&gt; &lt;td&gt;" . $row["wins"] . "&lt;/td&gt; &lt;td&gt;" . $row["losses"] . "&lt;/td&gt;&lt;/tr&gt;\n"; } } </code></pre> <p>Which returns the intended results. I don't know why, though.</p> <p>I'm not sure if the structure of my files matters, I have a config.php file that I require in and it has the connection information. The rest is in my index.php file.</p> <p>Here's config.php:</p> <pre><code>&lt;?php $db_host = $_SERVER["SERVER_NAME"]; $db_user = "..."; $db_pass = "..."; $db_database = "hangman"; $db = new mysqli($db_host, $db_user, $db_pass, $db_database); if ($db-&gt;connect_error) { echo "Could not connect to database."; exit; } // On first connect to database, create a user to hold data for users not logged in $result = $db-&gt;query("SELECT id FROM users WHERE id = 1"); if ($result-&gt;num_rows == 0) { $db-&gt;query("INSERT INTO users (id, username, email, password) VALUES (1, 'anonymous', 'anonymous', '" . password_hash("...", PASSWORD_BCRYPT) . "')"); } ?&gt; </code></pre> <p>I can include more of my PHP, please just say what, this is just driving me crazy.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload