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'];
}
}
Related
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.
I posted a code below about my website. In this code i want to update rows in my database, if the user changed the name of the topic on the website's form. Everything is working except the sql part. I mean the part where:"LIMIT 1 OFFSET '$x'" this part of the sql code is not good for some reason, but i don't know why. I tested it in xampp phpmyadmin and it works but here something just wrong.
<?php
$sql = "SELECT topicname, username, created, COUNT(commentid)
FROM user, topic, comment
WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner
AND user.username = '" . $_SESSION['user_name '] . "'
GROUP BY topicname ";
$lekerdezes = mysql_query($sql);
$num_rows = mysql_num_rows($lekerdezes); ?>
<?php
if (isset($_POST['delete']))
{
if (!empty($_POST['forumnev']))
{
for ($x = 0; $x < $num_rows; $x++)
{
foreach ($_POST['forumnev'] as $selected)
{
$seged = mysql_query("SELECT created FROM topic WHERE
created IN (SELECT created FROM user, topic, comment WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner AND user.username = '" . $_SESSION['user_name '] . "'
GROUP BY topicname ORDER BY created)
LIMIT 1 OFFSET '$x'");
if (!$seged)
{
echo mysql_error();
}
$seged2 = mysql_fetch_array($seged);
$seged2 = $seged2[0];
if (!$seged2)
{
echo mysql_error();
}
$sql = mysql_query("UPDATE topic SET topicname = '$selected' WHERE created = '$seged2'");
}
}
header("Location: topicedit.php");
}
}
?>
Try updating as follows:(Hope your limit: 1 and offset: $x)
$seged = mysql_query("SELECT created FROM topic WHERE created IN (SELECT created
FROM user,topic,comment
WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner
AND user.username = '". $_SESSION['user_name'] ."'
GROUP BY topicname
ORDER BY created)
LIMIT $x, 1");
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
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>';
}}
?>
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