This is my code and I don't know why IF/ELSE statement is not working
$user_id = $_POST['user_id'];
$strSQL = "SELECT chat.user_id,max(date) max_date ,user.user_id,user.firstname,user.lastname,user.picture
FROM ( select from_user_id as user_id,date,isread from chat
WHERE to_user_id = '$user_id'
Union select to_user_id as user_id,date,isread from chat WHERE from_user_id = '$user_id' ) as chat join user on user.user_id = chat.user_id
where user.status != 'block' group by chat.user_id order by max_date DESC";
$chatdata = array();
$objQuery = mysql_query($strSQL);
while($row = mysql_fetch_assoc($objQuery)){
$frduser_id = $row['user_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$picture = $row['picture'];
$strSQL2 = "SELECT isread from chat WHERE to_user_id = '$user_id' and from_user_id = '$frduser_id'
ORDER BY date DESC LIMIT 1";
//array_push($chatdata, $row);
$objQuery2 = mysql_query($strSQL2);
if (empty($objQuery2)) {
$row2 = 1;
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}else{
$row2 = mysql_fetch_assoc($objQuery2);
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}
}
You can use mysql_num_rows to get count of rows inside results.
$objQuery2 = mysql_query($strSQL2);
$objQuery2Rows = mysql_num_rows($strSQL2);
if ( $objQuery2Rows < 1) {
$row2 = 1;
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}else{
$row2 = mysql_fetch_assoc($objQuery2);
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}
Related
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.
I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}
Hello here is my current code:
$sqlpack = "Select * from package_in_plan where plan_id='$package_id' order by plan_id";
$planres = mysqli_query($conn,$sqlpack);
$plan = array();
while ($row1 = mysqli_fetch_assoc($planres)){
$plan[] = $row1['package_id'];
}
$plan_1 = implode(',', $plan);
for ($x = 0; $x < count($plan); $x++) {
$sql_service = "Select * from service_in_package where package_id='".$plan[$x]."'";
$chid = mysqli_query($conn,$sql_service);
while ($row = mysqli_fetch_array($chid)){
$ch_id = $row['service_id'];
$sql = "Select * from itv where status='1' and id='$ch_id' order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
}
}
What i need is the output to be sorted by number($chnr), right now my code is not sorting because it's receiving specific id from previous select ($ch_id).
How can i let the output of $results to be sorted "order by number".
Number is INT in itv table.
You could make use of subqueries which results in a single query that can be sorted.
$sql = "Select * from itv where status='1' and id IN
(Select service_id from service_in_package where package_id IN
(Select package_id from package_in_plan where plan_id='$package_id' order by plan_id))
order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
I am querying a large db in php, and I am not doing it near well enough.
I have the code I need, I simple cannot find a good way to compact these statements.
$q = $_GET['q'];
(dbinit)
$query = "SELECT post_id FROM wp_postmeta WHERE meta_key = '_billing_email' AND meta_value = '$q'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
$thing = $row['post_id'];
$querys = "SELECT 'meta_value' FROM wp_postmeta WHERE post_id = '$thing' AND meta_key = '_order_number'";
$results = mysql_query($querys);
$rows = mysql_fetch_array($results);
$postid = $rows['meta_value'];
$queryss = "SELECT post_id FROM $table WHERE meta_value = $postid AND meta_key = '_order_number'";
$resultss = mysql_query($query);
$rowss = mysql_fetch_array($result);
$order_id = $rowss['post_id'];
(handling)
}
I am wondering if there is a more efficient way to do these queries, or perhaps have them in one query?
Not sure where $table comes from.
But this JOIN attempt should work:
$q = $_GET['q'];
// ... dbinit ...
$query = "SELECT wpp.post_id,
wpp1.meta_value as orderNumber,
wpp2.post_id as orderId
FROM wp_postmeta wpp
LEFT JOIN wp_postmeta wpp1
ON wpp1.post_id = wpp.post_id
AND wpp1.meta_key = '_order_number'
LEFT JOIN $table wpp2
ON wpp2.meta_value = wpp1.meta_value
AND wpp2.meta_key = '_order_number'
WHERE wpp.meta_key = '_billing_email'
AND meta_value = '$q'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
(handling)
}
As you can see you can get
$thing = $row['post_id'];
$order_id = $row['orderId'];
inside the loop if needed.
I am learning JSON.
getDatas.php contents:
$query = "SELECT domain FROM access WHERE userid = '".$userid."'";
$result = mysql_query($query) or die(mysql_error());
$res = array();
while($row = mysql_fetch_array($result)){
$query2 = "SELECT COUNT(*) as cnt from clients WHERE domain ='".$row['domain']."'";
$res2 = mysql_query($query2) or die(mysql_error());
$res2 = mysql_fetch_array($res2);
$res[] = array('domain' => $row['domain'], 'count' => $res2['cnt']);
};
echo json_encode($res);
The output is:
[{"domain":"www.domain1.com","count":"2"},{"domain":"www.domain2.com","count":"42"},{"domain":"www.domain3.com","count":"61"}]
How do I print like this?
How do I get out clean?
www.domain1.com - 2
www.domain2.com - 42
www.domain3.com - 61
Try this:
$query = "SELECT a.domain, COUNT(c.domain) as cnt
FROM `access` as a
LEFT JOIN `clients` as c on c.domain = a.domain
WHERE a.userid = '".$userid."'
GROUP BY c.domain";
$result = mysql_query($query) or die(mysql_error());
$res = array();
while($row = mysql_fetch_array($result)){
$res[] = array($row['domain'] => $row['cnt']);
};
echo json_encode($res);
you only need to use
echo json_encode($res);