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;
}
Related
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.
$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.
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']
The function below will be used to input a Facebook access token into a database. The user id will already have an associated record so the "acc_tok" field just needs to be updated.
For some reason even though the $_result value holds "1" and the function echoes out "Successful!", a warning is appearing that says:
"Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given". Does anyone know why it appears that the query was successful but is only returning a boolean and not something the mysql_fetch_array can work with? Thanks for reading
function setUserAccessToken($_uid, $_accTok){
$sql = "UPDATE `user_core` SET `acc_tok`=$_accTok WHERE `id` = $_uid";
$_result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
echo $_result;
if ($_result) {
echo ("Successful!");
$_resultArray = mysql_fetch_array($_result);
print_r($_resultArray);
} else {
echo ("Failed!");
}
}
You're doing an UPDATE query, there are no rows to fetch from the result, hence a boolean value from mysql_query() and not a resource.
From the manual:
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error.
I wanted to arrange the array of table list with sort() function but i am getting same kind of warning.
<?php
require_once("lib/connection.php");
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
sort($result);
foreach ($result as $result){
echo $result ;
}
?>
and the warning I am getting are:
Warning: sort() expects parameter 1 to be array, resource given in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 9
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 10
The warning is pretty clear: mysql_query does not return an array with results from the query, but a resource. You need a function like mysql_fetch_array() to return the data you need (and on which you can perform a sort operation).
See the manual for the use of mysql_query() http://nl3.php.net/mysql_query
And maybe unrelated, but you can sort your results in MySQL right away by adding ORDER BY <fieldname> to your query.
The variable $result is only a resource of the type result. You need to fetch then the data from the result set with e.g. mysql_fetch_assoc().
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row["Tables_in_st_db_1"];
}
sort($array);
foreach ($array as $item) {
echo $item;
}
I'm not providing the most efficient code imaginable, but this should make it clear what's going on and solve your problem:
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$my_array_of_table_names = array();
while ( $row = mysql_fetch_array($result, MYSQL_NUM)) {
$my_array_of_table_names[] = $row[0];
}
sort($my_array_of_table_names);
foreach ($my_array_of_table_names as $table_name){
echo "$table_name\n";
}
Your problem is that you aren't actually getting the data from the query.
mysql_query() doesn't give you a recordset.
What it does is query the database and returns a database resource which you can then use to get the data.
What you need is after calling mysql_query(), you then need to also call mysql_fetch_array() or similar. (there are a range of functions available, but that's probably the best one to use in this case). Then sort() the data from that, not $result.
It clearly says: it expects an array and you pass something else.
If you had checked the type of $result you would have seen that it is not an array, intead a resource.