trying to get property of non-objec error [duplicate] - php

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
lets see code
public function chekfactor( $factor,$user) {
$arrfactor=preg_split('/<td>/',$factor);
$arrusers=preg_split('/<td>/',$user);
$tedstore=count($arrusers) ;
$tedkala=count($arrfactor) ;
$inttedstore=(int) $tedstore;
$inttedkala=(int)$tedkala;
$afa="0";
$a=(int)$afa;
for (;$a<$inttedstore;){
$storeusername=$arrusers[$a];
$faktorss=$arrfactor[$a];
$tablename='devlist'.$storeusername;
echo $tablename;
echo $faktorss;
$result = $this->conn->query("SELECT id FROM'".$tablename."' WHERE prcode='".$faktorss."' ");
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$user = array();
$user[$faktorss] = $row["id"];
return $user;
}
}else {
return "no";
}
$a++;
}
and give to me the errors
trying to get property of non-object on line 543 means " if
($result->num_rows > 0) { "
why happen and how fix it ?

change the line
if ($result->num_rows > 0)
for
if (!empty($result)) // make sure it is having value then use `num_rows` on it
The issue is that you are trying to access to num_rows property when $result is empty.

i guess your query is not working because of your quotes and space
"SELECT id FROM'".$tablename."' WHERE prcode='".$faktorss."'";
query will print like
SELECT id FROM'table_1' WHERE..
you can change like
"SELECT id FROM ".$tablename." WHERE prcode=".$faktorss."";

You can do
if($result && !empty($result)){
$num_rows = $result->num_rows;
if($num_rows > 0){
//the logic here
}
}

Related

How do I get a value outside the loop using PHP [duplicate]

This question already has answers here:
How to get comma separated values from database [duplicate]
(3 answers)
Converting MySQL results into comma separated values
(4 answers)
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 3 years ago.
I'm creating a small website using PHP, which is generally a site for showcasing the hospital, and I modified the code given in this example:
https://www.w3schools.com/php/php_mysql_select.asp
<?php
$query = "SELECT * FROM emp WHERE type = 'woman' ";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$cat = $row["cat"] . ',';
echo $cat;
////<---- echo on while (Loop)
}
}
The expected output would be as follows:
Output: 35,36
But I changed the code with the link above and it is as follows:
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$cat = $row["cat"] . ',';
}
echo $cat;
///// <---- echo Out of While (Loop)
}
Output: 35
My expecting output would be 35, 36 outside of "while" using "echo".
What code do you recommend to output "35,36" the same code above?
You can try the below code to achive your requirement
$data = array();
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result)) {
$data[] = $row["cat"];
}
}
echo implode(",",$data);

Loop Through Array using PHP and MySQLi [duplicate]

This question already has answers here:
How to loop through a mysql result set
(6 answers)
Closed 3 years ago.
I'm trying to loop through a MySQL table using PHP, but it is only showing one line.
//Retrieve a list of outstanding developments
$sql = "SELECT * FROM tblDevelopment WHERE strStatus=?";
$statement = mysqli_stmt_init($conn);
//Check for any errors in the SQL statement
if (!mysqli_stmt_prepare($statement,$sql)){
//Report any errors with the prepared $statement
header("Location: ../sqlerror.php");
exit();
} else {
//If there are no errors, query the database for the username
mysqli_stmt_bind_param($statement,'s', $status);
mysqli_stmt_execute($statement);
$results = mysqli_stmt_get_result($statement);
if ($row = mysqli_fetch_assoc($results)) {
echo 'header';
while ($row = mysqli_fetch_assoc($results))
{
echo $row['strDetail'] . "</";
}
echo 'footer';
} else {
echo 'No results to display';
}
}
The code works when there are no results, but it only shows one result when there are more than one - any ideas what I'm doing wrong?
You are almost there... you need to keep calling mysqli_fetch_assoc until you reach the end of the result set. Changing your if to a while should be enough to get you rolling.
while (($row = mysqli_fetch_assoc($results)) !== null) {

IF-ELSE from MySQL based on single row

I try to get the value 'entryScans' from my SQL-table and send it to $output based on the IF-ELSE case. What am I doing wrong?
<?php
include ('config.php');
$TicketNo=$_POST["TicketNo"];
$hash=$_POST["hash"];
$sql = "SELECT TicketNo,hash,entryScans FROM `Tickets` WHERE `TicketNo` = '$TicketNo' AND `hash` = '$hash'" or die(mysql_error());
$result = mysql_query($sql);
$row=mysql_num_rows($result);
if($row['entryScans'] = 0){
$output="ok";
}
else if ($row['entryScans'] > 0) {
$output="maybe";
else{
$output="error";
}
print(json_encode($output));
mysql_close();
?>
You are doing ...
if($row['entryScans'] = 0){
Which has 2 problems, = is assignement, == is testing equal to. The second part is that your fetching the results in...
$result = mysql_query($sql);
$row=mysql_num_rows($result);
So $row is the number of rows, $result is the query results...
So to check the number of rows in the result set
if($row == 0){
etc.
Update:
If you want the value of the column entryScans to be used, then you need to change the call of mysql_num_rows() to mysql_fetch_assoc(), so
$row=mysql_fetch_assoc($result);
Then you can leave the rest of the code to use $row['entryScans']
You are not processing your results. You just get the total number of rows in $row variable.
You need to process your query result by looping method.
Here is the example
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row['entryScans'] = 0)
{
$output="ok";
}
else if ($row['entryScans'] > 0)
{
$output="maybe";
}
else
{
$output="error";
}
}
Hope this works for you.

SQL select as a PHP function [duplicate]

This question already has answers here:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
(3 answers)
Closed 7 years ago.
I'm selecting data from MySQL. My code looks like this:
$sql = "SELECT oznacenie_odpadu FROM zdroj_dat ORDER by ID ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['oznacenie_odpadu'].'" >'.$row['oznacenie_odpadu'].'</option>';
}
}
This part of code is in my code multiple times. I'm calling it 5times only string "oznacenie_odpadu" is changing. Therefore I made function:
function select($data) {
$sql = "SELECT rozmer FROM zdroj_dat ORDER by id ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row[$data].'" >'.$row[$data].'</option>';
}
}
}
Calling it with select("somevalue");
Syntax is ok because I didn't change anything but when I load the page the data from database are not retrieved.
You have a variable scope issue. $conn is not available inside of your function. You need to pass it as a parameter so ic an be used inside of your function.
function select($conn, $data) {
$sql = "SELECT rozmer FROM zdroj_dat ORDER by id ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row[$data].'" >'.$row[$data].'</option>';
}
}
}
Call it:
select($conn, "somevalue");

Recursive PHP function is not returning a result [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Here is my function:
function loop($id) {
unset($result, $sql, $query);
$sql = " SELECT parent_id FROM page_entries WHERE id = '$id' ";
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_fetch_assoc($query) or die(mysql_error());
if ($result['parent_id'] != 0) {
echo $result['parent_id'] . "... looping<br>";
loop($result['parent_id']);
} else {
echo $result['parent_id'] . "... done loop";
return $result['parent_id'];
}
}
echo loop('2');
I'm echoing the parent_id for testing. This is what is output to the browser:
1... looping
0... done loop
Where I'm not sure: the echo loop('2') doesn't echo anything from return $result['id'] if I comment out the echo lines in the function. I've tried testing by changing the return to return 'foo'; and still nothing.
How can I fix it?
At a glance, I think
loop($result['parent_id']);
should be
return loop($result['parent_id']);
otherwise your if branch is returning nothing.

Categories