Error mysql fetch array mysql fetch assoc mysql fetch row mysql num rows etc expects parameter 1 to be resource

0 votes

Hii team,

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

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'];
}
May 19, 2020 in PHP by kartik
• 37,530 points
467 views

1 answer to this question.

0 votes

Hello,

Error occurred here was due to the use of single quotes ('). 

You can put your query like this:

mysql_query("
SELECT * FROM Users 
WHERE UserName 
LIKE '".mysql_real_escape_string ($username)."'
");
  • Using mysql_real_escape_string for prevention of SQL injection. 
  • Though we should use MySQLi or PDO_MYSQL extension for upgraded version of PHP (PHP 5.5.0 and later), but for older versions mysql_real_escape_string will do the trick.

Thank you!!

answered May 19, 2020 by Niroj
• 82,880 points

Related Questions In PHP

0 votes
1 answer

Error: Failed to load resource: net::ERR_CACHE_MISS

Hello @kartik, It is actually a permission issue. ...READ MORE

answered Jun 16, 2020 in PHP by Niroj
• 82,880 points
894 views
0 votes
1 answer

Error: Global Variable is not accessable to local function

Hey kartik, A variable declared outside a function has a ...READ MORE

answered Feb 19, 2020 in PHP by Niroj
• 82,880 points
212 views
0 votes
0 answers
0 votes
1 answer

Error:Lost connection to MySQL server at 'reading initial communication packet', system?

Hello, You have to follow the below steps: bind-address ...READ MORE

answered Apr 9, 2020 in PHP by Niroj
• 82,880 points
8,822 views
0 votes
1 answer

What is Laravel framework? Why one should use Laravel?

Laravel is a PHP web-framework; it utilized ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
550 views
0 votes
1 answer

How to download and install Lavavel framework?

Hey @kartik, First you must have xampp install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
456 views
0 votes
1 answer

Display Laravel in browser by using cmd promt?

Hello, First you need to have laravel install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
244 views
0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,880 points
243 views
0 votes
1 answer

Error:mysql_fetch_array() expects parameter 1 to be resource, boolean given

Hello @kartik, Error occurred here was due to ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,880 points
1,061 views
0 votes
1 answer