I am trying lớn select data from a MySQL table, but I get one of the following error messages:

mysql_fetch_array() expects parameter 1 to lớn be resource, boolean given

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)) emang lại $row<"FirstName">;

*

*

A query may fail for various reasons in which case both the mysql_* và the mysqli extension will return false from their respective query functions/methods. You need lớn chạy thử for that error condition and handle it accordingly.

Bạn đang xem: Php

mysql_* extension:

NOTE The mysql_ functions are deprecated and have sầu been removed in php version 7.

Chechồng $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 khổng lồ khuyến mãi with them.

Xem thêm: Quỹ Quốc Tế Bảo Vệ Thiên Nhiên ( Wwf Là Tổ Chức Gì, World Wide Fund For Nature

$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 handlingwhile($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 queryif($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_listelse { // 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_listelse if ( !$stmt->bind_param("s", $_POST<"username">) ) yourErrorHandler($stmt->error); // or $stmt->error_listelse if ( !$stmt->execute() ) yourErrorHandler($stmt->error); // or $stmt->error_listelse { $result = $stmt->get_result(); // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach foreach( $result as $row ) { ...These examples only illustrate what should be done (error handling), not how to lớn vày it. Production code shouldn"t use or die when outputting HTML, else it will (at the very least) generate invalid HTML. Also, database error messages shouldn"t be displayed khổng lồ non-admin users, as it discloses too much information.