Hi all i've had help from bluefleet (BF) with a mysql query which i've now put in a php file, the query works fine but i find i need to get extra data as well as the count, so what i need is when a match is found i need to look up the username in table tview3.
Whichever table the ip match is found there will be a column called UID, in the table tview3 there will also be a column called USERNAME, what i'd like to do is get the username(s) for the matches and then use them in a text file.
I have to state i'm a complete newbie at this, all help is appreciated
$myquery= "select sum(total)
from
(
SELECT count(*) as total
FROM " .TABLE_PREFIX."tview v
where v.ipaddress = '$ips'
union all
SELECT count(*) as total
FROM " .TABLE_PREFIX."tview2 v1
where v1.ipaddress = '$ips'
union all
SELECT count(*) as total
FROM " .TABLE_PREFIX."tview3 v3
where v3.ipaddress = '$ips'
) src";
$result = mysql_query($myquery);
$rowCount = mysql_num_rows($result);
If($rowCount !=0){
echo "NOT EMPTY";
}else{
echo "EMPTY";
}
mysql_free_result($result);
$UAM = strtoupper($_SERVER['HTTP_USER_AGENT']);
$ips = strtoupper($_SERVER['REMOTE_ADDR']);
$fp = fopen("logtext.txt", "a");
$DateOfRequest = date('m-d-Y H:i:s');
fwrite($fp, "$DateOfRequest . \nMatched Member: . $username . \nWith User Agent: . $UAM . \n\nIP: . $ips . \n\n");
So i'd like to populate a variable $username with the username(s) where the matches were found.
Regards,
Silo
Change:
SELECT count(*) as total
to
SELECT count(*) as total, username
Then you could do something like:
$row = mysql_fetch_array($query);
$username = $row['username'];
Hope that helps. Also check out this question for a bit more detail: How do I find the most common result in a column in my MySQL table
Related
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("students");
$id = $_POST['id'];
$grade = $_POST['grade'];
$query = "INSERT INTO `st_table` (`St_id`,`Grade`) VALUES ('$id','$grade')";
$result = mysql_query($query);
$query = "SELECT * from `st_table`";
$result = mysql_query($query);
echo "<table>";
echo "<th>St_id</th><th>Grade</th>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['St_id'] . "</td><td>" . $row['Grade'] . "</td></tr>";
}
This code adds values into a table both ID and Grade. I want another query that will be able to count how many As, Bs, Cs, etc. and OUTPUT it on an html table.
Here, Your query is ok just group by Grade not Grades
"SELECT `Grade`, COUNT(*) AS count FROM `st_table` GROUP BY `Grade`";
Here is sqlfiddle
After edit
The query i am mentioning should work for you, you can check fiddle for that as for as you modified code is concerned you have to change your table a bit since you are going to include St_id as well so make it 3 column and correspondingly change query too.
I'm trying to use the row from a query in another query.
This query correctly displays the username of the user currently signed in:
$param = $fgmembersite->UserEmail();
$query = mysqli_query($con, "SELECT username FROM Users1 WHERE email = '$param'
");
while ($row = mysqli_fetch_array($query)){
echo $row['username'] ;
}
I'm trying to find a way to use $row['username'] in another query something like...
$sql = mysqli_query($con, "SELECT * FROM messages WHERE to_user = '" . $row['username'] . "' ");
This doesn't give me a coding error, but it doesn't work. The username row obviously can't be taken from a separate query the way I'm attempting.
I have tried every combination I can think of but nothing has worked for me. Any help greatly appreciated.
Try a subquery
SELECT * FROM messages WHERE to_user in (SELECT username FROM Users1 WHERE email = '$param')
You can join the queries into one:
SELECT `messages`.*
FROM `messages`
JOIN `Users1`
ON `Users1`.`username`=`messages`.`to_user`
WHERE
`Users1`.`email`='$param'
Well you should place your second query inside while:
while ($row = mysqli_fetch_array($query)){
echo $row['username'] ;
$sql = mysqli_query($con, "SELECT * FROM messages WHERE to_user =
'" . $row['username'] . "' ");
}
Now loop ends when mysqli_fetch_arrray returns NULL and that NULL you are trying to insert into second query.
You can combine the two queries into one.
SELECT * FROM messages WHERE to_user = (SELECT username FROM Users1 WHERE email = '$param')
please assist. i have a database with a couple of tables and rows and i want the php to print specific rows as and when i want it to. at the moment it renders all the content of the spesific table on my webpage. in future, i would like it to display the contents of a specific table if a cirtain user is logged in so im going to do that when i understand if statements and get over this hurdle 1st. my code is as follows:
<?php
include 'connect-mysql.php';
echo "<br/>";
$query = "SELECT CUSTOMER_NAME, RAMSCODE FROM customer";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
echo "{$row['CUSTOMER_NAME']} <br>" .
"RAMSCODE: {$row['RAMSCODE']} <br>" ;
}
?>
To fetch specific rows from a table you have to include a WHERE clause in your SQL statement.
For example:
$query = "SELECT CUSTOMER_NAME, RAMSCODE FROM customer WHERE customer_id = 2";
Match the WHERE xxxxx clause to any column in your table
You need to specifiy yor criteria as a where clause in the SQL
$query = "SELECT CUSTOMER_NAME, RAMSCODE FROM customer where RAMSCODE = %1";
$result = mysql_query($query,mysql_real_escape_string($yourcode)) or die (mysql_error());
Also you really need to Read the Manuals!
As far as i've get what you want is to display only that row from customers table, which customer is logged in. you can use some thing like this:
while($row = mysql_fetch_array($result))
{
if($row['CUSTOMER_NAME'] == " //Customer Logged In (customer name from session)"){
echo "{$row['CUSTOMER_NAME']} <br>" .
"RAMSCODE: {$row['RAMSCODE']} <br>" ;
}else{
//do nothing or continue with printing all
}
}
Hope this helps.
Im trying to call all users from a database with the same interests as the current, logged in user on my website.
I have the following
// Get Session USER interest
$interestsquery = "SELECT `interest` FROM `user_interests` WHERE `user_id` = " . $usersClass->userID();
$result = mysql_query($interestsquery);
$interests = array();
while(list($interest) = mysql_fetch_array($result))
$interests[] = $interest;
$interest1 = $interests['1'];
$interest2 = $interests['2'];
$interest3 = $interests['0'];
// END INTERESTS
//USers with Same Interests
$interests_query = "SELECT * FROM produgg_users
join user_interests on produgg_users.id = user_interests.user_id
where interest = '$interest1' and produgg_users.id != '".$usersClass->userID()."'";
$interests_result = mysql_query($interests_query) or die(mysql_error());
if($interests_result != 0) {
while($interests_row = mysql_fetch_array($interests_result, MYSQL_ASSOC))
{
echo $interests_row['user_id'];
}
}
else
{
print "No users to display!";
}
//END SAME INTERESTS
which doesnt bring back any data, yet if I add (beneath //USers with Same Interests)
$interest1 = 'footy';
the interests_query seems to work, can anybody see where im going wrong?
My problem seems to lie here...
$interest1 = $interests['1'];
$interest2 = $interests['2'];
$interest3 = $interests['0'];
// END INTERESTS
//USers with Same Interests
$interest1 = 'footy';
If I manually assign a value to $interest variable it works, but i need to get use the value from the array above, does this make sense?
If your code brings back the correct data when you add $interest1 = 'footy'; line, that would imply that there is something wrong with the value of that variable when you don't. Have you tried var_dump($interest1); right under //Users with Same Interests line to see what kind of input you get from your interestsquery?
I would expect the var_dump to not return a valid string (since if it would, the query would work following the $interest1 = 'footy'; assumption), so you would have to look at what interestsquery returns wrong.
Looks like you querying user_id from user_interests as number, but from produgg_users as string. Maybe there's a problem
You can do it with one query:
$userID = mysql_real_escape_string($usersClass->userID());
$sql = "
SELECT * FROM user_interests AS ui1
JOIN LEFT user_interests AS ui2 ON ui1.id = ui2.id
JOIN LEFT produgg_users AS pu ON ui2.user_id = pu.id
WHERE ui.user_id = " . userID ;
I'm trying to count the yes and no votes so that I can just post the total yes/no for my website. This is should be easy, but I must be missing something somewhere since I don't get a return result. At least no php error. My total vores
$result = mysql_query("SELECT * FROM Poll");
$votes_Poll = mysql_num_rows($result);
$vote_yes = mysql_query("SELECT vote, COUNT(*) FROM Poll GROUP BY yes");
$vote_no = mysql_query("SELECT vote, COUNT(*) FROM Poll GROUP BY no");
// Display the results
echo $votes_Poll;
echo "<br>";
echo $vote_yes;
echo "<br>";
echo $vote_no;
thanks in advance
That's because you're echoing out a query object. Each of your queries should be...
$vote_yes = mysql_query("SELECT COUNT(*) AS total FROM Poll WHERE vote = 'yes' ");
$row = mysql_fetch_object($vote_yes);
echo $row->total; //echoes out the number of yes votes
You are misunderstanding how GROUP BY works. You do not use it to select an answer, it is used to select a field, by which aggregate functions are grouped.
If you use:
SELECT `vote`, COUNT(*) FROM Poll GROUP BY `vote`
Then this will return two results, each with two values, the vote (yes or no) and the count for that vote.
$handle = mysql_query("SELECT `vote`, COUNT(*) AS `count` FROM Poll GROUP BY `vote` ORDER BY `vote` DESC");
if ($handle) {
$results = mysql_fetch_assoc($handle);
echo ($results[0]['count'] + $results[1]['count']) . "<br>" . $results[0]['count'] . "<br>" . $results[1]['count'];
}
NB. Using the ORDER BY vote DESC part, that forces the order to yes then no (reverse alphabetical) and then you do not have to check which row is which.