Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I am trying to select data from a MySQL table, but I get one of the following error messages:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
or
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
Call to a member function fetch_array() on boolean / non-object
This is my code:
$username = $_POST['username']; $password = $_POST['password']; $result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username'); while($row = mysql_fetch_array($result)) { echo $row['FirstName']; }
The same applies to code like
$result = mysqli_query($mysqli, 'SELECT ...'); // mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given while( $row=mysqli_fetch_array($result) ) { ...
and
$result = $mysqli->query($mysqli, 'SELECT ...'); // Call to a member function fetch_assoc() on a non-object while( $row=$result->fetch_assoc($result) ) { ...
$result = $pdo->query('SELECT ...', PDO::FETCH_ASSOC); // Invalid argument supplied for foreach() foreach( $result as $row ) { ...
$stmt = $mysqli->prepare('SELECT ...'); // Call to a member function bind_param() on a non-object $stmt->bind_param(...);
$stmt = $pdo->prepare('SELECT ...'); // Call to a member function bindParam() on a non-object $stmt->bindParam(...);
A query may fail for various reasons in which case both the mysql_* and the mysqli extension will return false from their respective query functions/methods. You need to test for that error condition and handle it accordingly.
false
mysql_* extension:
NOTE The mysql_ functions are deprecated and have been removed in php version 7.
Check $result before passing it to mysql_fetch_array. You'll find that it's false because the query failed. See the mysql_query documentation for possible return values and suggestions for how to deal with them.
$result
mysql_fetch_array
mysql_query
$username = mysql_real_escape_string($_POST['username']); $password = $_POST['password']; $result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'"); if($result === FALSE) { die(mysql_error()); // TODO: better error handling } while($row = mysql_fetch_array($result)) { echo $row['FirstName']; }
mysqli extensionprocedural style:
$username = mysqli_real_escape_string($mysqli, $_POST['username']); $result = mysqli_query($mysqli, "SELECT * FROM Users WHERE UserName LIKE '$username'"); // mysqli_query returns false if something went wrong with the query if($result === FALSE) { yourErrorHandler(mysqli_error($mysqli)); } else { // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach foreach( $result as $row ) { ...
oo-style:
$username = $mysqli->escape_string($_POST['username']); $result = $mysqli->query("SELECT * FROM Users WHERE UserName LIKE '$username'"); if($result === FALSE) { yourErrorHandler($mysqli->error); // or $mysqli->error_list } else { // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach foreach( $result as $row ) { ...
using a prepared statement:
$stmt = $mysqli->prepare('SELECT * FROM Users WHERE UserName LIKE ?'); if ( !$stmt ) { yourErrorHandler($mysqli->error); // or $mysqli->error_list } else if ( !$stmt->bind_param('s', $_POST['username']) ) { yourErrorHandler($stmt->error); // or $stmt->error_list } else if ( !$stmt->execute() ) { yourErrorHandler
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.