how to get data collectively from table columns - php

i have such a table:
r_id date recipe_name
1 2012-05-20 Cheese Bread
1 2012-05-21 Cheese pie
2 2012-05-20 Spanish Bread
I would like to get all the data under r_id 1 to be in a single row how can i do that using Sql.I need to achieve something like this:
r_id recipe_name
1 Cheese Bread,Cheese pie
2 Spanish Bread
how can i do this using php too?

Use GROUP_CONCAT
SELECT r_id, GROUP_CONCAT(recipe_name)
FROM yourTable
GROUP BY r_id

Here's the php version
$query = "SELECT id, recipe_name FROM myTable";
$rs = mysqli_query($query);
$results = array();
while($r = mysqli_fetch_assoc($rs)) {
$results[$r['id']][] = $r['recipe_name'];
//$results[$r['id']][] = "<a href=''>".$r['recipe_name']."</a>";
}
foreach($results as $id => $recipes) {
print $id . ' ' . implode(',', $recipes) . "<br>";
}

Related

Unexpected result with "group by"-query

I am doing pagination stuff. I have sales transaction, each of which has a unique transaction id.
$sql = "SELECT COUNT(*) from sale_history group by transaction_id";
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
$rows = $row[0];
print $rows;
exit;
Data in my table is like:
sale id item customer transaction_id
1 abc aaa 1234
2 def aaa 1234
3 abc bbb 9876
4 def bbb 9876
Now this count should give me row count 2, but it is giving me 1.
In order to output Number of Rows, you should use
mysql_num_rows
$sql = "SELECT COUNT(*) from sale_history group by transaction_id";
$query = mysql_query($sql);
$num_rows = mysql_num_rows($query);
You are looking for mysql_num_rows()
***mysql_* is a depreacted library on php though. Use PDO or mysqli_* instead.

How to show the result form getting array?

Database :
--> product table
P_id P_name P_uploadKey
1 Cemera 7365
2 Notebook 7222
3 Monitor 7355
4 Printer 7242
--> buy table
B_id P_id B_name date
1 1,3,4 somchai 12/3/2016
2 2,3 kri 12/3/2016
This sql to show the find id on buy table where $_GET['B_id'] = '2' :
$bid = $_GET['B_id'];
$sqlB ="select * from buy where B_id ='$bid' ";
$Recordset2 = mysql_query($sqlB, $connect) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
And this sql code to show the result what is they buy, by get the $row_Recordset2['P_id'] like a 2,3 from code above :
$pid = $row_Recordset2['P_id'];
$sqlp ="select * from buy where P_id ='$pid' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
I want the to show like this, how we edit it:
Notebook
Monitor
This is answer i can do it.
$pid = $row_Recordset2['P_id'];
$array = explode(',', $pid);
foreach ($array as $item) {
$sqlp ="select * from buy where P_id ='$item' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
}
You can use like below,
$sqlp ="select * from product where P_id IN '($pid)'";
instead of
$sqlp ="select * from buy where P_id ='$pid' ";

Get information from MySql Query and put it in PHP array

I have a table of which I must select information using a MySQLi Query and push it to the end of a PHP array.
The table is in this format:
table = friends
id user1 user1_id user2 user2_id datemade accepted
1 name1 1 name2 2 2015-05-27 03:24:32 1
2 name3 3 name2 2 2015-05-27 03:24:32 1
3 name3 3 name1 1 2015-05-27 03:24:32 1
4 name4 4 name2 2 2015-05-27 03:24:32 1
id = an auto_incrementing number to keep track of everything
user1 = the person's name that asks for friendship
user1_id = that person's special unique id
user2 = name of the person that accepts/decline's friendship
user2_id = that person's special unique id
datemade = the date it was made :P
accepted = did he accept? (don't worry about this)
I want to select all users that are friends with $u.
In this example, $u's id is 1 (name is name1).
After running the query it would push it to the end of friend_array.
So if I printed this array the output would be:
2, 3
Since, id=1 is friends with id=2 and id=3
What query should I do and how would I push that to an array (I know about about array_push but I do not know how to implement it)?
Please try this code. It will return the array for all user's friends.
$sql = "SELECT user1 AS user FROM friends UNION SELECT user2 AS user FROM friends";
$data = mysqli_query($sql);
while ($v = mysqli_fetch_assoc($data)) {
$sql = mysqli_query("SELECT * FROM `friends` where (user1 = '" . $v['user'] . "' or user2 = '" . $v['user'] . "')");
$arr = array();
while ($array = mysqli_fetch_assoc($sql)) {
if ($array['user1'] == $v['user']) {
$arr[$v['user']][] = $array['user2_id'];
} else {
$arr[$v['user']][] = $array['user1_id'];
}
}
}
I finally figured it out!
$sql = "SELECT COUNT(id) FROM friends WHERE user1_id='$log_id' AND accepted='1' OR user2_id='$log_id' AND accepted='1'";
$query = mysqli_query($db_conx, $sql);
$query_count = mysqli_fetch_row($query);
$friend_count = $query_count[0];
//echo($friend_count); //how many friends
if($friend_count < 1){
echo($u." has no friends yet");
} else {
$all_friends = array();
$sql = "SELECT user1_id FROM friends WHERE user2_id='$log_id' AND accepted='1' ORDER BY RAND()";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
array_push($all_friends, $row["user1_id"]);
}
$sql = "SELECT user2_id FROM friends WHERE user1_id='$log_id' AND accepted='1' ORDER BY RAND()";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
array_push($all_friends, $row["user2_id"]);
}
$friendArrayCount = count($all_friends);
}
So it first counts how many friends you have to check if you have any, if you do not a, temp. message appearing saying no friends. If you have some friends it will run a query to check what friends you have is both columns - user1_id and user2_id. It will then finally add everything to the $all_friends array and it then counts how many friends you have.
Thank you to everyone that helped!

codeigniter get field names and row values into a single string

i want to get the field names and the row values into a single string.
example:
id | fname | gender | email
1 | john | male | 1#1.com
2 | mary | female | 2#2.com
select * from table where id=1
$this->db->where('id', 1);
$query = $this->db->get('table');
$row = $query->row();
$string = $row->id . $row->fname . $row->gender . $row->email;
Desire outcome:
id:1, fname:john, gender:male, email:1#1.com
or
id, fname, gender, email, 1, john, male, 1#1.com
Any advise on how to construct the sql query dynamically, many thanks.
$query = $this->db->query("select whatever from wherever.");
$string = "";
foreach ($query->result_array() as $row)
{
foreach($row as $key=>$val){$string.="$key:$val,";}
}
The only problem with selected answer is that if the query returns no records then you will not get any field names either.
You may prefer to get the field names discretely with the following command
$query = $this->db->query("select * from users");
$field_array = $query->list_fields();
$data = "";
foreach($field_array as $field){
$data .= $field . ", "
}

MySQL join from two tables with one condition

I can't figure out join..
need to select all rows from one table where a column equals something in another table..
like this:
SELECT ALL FROM someTable
WHERE COLUMN someColumn = '123' (IN A DIFFERENT TABLE)
something like that..
and the IDs need to match of course..
Just use an INNER JOIN:
SELECT *
FROM SomeTable S
JOIN SomeOtherTable S2
ON S.SomeKey = S2.SomeKey
WHERE S.SomeColumn = '123'
A Visual Explanation of SQL Joins
I wasn't completely clear of your question, so you may not need the WHERE clause if that represented your JOIN criteria.
A bit error in my question, let me clarify
tblplace
id place
1 London
2 Paris
3 New York
tbltraveller
tr_id tr_name id
1 John 3
2 Jackson 2
3 Susanna 1
4 Jimmy 3
5 Lucy 2
The problem is missing some data for New York:
Paris Jackson Lucy
New York
My code(guess there is an error in the while loop):
<?php
require_once("connMysql1.php");
$qry = "SELECT * FROM tblPlace WHERE tblPlace.id > 1 ORDER BY tblPlace.id";
$result = mysqli_query($db_link, $qry);
$qry1 = "SELECT tblPlace.id, tblPlace.Place, tbltraveller.tr_id,
tbltraveller.tr_name, tbltraveller.id FROM tblPlace INNER JOIN tbltraveller ON
tblPlace.id = tbltraveller.id WHERE tblPlace.id > 1 ORDER BY tbltraveller.id";
$result1 = mysqli_query($db_link, $qry1);
$no_of_row1 = mysqli_num_rows($result1);
$no_of_row = mysqli_num_rows($result);
echo "<table border=1 cellpadding=10>";
if ($no_of_row >0){
while ($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>";
echo $row['place']."<br>";
echo "</td>"." ";
echo "<td>";
while ($row1 = mysqli_fetch_assoc($result1)){
if($row1['id'] == $row['id']){
echo $row1['tr_name']." ";
}
}
echo "</td>";
"</tr>";
}
}
echo "</table>";
?>
`

Categories