For some reason this php block keeps returning this error and I cant figure out why; i've setting out the same code in multiple styles and paradigms but it keeps returning the error
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result
But when I change the code to utilise the mysqli_result function php says it's undefined.
php block:
global $connection;
$query ="SELECT * FROM Members AND EXISTS (SELECT * FROM Lead);";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
while($row){
echo "<li>".$row['FIRST_NAME']."</li>";
}
1.Replace AND with WHERE inside query
2.Put $row = mysqli_fetch_assoc($result); inside while()
Do like below:-
global $connection;
$query ="SELECT * FROM Members WHERE EXISTS (SELECT * FROM Lead);";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)){
echo "<li>".$row['FIRST_NAME']."</li>";
}
Related
Is there any way I can use the array from $query and use it in another query like the $query2 below? It works but it only returns the first data of the array...
Already tried these links already but none of them worked in my case
Array in SQL Query?
How to query a database with an array? WHERE = 'array()'
$query = mysqli_query($db, "SELECT * FROM patient_details WHERE discharged_date >= '2019-03-01' AND discharged_date < '2019-03-30'");
while ($row = mysqli_fetch_array($query)){
$result = $row['hosp_patient_no'];
$query2 = mysqli_query($db, "SELECT deficiencies_id FROM deficiency_patient_details WHERE hosp_patient_no = '$result'");
}
you are using mysqli_fetch_array which will "Fetches a result row as an associative, a numeric array, or both", but your requirement says it otherwise so you should use mysqli_fetch_all which "Fetches all result rows as an associative array, a numeric array, or both".
so your syntax should look like this
$query = mysqli_query($db, "SELECT * FROM patient_details WHERE discharged_date >= '2019-03-01' AND discharged_date < '2019-03-30'");
while ($row = mysqli_fetch_all($query)){
$result = $row['hosp_patient_no'];
$query2 = mysqli_query($db, "SELECT deficiencies_id FROM deficiency_patient_details WHERE hosp_patient_no = '$result'");
}
infact i would suggest you to do this in a single query using subquery.
After making some modifications to my existing script, I created an error:
Notice: Trying to get property of non-object...
I am trying to figure how to identify if the variable $result is an object so I don't get this error (I am getting the error on this particular line if ($result-> num_rows > 0) { ). Here is what have:
<?php
$sql = "SELECT * FROM input WHERE id =".$_GET["id"];
$result = mysqli_query ($conn,$sql);
if ($result-> num_rows > 0) {
while($row -> $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql2 = "SELECT * FROM output WHERE question_id = $myid ORDER BY date DESC";
$result2 = $conn->query($sql2);
$sql3 = "SELECT COUNT(*) as rowCount FROM output WHERE question_id = '".$myid."'";
$result3 = $conn->query($sql3);
$rowCount= $result3->fetch_assoc();
?>
How can I tell beforehand if this is an object or not?
First of all, do you have the variable $conn initialized any where in the code before that?
Second, you want to make your while loop like this:
while($row = $result->fetch_assoc()) { ... }
You can use to find content is array or object :
is_object() to find object
is_array() to find array
$sql = "SELECT * FROM compt WHERE id =1";
$result = mysqli_query ($conn,$sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
}
}
I know that inside MySQL, you can use:
SELECT COUNT(*) FROM table
I have written the following code in PHP to display the number of rows on the page:
$sql = 'select * from users';
$data = $conn -> query($sql);
echo $data;
But when I run it, I get the following error:
Catchable fatal error: Object of class PDOStatement could not be converted to string in [Directory] on line 19.
I think the problem is that the returned value is not in string form. If that is correct, how would I be able to display the number of rows on the page?
If you want to count the rows you can do this with PDO:
$sql = 'select * from users';
$data = $conn->query($sql);
$rows = $data->fetchAll();
$num_rows = count($rows);
Well, you arent badly off, you are almost there:
$sql = 'SELECT COUNT(*) as numrow FROM users';
$data = $conn -> query($sql);
rows = $data->fetchAll();
Depending on the type of return data, you could use
$rows->numrow if the return data is an object
There are some easy and faster way to do this
COUNT the column in query and fetch
$sql = "SELECT COUNT(id) AS total_row FROM table_name";
$stmt = $conn->query($sql);
$stmt->execute();
echo $count['total_row'];
Using rowCount()
$sql = "SELECT * FROM table_name";
$stmt = $conn->query($sql);
$stmt->execute();
$count = $stmt->rowCount();
echo $count;
I am trying to import MySQL data into PHP through the Wordpress PHP Snippet plugin. For whatever reason I keep getting error 'mysql_fetch_array() expects parameter 1 to be resource, boolean'.
My code is as follows:
Connection
[insert_php]
$conn = mysql_connect("localhost", "albert", "notrealpassword") or die
(mysql_error());
PHP
mysql_select_db('mydatabase');
$query = "SELECT * FROM mytable";
$result = mysql_query($query);
while ($subjectone = mysql_fetch_array($result))
{echo $subjectone['dataintable'];}
[/insert_php]
It is because before the last result the answer of mysql_fetch_array is array but while waits for bolean.
Also probably you know it is better to use new MySQLi functions
mysql_select_db('mydatabase');
$query = "SELECT * FROM mytable";
$result = mysql_query($query);
while (is_array($subjectone = mysql_fetch_array($result)))
{echo $subjectone['dataintable'];}
ihave created a simple project to help me get to grips with php and mysql, but have run into a minor issue, i have a working solution but would like to understand why i cannot run this code successfully this way, ill explain:
i have a function,
function fetch_all_movies(){
global $connection;
$query = 'select distinct * FROM `'.TABLE_MOVIE.'` ORDER BY movieName ASC';
$stmt = mysqli_prepare($connection,$query);
mysqli_execute($stmt);
mysqli_stmt_bind_result($stmt,$id,$name,$genre,$date,$year);
while(mysqli_stmt_fetch($stmt)){
$editUrl = "index.php?a=editMovie&movieId=".$id."";
$delUrl = "index.php?a=delMovie&movieId=".$id."";
echo "<tr><td>".$id."</td><td>".$name."</td><td>".$date."</td><td>".get_actors($id)."</td><td>Edit | Delete</td></tr>";
}
}
this fetches all the movies in my db, then i wish to get the count of actors for each film, so i pass in the get_actors($id) function which gets the movie id and then gives me the count of how many actors are realted to a film.
here is the function for that:
function get_actors($movieId){
global $connection;
$query = 'SELECT DISTINCT COUNT(*) FROM `'.TABLE_ACTORS.'` WHERE movieId = "'.$movieId.'"';
$result = mysqli_query($connection,$query);
$row = mysqli_fetch_array($result);
return $row[0];
}
the functions both work perfect when called separately, i just would like to understand when i pass the function inside a function i get this warning:
Warning: mysqli_fetch_array() expects
parameter 1 to be mysqli_result,
boolean given in
/Applications/MAMP/htdocs/movie_db/includes/functions.inc.php
on line 287
could anyone help me understand why?
many thanks.
mysqli_query failed to run your query:
Returns FALSE on failure. For
successful SELECT, SHOW, DESCRIBE or
EXPLAIN queries mysqli_query() will
return a result object. For other
successful queries mysqli_query() will
return TRUE.
Before running mysqli_fetch_array test $result... Something like:
if ($result !== false)
$row = mysqli_fetch_array($result);
else
return false;
Seems like a variable scope issue within your SQL statement. Outputting the SQL should show you the "true" error.
You may want to try using classes with your functions, for example:
class getInfo {
function fetch_all_movies(){
global $connection;
$query = 'select distinct * FROM `'.TABLE_MOVIE.'` ORDER BY movieName ASC';
$stmt = mysqli_prepare($connection,$query);
mysqli_execute($stmt);
mysqli_stmt_bind_result($stmt,$id,$name,$genre,$date,$year);
while(mysqli_stmt_fetch($stmt)){
$editUrl = "index.php?a=editMovie&movieId=".$id."";
$delUrl = "index.php?a=delMovie&movieId=".$id."";
echo "<tr><td>".$id."</td><td>".$name."</td><td>".$date."</td><td>".get_actors($id)."</td><td>Edit | Delete</td></tr>";
}
}
function get_actors($movieId){
global $connection;
$query = 'SELECT DISTINCT COUNT(*) FROM `'.TABLE_ACTORS.'` WHERE movieId = "'.$movieId.'"';
$result = mysqli_query($connection,$query);
$row = mysqli_fetch_array($result);
return $row[0];
}
}
$showInfo = new getInfo;
//fetch all movies
$showInfo->fetch_all_movies();
//List actors from movie 1
$showInfo->get_actors("1");
In case of an error mysqli_query will return false. You have to handle the error a simple way to do this might be:
$result = mysqli_query($connection,$query);
if (!$result) {
die(mysqli_error($connection));
}
$row = mysqli_fetch_array($result);
Please note that terminating (by doing die() ) usually is no good way to react on an error, log it, give the user anice error page etc. It's alsonogood practice to give the low level error message toauser, this might motivate users to try to exploit a possile security issue.
Last remark: you were writing
$query = 'SELECT DISTINCT COUNT(*) FROM `'.TABLE_ACTORS.'` WHERE movieId = "'.$movieId.'"';
you should properly escape the movieId there.