Error mysql fetch array expects parameter 1 to be resource boolean given

0 votes

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'];
}
Sep 16, 2020 in PHP by kartik
• 37,520 points
4,016 views

1 answer to this question.

0 votes

Hello @kartik,

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)."'
");

It's 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.

Hope it helps!!
Thank you!!

answered Sep 16, 2020 by Niroj
• 82,840 points
Hello, im facing the same error but the above solution didn't worked for me.
Can anyone suggest me the other solution

Hello,

Please share your mysql query so that i can identify your error

Related Questions In PHP

0 votes
1 answer

How to resolve “must be an instance of string, string given” prior to PHP 7?

Hello, Prior to PHP 7 type hinting can only be ...READ MORE

answered Apr 20, 2020 in PHP by I Navin
• 220 points
5,167 views
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,840 points
1,846 views
0 votes
0 answers

Warning: mysqli_query() expects at least 2 parameters, 1 given. What?

I have to build a PHP page ...READ MORE

May 12, 2022 in PHP by Kichu
• 19,040 points
1,279 views
0 votes
0 answers
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,090 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,018 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,951 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,894 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP