Trying to implement telegram api into this script New messages - php

how to implement this api telegram line into New messages? so it check and see if new messages, and then make the api call so i get a new message notification on my telegram bot
hope there is someone that know how to put this in : would be very usefull. i have tried but failed many attemps
this line:
$url = "https://api.telegram.org/bot6547564:AAGP6DH754460526544HCsvwTlfoGNJPaQ/sendMessage?chat_id=#841rhe67&text=Ny Besked på - Svar: https://fake.com/index.php?page=admin&p=sdeg";
file_get_contents($url);
into this
<?php
header('Content-Type: application/json');
require_once('../assets/includes/core.php');
function getFakeUsersChat($tab) {
global $mysqli,$sm;
$return = array();
$time_now = time()-300;
$timestamp = time();
$dw = date( "w", $timestamp);
if($tab == 'New'){
$query = $mysqli->query("SELECT DISTINCT s_id,r_id FROM chat where fake = 1 and online_day = '".$dw."' and seen = 0 order by id DESC");
} else {
$query = $mysqli->query("SELECT DISTINCT s_id,r_id FROM chat where fake = 1 and online_day = '".$dw."' order by id DESC");
}
$return['empty'] = false;
if ($query->num_rows > 0) {
while($value = $query->fetch_object()){
$senderName = getData('users','name','WHERE id ='.$value->s_id);
$fakeName = getData('users','name','WHERE id ='.$value->r_id);
$fakeId = $value->r_id;
$senderId = $value->s_id;
$fakePhoto = profilePhoto($value->r_id);
$senderStatus = userStatus($senderId);
$senderStatusIcon = 'avatar-online';
if($senderStatus == 'n'){
$senderStatusIcon = 'avatar-offline';
}
$chatId = getData('chat','id','WHERE s_id = '.$value->s_id.' AND r_id = '.$value->r_id.' ORDER BY id DESC LIMIT 1');
$searchName = strtolower($senderName);
$conv = $senderId.$fakeId;
$newMessagesFilter = 'WHERE r_id = '.$fakeId.' AND s_id = '.$senderId.' AND seen = 0';
$newMessagesCount = selectC('chat',$newMessagesFilter);
$return['conv'][] = $conv;
$return[$conv]['chatId'] = $chatId;
$newMessageLabel = '<span class="badge badge-pill badge-success" data-new-message="'.$chatId.'" style="margin-right:10px;font-size: 10px">NEW</span>';
if($tab == 'All'){
if($newMessagesCount == 0){
$newMessageLabel = '';
}
}
$return[$conv]['data'] = '<div class="list-group-item d-flex align-items-start border-right-3 border-right-primary bg-light rightSectionChatUser" data-conv="'.$conv.'" data-chatid="'.$chatId.'"
data-uid="'.$fakeId.'" data-search-bind="'.$searchName.'"
data-cid="'.$senderId.'" data-name="'.$fakeName.'" data-id="'.$fakeId.'" onclick="viewChat(this)" data-photo="'.profilePhoto($fakeId).'"
onmouseover="hoverFriendList(this,1)" onmouseout="hoverFriendList(this,2)" style="cursor: pointer;">
<div class="mr-3 d-flex flex-column align-items-center">
<div class="avatar avatar-md '.$senderStatusIcon.'" style="margin-bottom:5px" data-toggle="tooltip" data-placement="top" title="" data-original-title="'.$fakeName.'">
<img src="'.profilePhoto($senderId).'" alt="Avatar" class="avatar-img rounded-circle">
</div>
<div class="avatar avatar-md avatar-online" data-toggle="tooltip" data-placement="top" title="" data-original-title="'.$senderName.'">
<img src="'.profilePhoto($fakeId).'" alt="Avatar" class="avatar-img rounded-circle">
</div>
</div>
<div class="flex">
<p class="m-0">
<span class="d-flex align-items-center mb-1">
From: '.$senderName.'
<small class="ml-auto text-muted" style="font-size: 10px">
'.$newMessageLabel.'
'.getLastMessageMobileTime($fakeId,$senderId).'</small>
</span>
<span class="d-flex align-items-end">
<span class="flex mr-3">
<strong class="text-body mb-1">To: '.$fakeName.'</strong><br>
<small class="text-muted" style="margin-top:15px;max-height: 3rem; overflow: hidden; position: relative; display: inline-block;background: #fff;border-radius: 5px;padding: 5px 10px 5px 10px">
'.getLastMessageMobile($fakeId,$senderId).'
</small>
</span>
<a href="javascript:;" class="d-flex align-items-center mb-1">
<small class="text-muted mr-1">'.$newMessagesCount.'</small>
<i class="material-icons icon-16pt text-muted">chat</i>
</a>
</span>
</p>
</div>
</div>';
}
} else {
$return['empty'] = true;
}
return json_encode($return);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
switch ($_POST['action']) {
case 'load':
$uid = secureEncode($_POST['user']);
getUserInfo($uid,3);
$mysqli->query("UPDATE chat set seen = 1 where s_id = '".$sm['chat']['id']."' and r_id = '".$sm['user']['id']."'");
$arr =array();
$arr['id']=$sm['chat']['id'];
$arr['name']=$sm['chat']['name'];
$arr['chat'] = getChat($sm['user']['id'],$sm['chat']['id']);
echo json_encode($arr);
break;
case 'send':
global $mysqli;
if (strpos($_POST['message'], '<img') !== false) {
$message = mysqli_real_escape_string($mysqli, $_POST['message']);
} else {
$message = secureEncode($_POST['message']);
}
$r_id = secureEncode($_POST['r_id']);
$mobile = secureEncode($_POST['mobile']);
$time = time();
if(isset($_POST['fast'])){
$fast = secureEncode($_POST['fast']);
if($fast){
$date = date('m/d/Y', time());
$mysqli->query("INSERT INTO users_chat (uid,date,count,last_chat) VALUES ('".$sm['user']['id']."','".$date."',1,'".$time."') ON DUPLICATE KEY UPDATE count=count+1");
}
}
getUserInfo($r_id,1);
$fake = $sm['profile']['fake'];
$online_day = $sm['profile']['online_day'];
$data['message'] = $sm['lang'][658]['text'];
$data['chatHeaderRight']='<div class="js-message-block" id="you">
<div class="message">
<div class="brick brick--xsm brick--hover">
<div class="brick-img profile-photo" data-src="'.$sm['user']['profile_photo'].'"></div>
</div>
<div class="message__txt">
<span class="lgrey message__time" style="margin-right: 15px;">'.date("H:i", time()).'</span>
<div class="message__name lgrey">'.$sm['user']['first_name'].'</div>
<p class="montserrat chat-text">'.$message.'</p>
</div>
</div>
</div>
';
$data['time'] = date("H:i", time());
$event = 'chat'.$r_id.$sm['user']['id'];
$noti= 'notification'.$r_id;
$data['id'] = $sm['user']['id'];
$data['type'] = 1;
$data['icon'] = $sm['user']['profile_photo'];
$data['name'] = $sm['user']['first_name'];
$data['photo'] = 0;
$data['action'] = 'message';
$sm['push']->trigger($sm['plugins']['pusher']['key'], $event, $data);
$mysqli->query("INSERT INTO chat (s_id,r_id,time,message,fake,online_day) VALUES ('".$sm['user']['id']."','".$r_id."','".$time."','".$message."','".$fake."','".$online_day."')");
$arr = array();
$data['notification_chat'] = false;
$results = $mysqli->query("SELECT DISTINCT s_id FROM chat WHERE r_id = '".$r_id."' AND seen = 0 AND notification = 0 order by id desc");
if($results->num_rows > 0){
$data['notification_chat'] = getUserFriends($r_id);
$data['unread'] = checkUnreadMessages($r_id);
}
$sm['push']->trigger($sm['plugins']['pusher']['key'], $noti, $data);
if($fake == 0){
$sm['profile_notifications'] = userNotifications($sm['profile']['id']);
if($sm['profile']['last_access']+300 <= time() && $sm['profile_notifications']['message'] == 1){
chatMailNotification($r_id,$message);
}
}
$arr['message'] = cleanMessage($message);
if($mobile == 1){
$arr['chat'] = getLastChatMobile($sm['user']['id'],$r_id);
} else {
$arr['chat'] = getLastChat($sm['user']['id'],$r_id);
}
//activity content
if($sm['plugins']['logActivity']['enabled'] == 'Yes'){
$ac = array();
$ac['u1']['id'] = $sm['user']['id'];
$ac['u2']['id'] = $sm['profile']['id'];
$ac['u1']['name'] = $sm['user']['first_name'];
$ac['u2']['name'] = $sm['profile']['first_name'];
$ac['u1']['photo'] = $sm['user']['profile_photo'];
$ac['u2']['photo'] = $sm['profile']['profile_photo'];
$ac['message'] = $arr['message'];
$adminPush= 'adminActivity';
$pushData['message'] = $ac;
$ac = json_encode($ac);
$title = $sm['user']['first_name'].' sent a message to '.$sm['profile']['first_name'];
activity('message',$ac,$title);
$sm['push']->trigger($sm['plugins']['pusher']['key'], $adminPush, $pushData);
}
break;
case 'send_fake':
$message = secureEncode($_POST['mensaje']);
$r_id = secureEncode($_POST['uid2']);
$s_id = secureEncode($_POST['uid1']);
$mobile = secureEncode($_POST['mobile']);
$time = time();
getUserInfo($r_id,1);
$fake = $sm['profile']['fake'];
getUserInfo($s_id,6);
$data['message'] = cleanMessage($message);
if($mobile == 1){
$data['chat'] = getLastChatMobile($sm['search']['id'],$r_id);
} else {
$data['chatHeaderRight']='<div class="js-message-block" id="you">
<div class="message">
<div class="brick brick--xsm brick--hover">
<div class="brick-img profile-photo" data-src="'.$sm['search']['profile_photo'].'"></div>
</div>
<div class="message__txt">
<span class="lgrey message__time" style="margin-right: 15px;">'.date("H:i", $spotl->time).'</span>
<div class="message__name lgrey">'.$sm['search']['first_name'].'</div>
<p class="montserrat chat-text">'.cleanMessage($message).'</p>
</div>
</div>
</div>
';
}
$data['time'] = date("H:i", time());
$event = 'chat'.$r_id.$sm['search']['id'];
$noti= 'notification'.$r_id;
$data['id'] = $sm['search']['id'];
$data['type'] = 1;
$data['icon'] = $sm['search']['profile_photo'];
$data['name'] = $sm['search']['first_name'];
$data['photo'] = 0;
$mysqli->query("INSERT INTO chat (s_id,r_id,time,message,fake) VALUES ('".$s_id."','".$r_id."','".$time."','".$message."','".$fake."')");
$arr = array();
$data['notification_chat'] = false;
$results = $mysqli->query("SELECT DISTINCT s_id FROM chat WHERE r_id = '".$r_id."' AND seen = 0 AND notification = 0 order by id desc");
if($results->num_rows > 0){
$data['notification_chat'] = getUserFriends($r_id);
$data['unread'] = checkUnreadMessages($r_id);
}
$sm['push']->trigger(siteConfig('pusher_key'), $event, $data);
$sm['push']->trigger(siteConfig('pusher_key'), $noti, $data);
if($fake == 0){
$sm['profile_notifications'] = userNotifications($sm['profile']['id']);
if($sm['profile']['last_access']+300 >= time() && $sm['profile_notifications']['message'] == 1){
chatMailNotification($r_id,$message);
}
}
$arr['message'] = cleanMessage($message);
if($mobile == 1){
$arr['chat'] = getLastChatMobile($s_id,$r_id);
} else {
$arr['chat'] = getLastChat($s_id,$r_id);
}
echo json_encode($arr);
break;
case 'fake_list':
$tab = secureEncode($_POST['tab']);
echo getFakeUsersChat($tab);
break;
case 'load_fake':
$u1 = secureEncode($_POST['u1']);
$u2 = secureEncode($_POST['u2']);
//Secure info
$u1 = secureEncode($u1);
$u2 = secureEncode($u2);
//Get all chats
$chatt = $mysqli->query("SELECT * FROM chat where s_id = '".$u1."' and r_id = '".$u2."' OR r_id = '".$u1."' and s_id = '".$u2."' order by id asc");
//Update chat to seen
$mysqli->query("UPDATE chat set seen = 1 where s_id = '".$u2."' and r_id = '".$u1."'");
if ($chatt->num_rows > 0) {
while($chat= $chatt->fetch_object()){
//Get profile photo
$mensaje = $chat->message;
//Check if current user is the sender
if($chat->s_id == $u1){
if($chat->seen == 1){
echo'
<li class="me">
<div class="image">
<img src="'.profilePhoto($chat->s_id).'"/>
</div>
<p> '.$mensaje.' </p>
</li>
';
} else {
echo'
<li class="me">
<div class="image">
<img src="'.profilePhoto($chat->s_id).'"/>
</div>
<p> '.$mensaje.' </p>
</li>
';
}
} else {
echo'
<li class="you">
<div class="image">
<img src="'.profilePhoto($chat->s_id).'"/>
</div>
<p> '.$mensaje.' </p>
</li>
';
}
}
}
break;
case 'current_fake':
//GET POST INFO
$u1 = secureEncode($_POST['u1']);
$u2 = secureEncode($_POST['u2']);
//GET NEW MESSAGES
$results = $mysqli->query("SELECT * FROM chat WHERE r_id = '".$u1."' AND s_id = '".$u2."' AND seen = 0 order by id asc");
//CHECK IF CURRENT USER HAS NEW MESSAGE
if($results->num_rows > 0){
while ($obj = $results->fetch_object()){
//UPDATE MESSAGE AS SEEN
$mysqli->query("UPDATE chat SET seen = 1 where r_id = '".$u1."' and s_id = '".$u2."'");
//SHOW NEW MESSAGES
echo'
<li class="you">
<div class="image">
<img src="'.profilePhoto($obj->s_id).'"/>
</div>
<p> '.$obj->message.' </p>
</li>
';
}
}
break;
case 'access':
$time = time();
$access = secureEncode($_POST['access']);
$r_id = secureEncode($_POST['r_id']);
if($access == 1) {
$mysqli->query("INSERT INTO chat (s_id,r_id,time,message) VALUES
('".$sm['user']['id']."','".$r_id."','".$time."','".$sm['lang'][189]['text']."')");
$mysqli->query("UPDATE chat SET access = 2 WHERE s_id = '".$r_id."' AND r_id = '".$sm['user']['id']."' AND access = 1");
$mysqli->query("INSERT INTO blocked_photos (u1,u2) VALUES ('".$r_id."','".$sm['user']['id']."')");
} else {
$mysqli->query("INSERT INTO chat (s_id,r_id,time,message) VALUES
('".$sm['user']['id']."','".$r_id."','".$time."','".$sm['lang'][190]['text']."')");
$mysqli->query("UPDATE chat SET access = 2 WHERE s_id = '".$r_id."' AND r_id = '".$sm['user']['id']."' AND access = 1");
}
break;
case 'current':
$uid = secureEncode($_POST['uid']);
$mob = secureEncode($_POST['mobile']);
getUserInfo($uid,3);
$arr = array();
$arr['result'] = 0;
$results = $mysqli->query("SELECT s_id,id,photo FROM chat WHERE r_id = '".$sm['user']['id']."' AND s_id = '".$uid."' AND seen = 0 order by id asc");
if($results->num_rows > 0){
$re = $results->fetch_object();
$arr['result'] = 1;
$arr['photo'] = $re->photo;
if($mob == 1){
$arr['chat'] = getLastChatMobile2($sm['user']['id'],$uid);
} else {
$arr['chat'] = getLastMessage($sm['user']['id'],$uid);
}
$arr['message'] = cleanMessageById($re->id);
$mysqli->query("UPDATE chat set seen = 1 where r_id = '".$sm['user']['id']."' and s_id = '".$uid."'");
}
echo json_encode($arr);
break;
case 'read':
$id = secureEncode($_POST['id']);
$mysqli->query("UPDATE chat set seen = 1, notification = 1 where r_id = '".$sm['user']['id']."' and s_id = '".$id."'");
break;
case 'notification':
$user = secureEncode($_POST['user']);
$time = time();
$arr = array();
if($user == 0){
$results = $mysqli->query("SELECT DISTINCT s_id FROM chat WHERE r_id = '".$sm['user']['id']."' AND seen = 0 AND notification = 0 order by id desc");
} else {
$results = $mysqli->query("SELECT DISTINCT s_id FROM chat WHERE r_id = '".$sm['user']['id']."' AND s_id <> '".$user."' AND seen = 0 AND notification = 0 order by id desc");
}
if($results->num_rows > 0){
while($use = $results->fetch_object()){
$arr[] = $use->s_id;
}
$mysqli->query("UPDATE chat set notification = 1 where r_id = '".$sm['user']['id']."'");
}
echo json_encode($arr);
break;
case 'new':
echo getUserFriends($sm['user']['id']);
break;
case 'today':
$uid = secureEncode($sm['user']['id']);
$time = time();
$date = date('m/d/Y', time());
$mysqli->query("INSERT INTO users_chat (uid,date,count,last_chat) VALUES ('".$uid."','".$date."',1,'".$time."') ON DUPLICATE KEY UPDATE count=count+1");
break;
case 'chat_limit':
$uid = secureEncode($sm['user']['id']);
$date = date('m/d/Y', time());
$mysqli->query("DELETE FROM users_chat WHERE uid = '".$uid."' AND date = '".$date."'");
break;
}
}
//CLOSE DB CONNECTION
$mysqli->close();

Related

delete not working properly in while loop

here i am trying to delete from detail table here is one problem with my code if detail table have three records than only delete one record from detail table.and also it does not effect other two records in stock table..
Only one first record this code work properly after that it does not work in for delete query from detail table and not effected in stock table...
<?php
include("include/config.inc.php");
$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;
if($purchaseMasterId > 0) {
$k = 0;
$selectMaster = "SELECT purchasedetail.purchaseMasterId, colorId,
totalkg, purchaseDetailId, partyId
FROM purchasedetail
JOIN purchasemaster ON
purchaseMaster.purchaseMasterId = purchasedetail.purchaseMasterId
WHERE purchasedetail.purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$selectMasterRes = mysql_query($selectMaster);
while($purRow = mysql_fetch_array($selectMasterRes)) {
$purchaseDetailId = $purRow['purchaseDetailId'];
$purchaseMasterId = $purRow['purchaseMasterId'];
$colorId = $purRow['colorId'];
$totalkg = $purRow['totalkg'];
$partyId = $purRow['partyId'];
$select = "SELECT qty
FROM stock
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$selectRes = mysql_query($select);
if($stockRow = mysql_fetch_array($selectRes)) {
$current = $stockRow['qty'];
}
$updateStock = "UPDATE stock
SET qty = ".$current." - ".$totalkg."
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$updateStockRes = mysql_query($updateStock) or die(mysql_error());
if($updateStockRes) {
$deleteDt = "DELETE FROM purchasedetail
WHERE purchaseDetailId = ".$purchaseDetailId;
$deleteRes = mysql_query($deleteDt);
if($deleteRes){
$deleteMst = "DELETE FROM purchasemaster
WHERE purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$deleteMstRes = mysql_query($deleteMst) or die(mysql_error());
if(!$deleteMstRes) {
echo "Purchase Master Delete Fail";
} else {
header("Location:purdetail.php");
exit();
}
}
}
}
}
?>
Here is my updated code with perfect code working
<?php
include("include/config.inc.php");
$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;
if($purchaseMasterId > 0)
{
$k = 0;
$selectMaster = "SELECT purchasedetail.purchaseMasterId,colorId,totalkg,purchaseDetailId,partyId
FROM purchasedetail
JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
WHERE purchasedetail.purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$selectMasterRes = mysql_query($selectMaster) or die(mysql_error());
while($purRow = mysql_fetch_array($selectMasterRes))
{
$purchaseDetailId = $purRow['purchaseDetailId'];
$purchaseMasterId = $purRow['purchaseMasterId'];
$colorId = $purRow['colorId'];
$totalkg = $purRow['totalkg'];
$partyId = $purRow['partyId'];
$select = "SELECT qty
FROM stock
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$selectRes = mysql_query($select);
if($stockRow = mysql_fetch_array($selectRes))
{
$current = $stockRow['qty'];
}
$updateStock = "UPDATE stock
SET qty = ".$current." - ".$totalkg."
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$updateStockRes = mysql_query($updateStock) or die(mysql_error());
if($updateStockRes)
{
$deleteDt = "DELETE FROM purchasedetail
WHERE purchaseDetailId = ".$purchaseDetailId;
$deleteRes = mysql_query($deleteDt);
if($deleteRes)
{
$selectid = "SELECT purchaseDetailId
FROM purchasedetail
WHERE purchaseMasterId = ".$purchaseMasterId;
$selectidRes = mysql_query($selectid);
if(mysql_affected_rows() == 0)
{
$mst = "DELETE FROM purchasemaster
WHERE purchaseMasterId = ".$purchaseMasterId;
$mstRes = mysql_query($mst) or die(mysql_error());;
if($mstRes)
{
header("Location:purdetail.php");
exit();
}
else
{
}
}
}
}
}
}
?>

show more option is not working with php and mysql

i am trying to add limit each time when button is clicked but is not working
my html code is
<div class="media stream load-more">
<a href="<?php echo $_SERVER['PHP_SELF']; ?>" method ='post' name = 'showmore'>
<i class="icon-refresh shaded"></i>
show more...
</a>
</div>
php code is
$showvalue = 2;
if(isset($_POST['showmore'])) {
$showvalue += 1;
}
$feeds = $db->getfeeds($showvalue);
mysql function
public function getfeeds($showvalue){
$stmt = $this->con->prepare("SELECT id,image,title,status,profilepic,created_at,url FROM news ORDER BY id DESC LIMIT $showvalue");
$stmt->bind_param("s",$showvalue);
$stmt->execute();
$result = $stmt->get_result();
$nrow = array();
while ($r = $result->fetch_assoc()) {
$nrow[] = $r;
}
$frow['news'] = $nrow;
$json = str_replace("\\/", "/",json_encode($frow));
return $json;
}

Check if MySQL result is empty

I want to check if a MySQL result is empty and echo an error message.
This is my my current SQL-Script:
<?php
$sql = new rex_sql;
$sql->debugsql = 0; //Ausgabe Query
$sql->setQuery("SELECT * FROM $db_downloads WHERE gruppe = $gruppe_teilnehmer ORDER BY id DESC limit 4");
for($i=0;$i<$sql->getRows();$i++)
{
$id = $sql->getValue("id");
$dateiname = $sql->getValue("dateiname");
$dateigroesse = $sql->getValue("dateigroesse");
$dateipfad = $sql->getValue("dateipfad");
$dateidatum = date("d.m.Y",strtotime($sql->getValue("dateidatum")));
$dateizeit = date("H.i",strtotime($sql->getValue("dateidatum")));
$download_teilnehmer = $sql->getValue("teilnehmer");
$datei_projektnummer = $sql->getValue("projektnummer");
$projektseite = $sql->getValue("projektseite");
$ausgabe .= '<li><i class="fa fa-file-o"></i>'.$dateiname.'<ul><li><i class="fa fa-calendar"></i>'.$dateidatum.' | '.$dateizeit.'</li><li><i class="fa fa-circle"></i>'.$datei_projektnummer.'</li></ul></li>';
if(empty($ausgabe)) {
echo ("Error! No result!");
}
$sql->next();
}
?>
But there is no error message. I think the if-argument is wrong positioned.
Put it outside the loop:
<?php
$ausgabe = "";
$sql = new rex_sql;
$sql->debugsql = 0; //Ausgabe Query
$sql->setQuery("SELECT * FROM $db_downloads WHERE gruppe = $gruppe_teilnehmer ORDER BY id DESC limit 4");
for($i=0;$i<$sql->getRows();$i++)
{
$id = $sql->getValue("id");
$dateiname = $sql->getValue("dateiname");
$dateigroesse = $sql->getValue("dateigroesse");
$dateipfad = $sql->getValue("dateipfad");
$dateidatum = date("d.m.Y",strtotime($sql->getValue("dateidatum")));
$dateizeit = date("H.i",strtotime($sql->getValue("dateidatum")));
$download_teilnehmer = $sql->getValue("teilnehmer");
$datei_projektnummer = $sql->getValue("projektnummer");
$projektseite = $sql->getValue("projektseite");
$ausgabe .= '<li><i class="fa fa-file-o"></i>'.$dateiname.'<ul><li><i class="fa fa-calendar"></i>'.$dateidatum.' | '.$dateizeit.'</li><li><i class="fa fa-circle"></i>'.$datei_projektnummer.'</li></ul></li>';
$sql->next();
}
if(empty($ausgabe)) {
echo ("Error! No result!");
}
?>
Use php count function
if(!count($sql->getRows())) {
echo 'No data found';
}

How to check for 2 variables in a single mysql_query

Need some help in php mysql
suppose i am logged in as $active_username and I want to see all the messages between me($active_username) and my friend($username) from a table pvt_message, where i have columns(pid,user_from,user_to,body).
I tried using below code:
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE user_from='$active_username' && user_to='$username' ORDER BY id ASC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
rest are the code to show data fetched on table, there is no error in that,so i didnt post.
this above code fetched me only those message i had sent to my friend.
again i used below code instead of above one:
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE user_from='$active_username' && user_to='$username' ORDER BY id ASC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
n now it fetched me all those message he/she sent me.
I want to view message we both sent each other, in asending order of the primary key of the table.
so i tried using:
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE (user_from='$active_username' || user_from='$username') && (user_to='$username' || user_to='$active_username') ORDER BY id ASC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username' || username ='$username'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
how to get those data in table??? where user_from can be both me($active_username) or he($username) and user_to can be both me($active_username) or he ($username).
Thankyou in advance :)
Try it
SELECT * FROM pvt_messages WHERE user_from IN ('$active_username', '$username') AND user_to IN ('$active_username', '$username')
It will work if not have messages in DB from active_username to active_username
just change your query like below
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE ((user_from='$active_username' && user_to='$username') || (user_from='$username' && user_to='$active_username')) ORDER BY id ASC ") or die(mysql_error());
Put the below code before fetching from the user table and pass the $qry variable in the mysql_query function.
if($['user_from'] == $active_username)
{
$qry = "SELECT * FROM users WHERE username ='$active_username'";
}
else
{
$qry ="SELECT * FROM users WHERE username ='$username'";
}
$get_user_info = mysql_query($qry);
I was able to get messages sequentially between my friend and me.... on basis of pid of the table pvt message using
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE (user_from='$active_username' && user_to='$username') || (user_from='$username' && user_to='$active_username') ORDER BY id ASC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$get_user_info = mysql_query("SELECT * FROM users WHERE (username='$active_username' || username='$username')");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
echo "
<div style='float: left;padding-top:5px;'>
<img src='$profilepic_info' height='20' width='20'>
</div>
<div style='padding-left:20px;padding-top:5px;'>
<a href='$active_username'>Me:</a> $body
</div></br>
<div style=' margin-left:80px;'>
<br />
</div>
<hr />
";
............. but now while i am fetching the user datas , to show name of user and profile pic too, only my pic is beling displayed in both my messages and my friend messages.
there is something wrong in line:
$get_user_info = mysql_query("SELECT * FROM users WHERE (username='$active_username' || username='$username')");
Thankyou everyone, for suggestion. I had to change a little bit, to get result I wanted.
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$whosent= $row['user_from'];
$whoreceived= $row['user_to'];
$seen=$row['opened'];
/////////////////////////////////////////////////////
$get_user_info = mysql_query("SELECT * FROM users WHERE (username='$whosent')");
////////////////////////////////////////////////////
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
echo "
<div style='float: left;padding-top:5px; padding-left:20px;'>
<img src='$profilepic_info' height='50' width='40'>
</div>
<div style='margin-left:50px;padding-top:5px;'>
<a href='$active_username' style='color:black'> $whosent</a> $body
</div></br>
<div style=' margin-left:30px;'>
<font size='2px'>sent on: $date_send</font>

Php Game Sql Problems

I need help with this code, i need it to add an array in Gangs
under members and seperate them with -
I need every member in the Gang to be listed under Members and separated with -
so i can explode them below.
This is the line that adds the members to gangs but its without - and i think it erases members that are already there.
$result = mysql_query("UPDATE Gangs SET members='".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());
full code
if(isset($_POST['creategang'])){
if(empty($_POST['gang_name'])){
echo "Enter a Gang Name.";
} else {
if (strlen($_POST['gang_name']) > "20"){
echo "The username may not consist out of more then 20 characters.";
}else{
if (ereg('[^A-Za-z0-9]', $_POST['gang_name'])) {
echo "Invalid Name only A-Z,a-z and 0-9 is allowed.";
}else{
$sql = "SELECT name FROM Gangs WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$m_count = mysql_num_rows($query);
if($m_count >= "1"){
echo 'This name has already been used.!';
}else{
$sql = "INSERT INTO Gangs SET name = '".$_POST['gang_name']."' , owner= '$name'";
$res = mysql_query($sql);
$result = mysql_query("UPDATE users SET gang='".mysql_real_escape_string($_POST['gang_name'])."' WHERE id='" .mysql_real_escape_string($_SESSION['user_id']). "'")
or die(mysql_error());
$result = mysql_query("UPDATE Gangs SET members='".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());
echo 'Gang successfully created!';
}
}
}
}
}
?>
this is the code i will use to separate the array
$Gang_array = explode("-", $Gang_members);
Thanks for viewing my question and thanks in advance for helping me
new code to add to gang
<? include_once("connect.php"); ?>
<?
if(isset($_SESSION['user_id'])) {
// Login OK, update last active
$sql = "UPDATE users SET lastactive=NOW() WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
mysql_query($sql);
}else{
header("Location: index.php");
exit();
}
$sql = "SELECT * FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$userip = htmlspecialchars($row->userip);
$name = htmlspecialchars($row->name);
$sitestate = htmlspecialchars($row->sitestate);
$password = htmlspecialchars($row->password);
$mail = htmlspecialchars($row->mail);
$money = htmlspecialchars($row->money);
$exp = htmlspecialchars($row->exp);
$rank = htmlspecialchars($row->rank);
$health = htmlspecialchars($row->health);
$points = htmlspecialchars($row->points);
$profile = htmlspecialchars($row->profile);
$gang = htmlspecialchars($row->gang);
?>
<?php
$sql = "SELECT * FROM Gangs WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$Gang_name = htmlspecialchars($row->name);
$Gang_owner = htmlspecialchars($row->owner);
$Gang_money = htmlspecialchars($row->money);
$Gang_exp = htmlspecialchars($row->exp);
$Gang_level = htmlspecialchars($row->level);
$Gang_members = htmlspecialchars($row->members);
$Gang_array = explode("-", $Gang_members);
$Gang_profile = htmlspecialchars($row->profile);
?>
<div id="content" class="profile">
<h2>Gang Profile</h2>
<form method="post" >
<input type="submit" name="Petition" id="Petition" value="Petition">
</form>
<center>
<h1><?php echo $Gang_name; ?></h1>
Owner: <?php echo $Gang_owner; ?><br>
Gang Cash: $<?php echo $Gang_money; ?><br>
Gang Exp: <?php echo $Gang_exp; ?><br>
Gang Level: <?php echo $Gang_level; ?><br>
Gang Members: <?php echo $Gang_array; ?><br>
</center><br>
<p>Gang Quote</p>
<div id="UserText">
<?php
$Gang_profile = htmlentities($Gang_profile);
$Gang_profile = nl2br($Gang_profile);
$Gang_profile = stripslashes($Gang_profile);
echo $Gang_profile; ?>
</div>
</div>
<?
if (isset($_POST['Petition'])) {
$result = mysql_query("SELECT members FROM Gangs
WHERE name='".$Gang_name."'");
if ($result) {
while($row = mysql_fetch_assoc($result)) {
$members = $row['members'];
}
}
if ($members != '') $members .= '-'.$name;
else $members = $name;
$result = mysql_query("UPDATE Gangs SET members='".$members."' WHERE name='".$Gang_name."'");
}
If I understand your question correctly. You can try this :
Get the members first :
$result = mysql_query("SELECT members FROM Gangs
WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'");
if ($result) {
while($row = mysql_fetch_assoc($result)) {
$members = $row['members'];
}
}
Then add new member and do update :
//This is to check whether $name is already in the gangs
if (strpos($members,$name) !== false) {
if ($members != '') $members .= '-'.$name;
else $members = $name;
//Update to gangs
$result = mysql_query("UPDATE Gangs SET members='".$members."' WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'");
}
else sprintf("%s is in the gangs already",$name);
Hope it helps.
Pseudo code, but this should probably work:
$result = mysql_query("UPDATE Gangs SET members = members + '-' + '".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());

Categories