I trying to get all user with same ip from database - php

I am trying to find all user having same ip in database
and i tried few ways.
Now i am trying this but its only giving me one user
here is my short example of php code:
<?php
$CheckQuery = mysqli_query($GLOBALS["___mysqli_ston"],
"SELECT * FROM `logedfeeds`
WHERE ip='127.0.0.1'
GROUP BY user_id
ORDER BY `id`
DESC LIMIT 1000000");
//create table
$step1 = '<center><table border="1"><tr><td><b>UserID</b></td></tr>';
while($row = mysqli_fetch_assoc($CheckQuery)){
$userip = $row['user_id'];
$step2 = '<tr><td>' . $userip . '</td></tr>';
}
echo "$step1 $step2 </table></center>";
?>
I am trying this code but not seems to work.

You're overwriting your $step2 variable
change your code so it becomes
<?php
$CheckQuery = mysqli_query($GLOBALS["___mysqli_ston"],
"SELECT * FROM `logedfeeds`
WHERE ip='127.0.0.1'
GROUP BY user_id
ORDER BY `id`
DESC LIMIT 1000000");
//create table
$step1 = '<center><table border="1"><tr><td><b>UserID</b></td></tr>';
$step2 = '';
while($row = mysqli_fetch_assoc($CheckQuery)){
$userip = $row['user_id'];
$step2 .= '<tr><td>' . $userip . '</td></tr>';
}
echo "$step1 $step2 </table></center>";
?>
You first declare the $step2 variable outside your while loop and keep concatenating it then you echo it outside the loop.

Try this query:
SELECT * FROM logedfeeds
WHERE ip LIKE '%127.0.0.1%'
GROUP BY user_id
ORDER BY id
DESC LIMIT 1000000

Related

remove duplicate camma from mysql query

I am trying to fetch data with mulyiple value with Mysql IN operator but problem I am getting to much (,,,).
Here is my query :
$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id.= $page_id['page_id'];
$rel_b.= $page_id['rel_blog'];
}
$p_id."<br>";
echo $rel_b."<br>";
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
Ouery output is Like this this is not proper form so please help me .
SELECT * FROM pages WHERE rel_blog IN
(,114,93,126108default,,,,default,,,,default,,default,,,default,,default,,,,,,,default,,default,,,default,,default,,,,,,,,,,,,,,,,,,,,,,,,,,123,145,144,148,81,107default,,,,,default,,,,,,145,138,118default,,,,,,default,,,,,,,default,,,,,,,default,,,,,,,default,,,118default,,,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,default,,,,,,default,,,,,,,,,default,,,,,,,,default,,,,,default,,,,,,,123,122,125,113default,,,default,,,default,,124,145140default,,,,,,,,,,,,,default,,,,,,,,default,,,default,,,default,,,,default,,,,default,,default,,,,default,,,default,,,default,,default,,,,,,,default,,,,,,default,,,,default,,,,,,default,,,default,,,,137default,,,,,,,default,,,default,,,default,,,,,,default,,,,,,,,,,default,,,,,,,,82,83default,,,,,,default,,,,default,,,,default,,,default,,,default,,,,default,,,,default,,,,default,,,,default,,,default,,,,default,,,,default,,,default,,,default,,,,,,default,,,,,,default,,,,,default,,,default,,,,,default,,,,,default,,,,default,,,default,,default,,default,,default,,,,,,,default,,,,default,,default,,,,default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,,,,default,,,,default,,,,,default,,,,,,default,,,,,,,default,,,,,,default,,,,,,,default,,default,,default,,,,,default,,,default,,,default,,,default,,,,default,114,default,,,,,,117,116default,,,,,default,,,,117,116default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,default,,,default,,default,,,,,,default,,,,,,default,,,,,108default,,,,118,129,121,143,80,82,81,148,117,146,107,88,118,82,80,81,148,143,123,118,107,142,116,110,128,127,111,109,125,113,123,118,85,118,128,109,126,93,114,82,142,108,111,90,113,123,144,148,108,107,142,116,115,110,128,127,113,118,144,146,80,148,107,143,135,125,143,110,115,118,132,146,80,123,81,148,107,116,108,109,125,135,113,123,144,118,116,108,127,147,125,123,144,129,107,142,112,116,135,113,123,144,132,118,107,85,123,144,118,82,148,107,123,132,146,148,107,108,122,89,136,146,80,81,117,148,142,107,110,147,119,82,80,145,129,146,80,148,107,111,109,125,113,123,118,148,81,107,131,115,109,121,125,135,113,123,144,107,131,126,93,141,114,124,139,113,91,144,118,81,117,148,107,142,115,143,135,91,118,142,116,87,143,140,123,144,132,145,129,146,82,80,117,91,118,148,137,113,141,93,143,136,123,144,146,82,118,148,85,112,108,127,122,87,143,141,91,144,146,80,82,81,117,148,107,115,109,91,129,146,82,80,117,148,81,142,107,110,130,138,89,126,113,default,default,118,146,82,81,148,107,142,115,109,126,default,123,144,145,80,146,82,81,117,142,128,138,135,93,140,123,144,145,132,82,80,117,83,142,130,140,145,129,82,80,81,108,111,138,89,143,125,145,123,118,82,80,81,117,88,112,108,147,139,119,140,123,144,118,80,82,81,142,107,85,108,115,114,144,118,129,146,80,81,83,120,148,107,142,128,127,110,115,91,123,145,82,117,146,81,80,148,82,80,117,81,148,83,120,107,142,88,131,118,129,146,80,117,107,83,88,142,130,89,125,139,134,113,145,144,118,132,123,82,80,146,81,148,83,123,91,137,140,134,139,113,141,124,93140,150,150)
order by add_date desc limit 7
Actually I can't figure out how you are getting even a single comma... This is how you can try it out.
$get_id = "select rel_blog,page_id from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id.= $page_id['page_id'];
if($page_id['rel_blog']!=="" && $page_id['rel_blog']==="default") {
$rel_b.= $page_id['rel_blog'] .",";
}
}
$p_id."<br>";
$rel_b = substr($rel_b,0,-1); // get rid of the last comma
echo $rel_b."<br>";
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
You can do this before your query.
$rel_b = preg_replace('/,+/', ',', trim($rel_b, ','));
This will remove repeated commas as well as commas from beginning and end of the string.
Try using implode():
$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id = array();
$rel_b = array();
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id[] = $page_id['page_id'];
$rel_b[] = $page_id['rel_blog'];
}
$p_id = implode(',', $p_id);
$rel_b = implode(',', rel_b);
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";

PHP Mysqli trigger a query one after other

I am trying to fetch first id from one table and later after all the ids are fetched i am trying to fetch number of all that id's.
My Problem is
As i want to select the value of first query completion result
I am unable to trigger second query after the first query is completed both are triggerid at a time
First Query
$query ="SELECT * FROM abc WHERE xyz='xyz' And Standard='xyz' ";
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
$ID = array();
while($row=mysqli_fetch_array($data)){
$ID[] = $row['ID'];
}
$IDall = "'" . implode("','", $ID) . "'";
Second Query
$query="SELECT mobno FROM euser WHERE UserId IN ($IDall)" ;
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
$mobiles = array();
while($row=mysqli_fetch_array($data)){
$mobiles[] = $row['MobileNum'];
}
$mobilesStr = implode(',', $mobiles);
echo $mobilesStr;
}
Try this
SELECT mobno FROM euser WHERE UserId IN (SELECT ID FROM abc WHERE xyz='xyz' And Standard='xyz');
You don't need 2 queries. Only 1 is enough
SELECT mobno FROM euser WHERE UserId IN (
SELECT ID FROM abc ...
)
Try this
$query ="SELECT * FROM abc WHERE xyz='xyz' And Standard='xyz' ";
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
if(mysqli_num_rows($data) > 0) {
$ID = array();
while($row=mysqli_fetch_array($data)){
$ID[] = $row['ID'];
}
$IDall = "'" . implode("','", $ID) . "'";
$query2="SELECT mobno FROM euser WHERE UserId IN ($IDall)" ;
$data2=mysqli_query($mysqli,$query2)or die(mysqli_error());
$mobiles = array();
while($row=mysqli_fetch_array($data2)){
$mobiles[] = $row['MobileNum'];
}
$mobilesStr = implode(',', $mobiles);
echo $mobilesStr;
}
In this the second query will execute when the first query has a result.

PHP - Get data from database then fetch it again

I am trying to get data from database then fetch it again with different mysql_query using while() in both query , but the problem it is producing results more than one time because i used while in first query . So any answer to get all data without while() for first query ?
$AllFrnd = "SELECT friend , followers FROM frndlist WHERE userid = '".$_SESSION[' user_id ']."' ORDER BY id DESC";
$getfrnd = mysql_query($AllFrnd);
while($frnd = mysql_fetch_array($getfrnd)) {
$query2 = "SELECT * FROM post WHERE (userid ='".$frnd['friend']."') OR (userid = '".$frnd['followers']."') OR (userid = '".$_SESSION[' user_id ']."') ORDER BY id DESC";
$rs = mysql_query($query2);
while($row = mysql_fetch_array($rs)) {
echo ''.$row['content'].'';
you can try this :
// Associative array
$frnd =mysql_fetch_array($getfrnd,MYSQL_ASSOC);
then get your data as like :
echo $frnd ["friend"]
echo $frnd ["followers"]
And you should use mysqli_fetch_array instead of mysql_fetch_array
Hope it helps
You can use a query just like this :
SELECT *
FROM post
WHERE userid IN (
SELECT followers
FROM frndlist
WHERE userid = '" . $_SESSION['user_id'] . "'
)
OR userid IN (
SELECT friend
FROM frndlist
WHERE userid = '" . $_SESSION['user_id'] . "'
)
OR userid = '" . $_SESSION['user_id'] . "'
ORDER BY id DESC
You can easyly handle by using this function
function sel($table,$field="*", $condition="1",$sort="" ){
if($sort!='') $sort="order by $sort ";
//echo "select $field from $table where $condition $sort ";
$sel_query=mysql_query("select $field from $table where $condition $sort ");
//$sel_result=array();
while($temp_res=#mysql_fetch_array($sel_query))
{
$sel_result[]=$temp_res;
}
return isset($sel_result)?$sel_result: 0;
}
while($frnd = mysql_fetch_array($getfrnd)) {
$temp_res=sel("post","*"," (userid ='".$frnd['friend']."') OR (userid = '".$frnd['followers']."') OR (userid = '".$_SESSION[' user_id ']."') ORDER BY id DESC");
if($temp_res)foreach($temp_res as $row){
echo $row['content'];
}
}

Duplicates in php while loop

I am getting a bunch of id's from the database - for each id, I want to do a mysql query to get the relating row in another table. This works fine although I also need to get similiar data for the logged in user. With the mysql query I am using - it duplicates the logged in user data according to how many id's it originally pulled. This makes sense although isnt what I want. I dont want duplicate data for the logged in user and I need the data to come from one query so I can order things with the timestamp.
<?php
mysql_select_db($database_runner, $runner);
$query_friends_status = "SELECT DISTINCT rel2 FROM friends WHERE rel1 = '" . $_SESSION ['logged_in_user'] . "'";
$friends_status = mysql_query($query_friends_status, $runner) or die(mysql_error());
$totalRows_friends_status = mysql_num_rows($friends_status);
while($row_friends_status = mysql_fetch_assoc($friends_status))
{
$friends_id = $row_friends_status['rel2'];
mysql_select_db($database_runner, $runner);
$query_activity = "SELECT * FROM activity WHERE user_id = '$friends_id' OR user_id = '" . $_SESSION['logged_in_user'] . "' ORDER BY `timestamp` DESC LIMIT 15";
$activity = mysql_query($query_activity, $runner) or die(mysql_error());
$totalRows_activity = mysql_num_rows($activity);
echo "stuff here";
}
?>
You can try this single query and see if it does what you need
$userID = $_SESSION['logged_in_user'];
"SELECT * FROM activity WHERE user_id IN (
SELECT DISTINCT rel2 FROM friends
WHERE rel1 = '$userID')
OR user_id = '$userID' ORDER BY `timestamp` DESC LIMIT 15";
You probably want something like this:
$user = $_SESSION['logged_in_user'];
  $query_friends_status = "SELECT * FROM friends, activity WHERE activity.user_id = friends.rel2 AND rel1 = '$user' ORDER BY `timestamp` DESC LIMIT 15";
You can replace * with the fields you want but remember to put the table name before the field name like:
table_name.field_name
I hope this helps.
<?php
require_once "connect.php";
$connect = #new mysqli($host, $db_user, $db_password, $db_name);
$result = $connect->query("SELECT p.name, p.user, p.pass, p.pass_date_change,p.email, p.pass_date_email, d.domain_name, d.domain_price, d.domain_end, d.serwer, d.staff, d.positioning, d.media FROM domains d LEFT JOIN persons p
ON d.id_person = p.id AND d.next_staff_id_person != p.id");
if($result->num_rows > 1)
{
while($row = $result->fetch_assoc())
{
echo '<td class="box_small_admin" data-column="Imię i nazwisko"><div class="box_admin">'.$row["name"].'</div></td>';
echo '<td class="box_small_admin" data-column="Domena"><div class="box_admin">'.$row["domain_name"].'</div></td>';
}}
?>

MySQL show results with inverted order?

I need to get the last 5 results, that's why I order them by Date DESC but I need to display the results from older to newer. How can I do this?
$data = mysql_query("SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 5");
while($row = mysql_fetch_array( $data ))
{
print "<img class='badge' title='" . $row['Site'] . "' src='http://getfavicon.appspot.com/http://" . $row['Site'] . "?defaulticon=1pxgif' />";
}
$results = array();
while($row = mysql_fetch_array($data)) {
$results[] = $row;
}
$results = array_reverse($results);
foreach ($results as $row) {
echo $row['Site']; // etc
}
Manual link: http://php.net/function.array-reverse.php
$data = mysql_query("
SELECT * FROM (
SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 5)
t ORDER BY Date
");
Also:
Please use mysql_real_escape_string (http://at2.php.net/manual/en/function.mysql-real-escape-string.php) for the variable $user. Depending on where you get that from it could be a security leak through sql injection.
You can actually order a second time in the same query.
I assume that you have an auto incrementad id (I'll call it 'EntryId' in this example) and then you hopefully should get what you need?
SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC, EntryId ASC LIMIT 5

Categories