0Day Forums
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Database (https://0day.red/Forum-Database)
+---- Forum: MySQL (https://0day.red/Forum-MySQL)
+---- Thread: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource (/Thread-mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-mysql-num-rows-etc-expects-parameter-1-to-be-resource)

Pages: 1 2 3


mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - uncontentiousness66034 - 07-27-2023

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'];
}




RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - abet577 - 07-27-2023

Put quotes around `$username`. String values, as opposed to numeric values, must be enclosed in quotes.

$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");

Also, there is no point in using the `LIKE` condition if you're not using wildcards: if you need an exact match use `=` instead of `LIKE`.


RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - hardfeatured642 - 07-27-2023

Your code should be something like this

$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM Users WHERE UserName LIKE '$username'";
echo $query;
$result = mysql_query($query);

if($result === FALSE) {
die(mysql_error("error message for the user"));
}

while($row = mysql_fetch_array($result))
{
echo $row['FirstName'];
}

Once done with that, you would get the query printed on the screen. Try this query on your server and see if it produces the desired results. Most of the times the error is in the query. Rest of the code is correct.


RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - barbi130 - 07-27-2023

$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');

You define the string using single quotes and PHP does not parse single quote delimited strings. In order to obtain variable interpolation you will need to use double quotes OR string concatenation (or a combination there of). See

[To see links please register here]

for more information.

Also you should check that mysql_query returned a valid result resource, otherwise fetch_*, num_rows, etc will not work on the result as is not a result! IE:

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');

if( $result === FALSE ) {
trigger_error('Query failed returning error: '. mysql_error(),E_USER_ERROR);
} else {
while( $row = mysql_fetch_array($result) ) {
echo $row['username'];
}
}

[To see links please register here]

for more information.


RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - Florinda290423 - 07-27-2023

Please check once the database selected are not because some times database is not selected

Check



mysql_select_db('database name ')or DIE('Database name is not available!');

before MySQL query
and then go to next step

$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');

f($result === FALSE) {
die(mysql_error());


RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - dore44 - 07-27-2023

This query should work:


$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '%$username%'");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'];
}

The problem is single quotes, thus your query fails and returns FALSE and your WHILE loop can't execute. Using % allows you to match any results containing your string (such as SomeText-$username-SomeText).

This is simply an answer to your question, you should implement stuff mentioned in the other posts: error handling, use escape strings (users can type anything into the field, and you MUST make sure it is not arbitrary code), use [PDO][1] instead mysql_connect which is now depricated.


[1]:

[To see links please register here]




RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - hapaxes538263 - 07-27-2023

You can also check wether `$result` is failing like so, before executing the fetch array

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');
if(!$result)
{
echo "error executing query: "+mysql_error();
}else{
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'];
}
}


RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - hildie687 - 07-27-2023

Go to your `config.php`. I had the same problem. Verify the username and the password, and also sql select is the same name as the config.


RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - inger158617 - 07-27-2023

$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '%$username%'") or die(mysql_error());

while($row = mysql_fetch_array($result))
{
echo $row['FirstName'];
}

Sometimes suppressing the query as `@mysql_query(your query);`


RE: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource - iamb962729 - 07-27-2023

First, check your connection to the database. Is it connected successfully or not?

If it's done, then after that I have written this code, and it works well:

if (isset($_GET['q1mrks']) && isset($_GET['marks']) && isset($_GET['qt1'])) {
$Q1mrks = $_GET['q1mrks'];
$marks = $_GET['marks'];
$qt1 = $_GET['qt1'];

$qtype_qry = mysql_query("
SELECT *
FROM s_questiontypes
WHERE quetype_id = '$qt1'
");
$row = mysql_fetch_assoc($qtype_qry);
$qcode = $row['quetype_code'];

$sq_qry = "
SELECT *
FROM s_question
WHERE quetype_code = '$qcode'
ORDER BY RAND() LIMIT $Q1mrks
";
$sq_qry = mysql_query("
SELECT *
FROM s_question
WHERE quetype_code = '$qcode'
LIMIT $Q1mrks
");
while ($qrow = mysql_fetch_array($sq_qry)) {
$qm = $qrow['marks'] . "<br />";
$total += $qm . "<br />";
}
echo $total . "/" . $marks;
}