I am getting an error that says Call to a member function fetch_array() on boolean on line 23.
Line 23 consists of this line of code
$row = $query->fetch_assoc();
Here is the whole block
if(!filter_has_var(INPUT_GET, 'id')) {
echo "Error: book id was not found.";
require_once ('includes/footer.php');
exit();
}
$book_id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$sql = "SELECT * FROM books WHERE book_id=" . $book_id;
$query = $conn->query($sql);
$row = $query->fetch_assoc();
add this
$query = $conn->query($sql) or trigger_error($mysqli->error."[$sql]");
if ($query->num_rows > 0) {
while($row = $query->fetch_assoc()) {
}
} else {
echo "0 results";
}
Always check for errors when running a query.
Related
I have a sql database that when a key is passed from the web page input box back to the sql database (server)
this generates the encoded key and passes it back to the web page.
If the wrong key is sent then it returns wrong key. If it is correct it will return the generated key.
This works perfectly.
However when i try to add an additional check in the name of payment 1 for true 0 for false i can not combine the two. Meaning i would like both conditions to be met to make this work. Here is the code that works.
$sql = "SELECT decrypt_key FROM keys WHERE software_key=" . "'" . $software_key_encoded . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["decrypt_key"];
}
} else {
echo "Failed";
}
$conn->close();
}
else {
echo "Failed";
}
followed by what i am trying to do, This throws the error of
Trying to get property of non-object in /---/ Failed
$sql = "SELECT decrypt_key FROM keys WHERE software_key=" . "'" . $software_key_encoded . "'";
$eql = "SELECT payment FROM new_keys WHERE payment =1";
$result = $conn->query($sql) && $result1 = $conn->query($eql);
if ($result->num_rows > 0 && $result1->num_rows > 1) {
while($row = $result->fetch_assoc()) {
echo $row["decrypt_key"];
#echo $row["payment"];
}
} else {
echo "Failed";
}
$conn->close();
}
else {
echo "Failed";
}
$result = $conn->query($sql) && $result1 = $conn->query($eql);
change this line to:
$result = $conn->query($sql);
$result1 = $conn->query($eql);
and in if statement change $result1->num_rows > 1 to $result1->num_rows > 0;
Split the result variables in 2 different statements like below
$result = $conn->query($sql);
$result1 = $conn->query($eql);
If you still face any issue please post the error message too.
I created the code using mysqli_fetch_assoc with 'while' as shown below.
But it does not work.
if ($ result = mysqli_query ($ dbconn, $ query)) {
It works by here.
while ($ row = mysqli_fetch_assoc ($ result)) {
It does not work from here.
I can not find the wrong part.
If I do not use 'while', it works as follows.
What is the problem?
// not works
$query = "select * from member where f_status='1'";
if ($result=mysqli_query($dbconn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
if ($row[f_status]==0) {
error("No data");
} else {
echo $row[f_user_id];
echo $row[f_user_name];
}
}
mysqli_free_result($result);
}
// works
$query = "select * from member where f_status='1'";
$result = mysqli_query($dbconn, $query);
$row = mysqli_fetch_assoc($result);
if ($row) {
echo $row[f_user_id];
echo $row[f_user_name];
} else {
error("No data");
}
I think the problem is the way you have iterated while loop and condition to show No data message,
You should try this way:
$query = "select * from member where f_status='1'";
if ($result = mysqli_query($dbconn, $query)) {
if ($result->num_rows) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row[f_user_id];
echo $row[f_user_name];
}
}
else {
error("No data");
}
mysqli_free_result($result);
}
Hope this should solve your issue.
if(!empty($username)&&!empty($password)) {
//following an old tutorial
$query = "SELECT `id` FROM `users` WHERE `Username`='$username' AND `Password`='$password_hash'";
if($query_run= mysqli_query($link, $query)) {
$query_num_rows = mysqli_num_rows($query_run);
//tried most of solutions on the site not working
if ($query_num_rows==0) {
echo 'Invalid username/password combination';
}
else if ($query_num_rows==1) {
//need sometihng to substitute the mysql_result
//its not working and iam not kinda ahnde with php
$user_id = mysql_result($query_run, 0, 'id');;
$_SESSTION['user_id'] = $user_id;
header ('Location: php.php');
}
}
}
mysql has been depricated, use mysqli instead
You can use this code to get data:
$query = "SELECT id FROM users WHERE Username='$username' AND Password='$password_hash'";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result))
{
echo "id:" . $row["id"];
}
} else {
echo "No results found";
}
I get warning mysqli_num_rows() expects parameter 1 to be mysqli_result, array and get 0 result.
I am sure i have the data, because when i run the same query in phpmyadmin, i get result 1 record.
here is my code
<?php
require_once('dbConn.php');
$sql = "SELECT * FROM tb_schedule WHERE status1=0 OR status2=0";
$resultq = mysqli_fetch_array(mysqli_query($con,$sql));
if (mysqli_num_rows($resultq) > 0) {
// output data of each row
$notif = 0;
while($row = mysqli_fetch_assoc($resultq)) {
$notif = 0;
$name = $row['name'];
$uid = $row['uid'];
$token = $row['token'];
$datetime = $row['datetime'];
$resvid = $row['resvid'];
//....
if ($notif == 1) {
if (mysqli_query($conn, $sql2)) {
echo "Record updated successfully = " . $name;
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
}
} else {
echo "0 results";
}
?>
just change this line
$resultq = mysqli_fetch_array(mysqli_query($con,$sql));
to
$resultq = mysqli_query($con,$sql);
you get this "mysqli_num_rows() expects parameter 1 to be mysqli_result, array" error because when you use "mysqli_num_rows($resultq)" you must supply "mysqli_result" which is from "mysql_query" see this, but in your case, you supply the arguments with "array" come from "mysqli_fetch_array(mysqli_query($con,$sql));" result see this
in your case, you use
$resultq = mysqli_fetch_array(mysqli_query($con,$sql));
this for get schedule
if (mysqli_num_rows($resultq) > 0) {
this for check if any schedule
while($row = mysqli_fetch_assoc($resultq)) {
this for fetch it with assoc type, will make waring again because you supply array for the arguments, see mysqli_fetch_assoc manual
try this,
require_once('dbConn.php');
$sql = "SELECT * FROM tb_schedule WHERE status1=0 OR status2=0";
/**
* get schedule data with fetch assoc and save it to $rows
*/
$rows = mysqli_fetch_assoc(mysqli_query($con,$sql));
// check if $rows is not null or empty
if (!empty($rows) > 0) {
// output data of each row
$notif = 0;
// loop rows data and call it with row
foreach($rows as $row) {
$notif = 0;
$name = $row['name'];
$uid = $row['uid'];
$token = $row['token'];
$datetime = $row['datetime'];
$resvid = $row['resvid'];
//....
if ($notif == 1) {
if (mysqli_query($conn, $sql2)) {
echo "Record updated successfully = " . $name;
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
}
} else {
echo "0 results";
}
I am currently trying to read data from multiple tables in a database and display the result in a tabular form.
But mysqli_query() returns true and mysqli_fetch_row() returns false.
$link is the connection object which has been initialized.
Code Snippet 1 :
$sql = "
SELECT *
FROM users_list, user_personal_details, user_bank_details
WHERE
users_list.email = user_personal_details.email AND
users.email=userbank_details.email
LIMIT $num_rec_per_page
OFFSET $start_from";
$rs_result = mysqli_query($link, $sql);
if ($rs_result == false) {
echo "Query failed";
exit();
}
else echo "Query successful";
Code Snippet 2 :
$row = mysqli_fetch_row($rs_result);
if ($row == false)
echo "Invalid";
while ($row = mysqli_fetch_row($rs_result)) {
echo "In loop";
$name = $row['name'];
$date_added = $row['date_added'];
echo $name;
echo $date_added;
}
A mysqli_fetch_row() has been used outside the loop for debugging purposes.
The message Query successful is printed.
The message Invalid is printed.
What could be the reason?
You call mysqli_fetch_row() two times, and probably you get false on the second one.
May be your query returns true but may be possible it does't return any result .Use mysqli_num_rows() to check number of rows return by your query.
And don't fetch rows two times
$sql = "SELECT * FROM users_list,user_personal_details,user_bank_details where users_list.email=user_personal_details.email and users.email=userbank_details.email LIMIT $num_rec_per_page OFFSET $start_from";
$rs_result = mysqli_query($link, $sql); //run the query
if ($rs_result == false) {
echo "Query failed";
exit();
} else {
$data = mysqli_num_rows($rs_result);
if ($data > 0) {
while ($row = mysqli_fetch_row($rs_result)) {
$name = $row['name'];
$date_added = $row['date_added'];
echo $name;
echo $date_added;
}
} else {
echo "No result found";
}
}