Getting error of non-object property - php

Here, I am getting error like,
Notice: Trying to get property of non-object
in last two lines while fetching record.
what does it say?
My code:
$Id = $_REQUEST['id'];
$sql = "Select * From ".CHANNEL_MASTER."
Where sam_status = '".ACTIVE_STATUS."' And user_id = '".$_SESSION['user_id']."' And sam_id = '".$Id."'";
$db->query($sql);
$row = $db->fetch_object(MYSQL_FETCH_SINGLE);
$siteID = array_search($row->sam_site_id, $site_id_array);
$ebay_token = $row->sam_ebay_token;

You need to store query result into a variable then fetch data from it.
So instead of
$db->query($sql);
$row = $db->fetch_object(MYSQL_FETCH_SINGLE);
use
$result=$db->query($sql);// store query result into $result
$row = $result->fetch_object(MYSQL_FETCH_SINGLE);// fetch data from $result

$Id = $_REQUEST['id'];
$sql = "Select * From ".CHANNEL_MASTER."
Where sam_status = '".ACTIVE_STATUS."' And user_id = '".$_SESSION['user_id']."' And sam_id = '".$Id."'";
$result = $db->query($sql);
$row = $result->fetch_object(MYSQL_FETCH_SINGLE);
$siteID = array_search($row->sam_site_id, $site_id_array);
$ebay_token = $row->sam_ebay_token;

Related

How to sum column in database using php

i'm trying to sum a column name "total". and i want to display the total sorting by id. if user A login he can see total booking in his account.
I keep get the error:
"Notice: Array to string conversion in Array."
can someone help me? I want to echo the total in input form.
this is my php code:
<?php
include ('connect.php');
$sql = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
$id = $row['p_id'];
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$row2 = mysql_fetch_array($sql);
$sum = $row['total'];
?>
Try this,
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($result2) or die(mysql_error());
$sum = $row['total'];
i got it! thanks this is my code
this is the code:
<?php
include ('connect.php');
$sql8 = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result8 = mysqli_query($conn,$sql8);
$row8 = mysqli_fetch_assoc($result8);
$id = $row8['p_id'];
$sql9 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result9 = mysqli_query($conn,$sql9);
$row9 = mysqli_fetch_array($result9);
$sum = $row9['total'];
?>

How to fetch single row data from php?

for ($i=$start; $i<$start+$scale && $i < $total_record; $i++)
{
$sql = "select * from memo where num = ?";
$stmh = $pdo->prepare($sql);
//mysql_data_seek($result, $i);
$row = mysql_fetch_array($result);
$sql2 = "select * from phptest.memo order by num desc";
$stmh2 = $pdo->query($sql2);
$stmh2->execute();
//$row = $stmh2->fetch(PDO::FETCH_ASSOC);
$row = $stmh->fetchColumn($i-1);
$memo_id = $row['id'];
$memo_num = $row['num'];
$memo_date = $row['regist_day'];
$memo_nick = $row['nick'];
$memo_content = $row['content'];
Hi guys i want fetch single row data by using PDO method instead of $row = $stmh2->fetch(PDO::FETCH_ASSOC); like mysqli_data_seek($result,$i);. What should i do?
$sql2 = "select * from phptest.memo order by num desc";
$stmh2 = $pdo->query($sql2);
$stmh2->execute();
while($r = $stmh2->fetch()) {$row[] = $r;}
Now $row[$i] should be getting you the same results as mysqli_data_seek($result,$i) would have.
IE: $row[0] would return the first row.

Getting an array to string conversion error

Can someone help me what's the problem with this code? Im trying to store the fetched data to an array and i want to based on the values of that array. Im getting an error of Array to string conversion. The datatype value of an array is string
Here's the code.
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = array($row['subj_descr']);
}
$sql ="SELECT * FROM notification WHERE subj_descr IN ({implode(',', $data})";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
Remove array inside your while loop:
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = $row['subj_descr'];
}
$sql ="SELECT * FROM notification WHERE subj_descr IN ({implode(',', $data})";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
create a new variable and implode in it.
Try this
$implodeAray = implode(",", $data);
$sql ="SELECT * FROM notification WHERE subj_descr IN ($implodeAray)";
You are creating a multidimensional array and so change this statement
$data[] = array($row['subj_descr']);
to
$data[] = $row['subj_descr'];
As SQL IN statement always used a single dimensional array so also make change in query where clause.
I have changed all, please try below code:
<?php
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = $row['subj_descr'];
}
$dataStr = implode(',', $data);
$sql ="SELECT * FROM notification WHERE subj_descr IN (".$dataStr.")";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
?>
You stored the element of your array inside another array while looping.
Do this:
$sql3 ='SELECT DISTINCT subj_descr
FROM subj_enrolled
WHERE enroll_ref = "$ref"';
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
//Your error was here
//Each elements is escaped for security reasons
$data[] = mysqli_escape_string($con,$row['subj_descr']);
}
//This implodes and puts a single quote around each element
$dataIn= '\'' . implode( '\', \'', $data ) . '\'';
$sql ="SELECT * FROM notification
WHERE subj_descr IN ($dataIn)";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);

SELECT Count php/sql

I am trying to store a mysql value into a php variable. I have the following query which I know works. However, I the value for $count is always 0. Can someone explain what I need to do to get the count value? The count should be the count of x's w here name_x=.$id.
$query = "SELECT COUNT(name_x) FROM Status where name_x=.$id.";
$result = mysql_query($query);
$count = $result;
Is first letter in table name is really capital. Please check it first.
or Try :
$query = "SELECT COUNT(*) as totalno FROM Status where name_x=".$id;
$result = mysql_query($query);
while($data=mysql_fetch_array($result)){
$count = $data['totalno'];
}
echo $count;
$query = "SELECT COUNT(*) FROM `Status` where `name_x`= $id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$count = $row[0];
please try it
$query = "SELECT COUNT(*) FROM Status where name_x=$id";
$result = mysql_query($query);
$count = mysql_result($result, 0);
You are missing single quotes around $id. Should be
name_x = '" . $id . "'";

Writing and reading to a mysql table an array data structure with PHP

I am build a data structure using multidimensional associative arrays. Can I update that data structure into a mysql table field and then read it back again?
Here is an example of what I am trying to do:
$result = mysql_query("select * FROM color") or die(mysql_error());
$colors = "";
while($colorrec = mysql_fetch_array($result)){
$colors[$colorrec['ID']][0] = $colorrec['Description'];
$colors[$colorrec['ID']][1] = $colorrec['HexCode'];
}
If I now do:
mysql_query("UPDATE tempfile SET ColorInfo = '".$colors."' WHERE ID = '".tempID."'");
Can I then do:
$result = mysql_query("select * from tempfile WHERE ID = '".tempID."'");
$temprec = mysql_fetch_array($result);
$colors = $temprec['ColorInfo'];
You could serialize it to hold it's data type, then unserialize it when you fetch it back.
$result = mysql_query("select * FROM color") or die(mysql_error());
$colors = array();
while($colorrec = mysql_fetch_array($result)){
$colors[$colorrec['ID']] = array($colorrec['Description'], $colorrec['HexCode']);
}
mysql_query("UPDATE tempfile SET ColorInfo = '".serialize($colors)."' WHERE ID = '".$tempID."'");
$result = mysql_query("select * from tempfile WHERE ID = '".$tempID."'");
$temprec = mysql_fetch_array($result);
$colors = unserialize($temprec['ColorInfo']);

Categories