warning problem: expects parameter 1 to be mysqli_result
mysqli_query()
returns FALSE
if there was an error in the query. So you should test for it...
/* Select queries return a resultset */
if ($result = mysqli_query($dbc, "SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", $result->num_rows);
/* free result set */
$result->close();
}
See this link for the mysqli_query
reference
http://php.net/manual/en/mysqli.query.php
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in
mysqli_fetch_array()
's 1st parameter must be a result of a query. What you are doing is you are passing the connection (which doesn't make sense) and the query command itself.
Read the doc here: http://php.net/manual/en/mysqli-result.fetch-array.php
To fix this, execute the query first, then store the result to a variable then later fetch that variable.
$sql = "select * from privinsi";
$result = mysqli_query($connection,$sql);
while($r = mysqli_fetch_array($result))
{
// your code here
}
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in on line 21
The mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both.
Basically replaces this loop here:
$query = "SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$res = $con->query($query);
while ($row = $res->fetch_assoc()) {
//do something
}
With this:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql);
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);
// Free result set
mysqli_free_result($result);
mysqli_close($con);
?>
So in your case you just have to take the result of the query and (since I can see you have specified you want it as associative array choose that option) it would look like this:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
//do something
}
error mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given
Problem
You are missing how to pass the argument to mysqli_fetch_array()
.
Solution
Therefore, this line:
if(mysqli_query($cons, $result)) {
should be
if($res = mysqli_query($cons, $result)) { // assign the return value of mysqli_query to $res
(FWIW, I'd go with $res = mysqli_query($cons, $result);
then do if($res) {
.)
And then do
while($row = mysqli_fetch_array($res)) // pass $res to mysqli_fetch_array instead of the query itself
Why?
You were giving to mysqli_fetch_array()
- as an argument - the string
that contains your query. That's not how it works. You should pass the return value of mysqli_query()
instead. Therefore, you could also write: while($row = mysqli_fetch_array(mysqli_query($cons, $result))) {}
(but it's not adviced, it is just to show you how it works).
getting warning mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, array given
Try changing your code to:
...
if (!empty($result)) { // check if result is empty or not..
if (mysqli_num_rows($result)>0) {
while ($r = mysqli_fetch_assoc($result)) {
// changing $result to $output to avoid confusions in using same array for printing.
$output = array(
'email' => $r['email']
);
array_push($all, $output);
}
}
else {
echo '{"success":0}';
}
else {
print "No results: " . $sql_query; // check whats the issue with query
}
...
Related Topics
How to Join Three Tables in Codeigniter
Escaping Curl @ Symbol with PHP
Call Parent Constructor Before Child Constructor in PHP
PHP $_Server['Remote_Addr'] Shows Ipv6
Why Does (0 == 'Hello') Return True in PHP
What Does PHP Do with Deprecated Functions
Increment Behavior on Strings - PHP Easter Egg
How to Merge Subarray Values and Remove Duplicates
Is There Any Legitimate Use for Bare Strings in PHP
Installing Mcrypt on Osx 10.8.2 - PHP 5.3.15 with Homebrew
Php: What Is the Complexity [I.E O(1),O(N)] of the Function 'Count'
How to Upload a File Using Jquery's $.Ajax Function with JSON and PHP
Php: What If I Call a Static Method in Non-Static Way
How to Check Whether Suhosin Is Installed
PHP Date - Get Name of the Months in Local Language
Sqlite Database Hosted on Heroku Getting Automatically Reset