mysqli_fetch_array parameter error - php

I have this problem where I am trying to get input from the user and then display information. The user is meant to insert an ID number, obtained by $_POST[PID], then I catch that in result and when I try to get the result and print their info. This is the code:
$result = mysqli_query($connection, $_POST[PID]);
while($row = mysqli_fetch_array($result))
{
echo some info
echo "<br>";
}
However such code yields this error:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
How can I fix such error such that I am able to obtain the input and use it in mysqli_fetch_array

The second argument for mysqli_query() must be a query.
For instance:
$var = "insert into tbl where field = '{$_POST['PID']}'";
$result = mysqli_query($connect, $var);
Read it here:
http://www.w3schools.com/php/func_mysqli_query.asp

Refer to my answer from your previous question(similar to this question)
Obtain resource from POST php

Please learn about these functions to properly use them (Link here)
Instead of $_POST[PID] in mysqli_query() there must be some query string like
"select * from tablename where id ='". $_POST['PID']."'
Also, you need to enclose the key name within single quotes like $_POST['PID']

Related

How can I fetch data from the database using mysqli_fetch_all?

if(isset($_POST['saveUserName'])){
$newUserName = $_POST['newUserName'];
$id = $_SESSION['id'];
$sql = "UPDATE users SET user='$newUserName' WHERE id='$id';";
$result = mysqli_query($conn, $sql);
$result = mysqli_fetch_all($result);
}
This code shows me the following error:
Warning: mysqli_fetch_all() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\loginsystem\modify.php on line 13
Can you tell me what's wrong in this code ???
Thank you..
mysqli_query() function returns,
For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object. For other successful queries it will return TRUE. FALSE on failure.
Here, $result contains true or false. That's why you got that particular error message.
The mysqli_fetch_all() function fetches all result rows and returns the result-set as an associative array, a numeric array, or both.
If you want to get a result set then you need to change the query to select or show something.

Error Query PHP after migrate to mysql database

I have execute query using PHP which previously executed on mssql server database . Now with the same table and data. I using mysql database to execute my query. But error happen. Any suggestion for my query below in order to can execute using mysql database :
$year = mysql_query("SELECT * FROM education_year ORDER BY id DESC");
if (isset($_GET['year'])){
$educationyear= mysql_fetch_array(mysql_query("SELECT * FROM educationyear WHERE year='{$_GET['year']}'"));
}else {$educationyear = mysql_fetch_array($year);}
$kode['KODE'] = mysql_fetch_array(mysql_query("SELECT KODE FROM educationyear WHERE year='$educationyear'"));
$result = mysql_query("SELECT * FROM Province");
while($row = mysql_fetch_array($result))
{
$xd = mysql_fetch_array(mysql_query("SELECT COUNT (*) AS total FROM child WHERE id_province='{$row['province_code']}' AND education='A'
AND educationyear='{$educationyear['KODE']}'"));
}
Error message like below :
Notice: Array to string conversion in C:\xampp\htdocs\xy\demo.php on line 19
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xy\demo.php on line 20 .
Its line when execute $xd query.
There are a few problems with your code
1st: When you use an array within double-quoted string, do not quote the array key. Change
"...WHERE year='{$_GET['year']}..."
"...WHERE id_province='{$row['province_code']}'..."
To:
"...WHERE year='{$_GET[year]}..."
"...WHERE id_province='{$row[province_code]}'..."
2nd: The design pattern below is not good:
mysql_fetch_array(mysql_query("SELECT...")
You're taking the result of mysql_query and feeding it directly to mysql_fetch_array. This works as long as the query succeeds and returns a resource. If the query fails, it will return FALSE and mysql_fetch_array will trigger the error you see:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
Instead, make sure there is no error before proceeding
$result = mysql_query("SELECT...")
if($result===false){
//Query failed get error from mysql_error($link).
//$link is the result of mysql_connect
}
else{
//now it's safe to fetch results
$record = mysql_fetch_array($result);
}
3rd: do not use mysql_ functions. They have been abandoned for years and have been removed from the most recent version of PHP. Switch to MySQLi or PDO
4th: learn about prepared statements. You're using user supplied input directly in your query ($_GET['year']) and this makes you vulnerable to SQL injection.

PHP mysql_fetch_assoc() error

I am trying to retrieve data that is collected in a function, that contains my query. I am returning the data back to the main page and printing it using a while loop however i am getting this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given
try to return your result like
return $query;
cause you are returning string(your mysql_result($query, 0) returning 0th position column value only), need a query result for mysql_fetch_assoc()
try the corrected function:
function get5star(){
$strSQL = "SELECT * FROM5starproducts";
$query = mysql_query($strSQL);
return $query;
}

PHP mysqli_fetch_array supposedly getting a boolean

$result = mysqli_query($conn,"SELECT * FROM Players");
if ($result !== FALSE) {
while($row = mysqli_fetch_array($result)) {
$result = mysqli_query($conn,"UPDATE Players SET Score='$score' WHERE ID='$id'");
}
}
This works. That is, the databse is indeed updated and everything is all cool.
But it throws a warning:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
boolean given
If you search around, the explanation is that the query is failing - hence, it is returning FALSE, so you get the warning.
... But this doesn't make sense in my case. The query is not failing. When I run this script, my database is updated just fine. Besides, there is also a conditional checking if the result is a boolean before using mysqli_fetch_array, so technically this warning should never happen in the first place.
Whatever, the problem must be with $result. Let's do:
echo gettype($result);
Which results in
"object"
Well, this explains why is it passing the condition. However, this still won't explain why mysqli_fetch_array insists this is a boolean (because it isn't).
What is the problem, then?
Tested with PHP Version 5.3.24 and 5.4.19.
You're overwriting the resultset $result from your SELECT query with a new $result value from the UPDATE query inside your loop
while($row = mysqli_fetch_array($result)) {
$result2 = mysqli_query($conn,"UPDATE Players SET Score='$score' WHERE ID='$id'");
}
In while loop used Same variable name '$result' which are already used before. Change variable name inside loop.
while($row = mysqli_fetch_array($result)) {
$result = mysqli_query($conn,"UPDATE Players SET Score='$score' WHERE ID='$id'");
}
The $result array returns nothing when you are updating the table. That's why, you are getting this warning in the while loop when it is trying to fetch data from $result into $row.

"mysql_fetch_assoc()" error when data in mysql field is changed

I have a mySQL database from where I fetch some data via PHP.
This is what I've got:
if ($db_found) {
$URL_ID = $_GET["a"];
$SQL = "SELECT * FROM tb_employees WHERE URL_ID = $URL_ID";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
$firstname = $db_field['firstname'];
$surname = $db_field['surname'];
$function = $db_field['function'];
$email = $db_field['email'];
$telnr = $db_field['telnr'];
}
mysql_close($db_handle);
}
else {
print "Database not found... please try again later.";
mysql_close($db_handle);
}
The URL_ID field in my mySQL database is, for this example, 001. When I go to www.mydomain.com/index.php?a=001 it fetches all the data, puts it into a variable, and I can echo the variables without any problem.
Now, I want to change the URL_ID, and I've changed it to "62ac1175" in the mySQL database. However, when I proceed to www.mydomain.com/index.php?a=62ac1175, I get this error message:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource,
boolean given in
mydomain.com\db_connect.php on line 17
The field in mySQL has varchar(8) as type and utf8_general_ci as collation.
If I change the entry back to 001 and change my URL to ?a=001, it works fine again.
What's going wrong?
You are not doing any error checking in your query, so it's no wonder it breaks if the query fails. How to add proper error checking is outlined in the manual on mysql_query() or in this reference question.
Example:
$result = mysql_query($SQL);
if (!$result)
{ trigger_error("mySQL error: ".mysql_error());
die(); }
your query is breaking because you aren't wrapping the input in quotes. You can avoid* quotes only for integers (which 62ac1175 is not). Try
$SQL = "SELECT * FROM tb_employees WHERE URL_ID = '$URL_ID'";
Also, the code you show is vulnerable to SQL injection. Use the proper sanitation method of your library (like mysql_real_escape_string() for the classic mysql library that you are using), or switch to PDO and prepared statements.
In your code, this would look like so: Instead of
$URL_ID = $_GET["a"];
do
$URL_ID = mysql_real_escape_string($_GET["a"]);
* however, if you avoid quotes, mysql_real_escape_string() won't work and you need to check manually whether the parameter actually is an integer.

Categories