I have a problem today. Where I was displaying the value of "fldFrom" in my php form from database. And I can successfully display it. But my problem is, how can I display only once even there is a same word in "fldFrom". Like, in my fldFrom there are same value of "06/24/2013" so then when I test it, the result is "06/24/2013 06/24/2013" because in my database of "fldFrom" has a two value but different row.. I want them to be combine as one.
Here's my code:
<?php
$all = mysql_query("SELECT fldFrom FROM tbldata WHERE fldWeek = '$get_week'");
while ($row = mysql_fetch_array($all))
{
echo "<input type='text' name='play[]' value='" . $row['fldFrom']."'>";
}
?>
Here's my database:
*I want to display the result of fldFrom in just once only...Not twice like this one
thanks
you can use distinct in SELECT statment like this:
$all = mysql_query("SELECT DISTINCT fldFrom FROM tbldata WHERE fldWeek = '$get_week'");
for more information check it here http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
Edit1:
you can do same with group by, like this:
$all = mysql_query("SELECT fldFrom FROM tbldata WHERE fldWeek = '$get_week' GROUP BY fldFrom");
Just use the DISTINCT operator of SQL:
SELECT DISTINCT fldFrom FROM tbldata WHERE fldWeek = '$get_week'
DISTINCT removes duplicates for you.
Related
here's the code and i want to echo only 1 city from mysql database!
<?php
include('db.php');
$queryss = mysqli_query($conn, 'SELECT * FROM areas');
while ($rowx = mysqli_fetch_array($queryss)) {
echo "{pro:'$rowx[1]',city:'$rowx[2]', dist:'$rowx[3]', town:'$rowx[4]', area:'$rowx[5]',subarea:'$rowx[6]',ucname:'$rowx[7]'},";
}
?>
and i'm, getting this input here! 3 time karachi in my html, but i want only 1 of this city. SELECT DISTINCT is working in mysql but how can i use it in PHP?
Your query should be
SELECT * FROM areas GROUP BY id
I have tested it.
Use
SELECT DISTINCT column_name1,column_name2 FRAM areas
in your SQL where column_nameN stands for the columns you need for your output.
OR use something like this (untested):
$results = [];
while ($rowx = mysqli_fetch_array($queryss)) {
$results[] = $rowx;
}
$results= array_unique($results);
foreach($results as $rowx) {
echo "{pro:'$rowx[1]',city:'$rowx[2]', dist:'$rowx[3]', town:'$rowx[4]', area:'$rowx[5]',subarea:'$rowx[6]',ucname:'$rowx[7]'},";
}
First Solution
You can insert distinct keyword into your SQL to accomplish what you need, like so
SELECT DISTINCT your_column_name FROM table_name
Second Soltion
You can execute your SQL statement and then use array_unique, to be like so
$selectStatement = mysqli_query($con, 'SELECT * FROM areas');
$selectedArrayValues = mysqli_fetch_array($selectStatement);
$selectedUniqueArrayValues = array_unique(selectedArrayValues);
// Then return that array to your HTML code
I recommend the first solution because it's more optimized
I'm trying to show stuff queried from two tables, but on one html table. Data is shown for the last 30 days, based on which, an html table is being generated.
Currently I'm stuck using two queries and generating two html tables:
$query1 = mysqli_query( $con, "SELECT date, stuff* " );
while( $record = mysqli_fetch_array( $query1 ) ){
echo '<html table generated based on query>';
}
$query2 = mysqli_query( $con, "SELECT date, other stuff*" );
while( $record = mysqli_fetch_array( $query2 ) ){
echo '<another html table generated based on query2>';
}
Is there a possibility to show both queries on one html table instead?
Note that it gets tricky since we have dates on one table which are not necessarily found in the second table or vice-versa.
Thanks for the support guys. So far I'm stuck at this:
SELECT * FROM user_visit_logs
LEFT JOIN surfer_stats ON user_visit_logs.date = surfer_stats.date
UNION
SELECT * FROM user_visit_logs
RIGHT JOIN surfer_stats ON user_visit_logs.date = surfer_stats.date
The query completes, but the 2nd table fields are all null:
Furthermore, it breaks when I add additional clause like:
WHERE user_id = '{$_SESSION['user_id']}' ORDER BY date DESC LIMIT 30
I think you are after FULL OUTER JOIN concept:
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2)
In which you may use common dates as a shared row.
So the query will get to simple one:
$query = "
SELECT table1.date, stuff
FROM table1
LEFT OUTER JOIN table2 ON table1.date = table2.date
UNION
SELECT table2.date, other_stuff
FROM table1
RIGHT OUTER JOIN table2
ON table1.date = table2.date
";
$result = mysqli_query( $con, $query );
while( $record = mysqli_fetch_array( $result ) ){
echo '<html table generated based on query>';
}
Example
This is an schematic diagram of FULL OUTER JOIN concept:
After running into quite a few bumps with this one, I finally managed to merge 2 columns from each table and also to use where and sort clauses on them with the following query:
( SELECT user_visit_logs.user_id,user_visit_logs.date,unique_hits,non_unique_hits,earned,sites_surfed,earnings FROM user_visit_logs
LEFT OUTER JOIN surfer_stats ON user_visit_logs.user_id = surfer_stats.user_id AND user_visit_logs.date = surfer_stats.date where user_visit_logs.user_id = 23 ORDER BY date DESC LIMIT 30 )
UNION
( SELECT surfer_stats.user_id,surfer_stats.date,unique_hits,non_unique_hits,earned,sites_surfed,earnings FROM user_visit_logs
RIGHT OUTER JOIN surfer_stats ON user_visit_logs.user_id = surfer_stats.user_id AND user_visit_logs.date = surfer_stats.date where user_visit_logs.user_id = 23 LIMIT 30 )
Simplified, "user_visit_logs" and "surfer_stats" were the 2 tables needed to be joined.
Absolutely. Just pop them both into a variable:
$data = '';
$query = mysqli_query($con,"SELECT date, stuff* ");
while($record = mysqli_fetch_array($query)) {
$data.= '<tr><td>--Your Row Data Here--</td></tr>';
}
$query2 = mysqli_query($con,"SELECT date, other stuff*");
while($record = mysqli_fetch_array($query2)) {
$data .= '<tr><td>--Your Row Data Here--</td></tr>';
}
echo "<table>$data</table>";
Instead of using echo in your loop, you're just storing the results in $data. Then, you're echoing it out after all data has been added to it.
As for your second point, it's not a big deal if fields don't exist. If they're null, you'll just have a column that doesn't have data in it.
Here's an example with fake column names:
$data = '';
$query = mysqli_query($con,"SELECT date, stuff* ");
while($record = mysqli_fetch_array($query)) {
$data.= "<tr><td>{$record[id]}</td><td>{$record[first_name]}</td><td>{$record[last_name]}</td></tr>";
}
$query2 = mysqli_query($con,"SELECT date, other stuff*");
while($record = mysqli_fetch_array($query2)) {
$data .= "<tr><td>{$record[id]}</td><td>{$record[first_name]}</td><td>{$record[last_name]}</td></tr>";
}
echo "<table><tr><th>ID</th><th>First Name</th><th>Last Name</th></tr>$data</table>";
I have a feeling I may have misunderstood the need. If so, I apologize. If you can elaborate just a bit more I can change my answer :)
Basically, I am seeking to know if there is a better way to accomplish this specific task.
Basically, what happens is I query the db for a list of "project needs" -- These are each uniquer and only appear once.
Then, I search another table to find out how many members have the required "skills - which are directly correlated to the project needs".
I accomplished exactly what I was trying to do by running a second query and then inserting them into an array like this:
function countEachSkill(){
$return = array();
$query = "SELECT DISTINCT SKILL_ID, SKILL_NAME FROM PROJECT_NEEDS";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
$query = "SELECT COUNT(*) as COUNT FROM MEMBER_SKILLS WHERE SKILL_ID = '".$row['NEED_ID']."'";
$cResult = mysql_query($query);
$cRow = mysql_fetch_assoc($cResult);
$return[$row['SKILL_ID']]['Count'] = $cRow['COUNT'];
$return[$row['SKILL_ID']]['Name'] = $row['SKILL_NAME'];
}
arsort($return);
return $return;
}
But I feel like there has to be a better way (perhaps using some kind of join?) that would return this in a result set to avoid using the array.
Thanks in advance.
PS. I know mysql_ is depreciated. It is not my choice on which to use.
SELECT P.SKILL_ID, P.SKILL_NAME, COUNT(M.SKILL_ID) as COUNT FROM PROJECT_NEEDS P INNER JOIN MEMBER_SKILLS M
ON P.SKILL_ID=M.SKILL_ID
GROUP BY P.SKILL_ID, P.SKILL_NAME
I've adjusted Nriddens answer to accomodate for the select distinct, Im under the belief that his adjustment would be ok given SKILL_ID is a primary key
function countEachSkill(){
$return = array();
$query = "
SELECT
COUNT(*) AS COUNT,
PROJECT_NEEDS.SKILL_NAME,
PROJECT_NEEDS.SKILL_ID
FROM
(SELECT DISTINCT
SKILL_ID, SKILL_NAME
FROM
PROJECT_NEEDS) AS PROJECT_NEEDS
INNER JOIN
MEMBER_SKILLS
ON
MEMBER_SKILLS.SKILL_ID = PROJECT_NEEDS.SKILL_ID
GROUP BY PROJECT_NEEDS.SKILL_ID";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
$return[$row['SKILL_ID']]['Count'] = $row['COUNT'];
$return[$row['SKILL_ID']]['Name'] = $row['SKILL_NAME'];
}
arsort($return);
return $return;
I am subquerying on the select distinct because I dont believe you have a dedicated skills table with an auto inc primary key, if that was there I wouldn't be using a subquery.
Can you test this query
select project_needs.*,count(members_skills.*) as count from project_needs
inner join members_skills
on members_skills.skill_id=project_needs.skill_id Group by project_needs.skill_name, project_needs.skill_id
I'm trying to do nested mysql query but I'm failing
it's as simple as I have a table called 'todo' which is for saving to-do list
when I do
well this code is just to show how I wanna do it logically , I know it doesn't work
and I think it probably needs JOIN or UNION but I couldn't do it
$result = mysqli_query($con,"SELECT type FROM todo WHERE user = '$username' GROUP BY type");
while($row = mysqli_fetch_array($result))
{
echo '<td>- '." ".$row['type'].'</td>';
echo "</br>";
$result2 = mysqli_query($con,"SELECT titleFROM todo WHERE type= '$row['type']'");
while($row = mysqli_fetch_array($result2))
{
echo '<td>-- '." ".$row['title'].'
}
</td>';
echo "</br>";
}
I want the result to like
-Work
--Mr Jack
--Company
-School
--Android Class
--Entrepreneurship
--php development
and so on...
Your inner loop overwrites the outerloop's $row variable. Try using a different variable name in the inner loop. Also, there's an error in your inner loop's SQL query (missing space between the tablename and FROM keyword).
I have put together the following code, the problem is that each while loop is only returning one set of data.
$result = mysql_query("SELECT date FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' GROUP BY date");
$i = 1;
echo "<table cellspacing=\"10\" style='border: 1px dotted' width=\"300\" bgcolor=\"#eeeeee\">";
while ($row = mysql_fetch_assoc($result))
{
$date=date("F j Y", $row['date']);
echo $date;
echo "
<tr>
<td>Fixture $i - Deadline on $date</td>
</tr>
";
$result = mysql_query("SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' AND date = '$row[date]' ORDER BY date");
while ($row = mysql_fetch_assoc($result))
{
extract ($row);
echo "
<tr>
<td>$home_user - $home_team V $away_user - $away_team</td>
</tr>
";
}
$i++;
}
echo "</table>";
I should get many dates, and then each set of fixture below.
At the moment, the first row from the first while loop is present, along with the data from the second while loop.
However, it doesn't continue?
Any ideas where I can correct this?
Thanks
Replace
$result = mysql_query("SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' AND date = '$row[date]' ORDER BY date");
while ($row = mysql_fetch_assoc($result))
with
$result1 = mysql_query("SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' AND date = '$row[date]' ORDER BY date");
while ($row = mysql_fetch_assoc($result1))`
What happens with your current code is that after executing internal while, next call (in external loop)to mysql_fetch_assoc($result) always returns false (because you just iterated through it in internal loop). All you need is to use a different variable in internal loop.
You're overwriting the $result variable. Change the second one to $result2 or something and see what happens. Make sure you do it when you set the variable, and use it in the query.
You are changing your $result variable in your loop so it is at the end after the inner while loop has run.
you just mixing variables. second result and row should be named differently.
though it would be better to make it with one single query
Well you've got the answer to your question.
I just want to add that usually you can write a smarter SQL query and manage without an inner query with it's loop. And your code will work a lot faster if you'll have 1 query instead of N+1.
LEFT JOIN usually can be used to replace inner looping. Example:
SELECT DISTINCT A.date, B.* FROM table_fixtures A
LEFT JOIN table_fixtures B ON A.date = B.date
WHERE B.compname = "a value"
However this time it looks illogical. I think what you actually do is achievable with a simple query like this:
SELECT * FROM table ORDER WHERE compname="something" ORDER BY date
You are reusing the same variables over for your inner loop and its breaking the outer loop.
Change your inner loop to something like:
$result2 = mysql_query("SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' AND date = '$row[date]' ORDER BY date");
while ($row2 = mysql_fetch_assoc($result2))
{
extract ($row2);
echo "
<tr>
<td>$home_user - $home_team V $away_user - $away_team</td>
</tr>
";
}
On a different note - why do you have a GROUP BY date in the first row, when all you have in the projection list is the date?