I have 3 tables cam_details, cam_category and upload_data as following..
cam_details:
+---------+-----------+-----------
| cam_id | category_id| cam_name |
+========+============+===========
| 1 | 1 | CCTV |
+--------+------------+-----------
| 2 | 1 | CCtv2 |
+--------+------------+===========
| 3 | 2 | cctv3 |
+--------+------------+===========
| 4 | 4 | cctv4 |
+--------+------------+===========
cam_category:
+-------------+---------------+
| category_id | category_name |
+=============+===============+
| 1 | Analog |
+-------------+---------------+
| 2 | Digital |
+-------------+---------------+
| 3 | Network |
+-------------+---------------+
| 4 | Simple |
+-------------+---------------+
upload_data:
+---------+-----------+
| cam_id | FILE_NAME |
+========+============+
| 1 | abc.jpg |
+--------+------------+
| 1 | abc2.jpg |
+--------+------------+
| 1 | abc3.jpg |
+--------+------------+
| 2 | xyz.jpg |
+--------+------------+
now i want to fetch the details of the cam in a array and display the results. i am able to fetch cam pic and cam_details but not the cam_category name.
code below...
<?php
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT cam_details.*, upload_data.FILE_NAME FROM `cam_details`
JOIN upload_data on cam_details.cam_id = upload_data.cam_id LEFT JOIN cam_category c
on cam_details.category_id = c.category_id
GROUP BY upload_data.cam_id ORDER BY cam_id DESC");
while($row = mysqli_fetch_array($result))
{?>
instead of left join do only join and use selec command at first for the cateroy table...
<?php
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT cam_details.*, upload_data.FILE_NAME, cam_category.* FROM `cam_details`
JOIN upload_data on cam_details.cam_id = upload_data.cam_id JOIN cam_category
on cam_details.category_id = cam_category.category_id
GROUP BY upload_data.cam_id ORDER BY cam_id DESC");
while($row = mysqli_fetch_array($result))
{?>
Related
I have 2 tables in my product database:
product_list(id, Product_ID, Product_Name, Supplier),
product_option (id, Product_ID, Color, Size). both 'id's are primary keys with auto_increment.
I want to print all Color and Size values under each Product_Name (without repetition) that is from product_list table. I've been trying to figure out how to properly use foreach loop within while loop but now I'm out of related search result.
How my tables look:
product_list table:
|id | Product_ID | Product_Name | Supplier |
| -- | ---------- | ------------ | -------- |
| 1 |A1 | product1 | company1 |
| 2 |A2 | product2 | company2 |
| 3 |A3 | product3 | company3 |
| 4 |A4 | product4 | company4 |
product_option table:
|id |Product_ID | Color | Size |
| -- | --------- | ----- | ---- |
| 1 |A1 | red | S |
| 2 |A1 | red | M |
| 3 |A1 | black | S |
| 4 |A1 | black | M |
...
My expected output is:
| Product_ID | Product_Name | Supplier |
|:----------:|:------------:|:-----------:|
| A1 | Product1 | companyname |
| | red S | |
| | red M | |
| | black S | |
| | black M | |
| A2 | Product2 | companyname |
| | Large | |
Color and Size from product_option table with the same Product_ID will display under Product_Name row and Product_Name from product_list will only display once (instead of 4 times in the case of A1).
These are my code so far: (didn't write any table or styling for clean view)
include_once 'includes/dbh.inc.php';
$sql = "
SELECT
pl.Product_ID pid,
po.Product_ID poid,
pl.Product_Name,
po.Color color,
po.Size size,
pl.Supplier
FROM
product_list pl
LEFT JOIN
product_option po ON pl.Product_ID = po.Product_ID
ORDER BY
pl.Product_ID;";
$result = mysqli_query($conn, $sql) or die(mysqli_error());
if ($result -> num_rows > 0){
while ($row = $result -> fetch_assoc()) {
echo $row['pid'] . " " . $row['Product_Name'] . " " . $row['Supplier'] . "<br><br>";
if (!empty($row['color'] || $row['size'])) {
foreach ($row as $data) {
echo $data['color'] . ' /' . $data['size'] . '<br><br>';
}
}
}
}
Connection file: I use Xampp - phpmyadmin.
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "product";
// Create Connection
$conn = new mysqli ($dbServername, $dbUsername, $dbPassword, $dbName);
// Check Connection
if ($conn -> connect_error) {
die("Connection Failed: " . $conn -> connect_error);
}
I'm ashamed to admit that the second 'if' and the foreach doesn't seem to work, and I don't know where to include the Product_ID match condition..
So far the output of this code is just 'A1 product1 company1', only the first result of the while loop.
From comment:
If it's ok for you to change how the data is being showed in the field, I suggest to make it horizontal with a query like this:
SELECT
pl.Product_ID pid,
po.Product_ID poid,
pl.Product_Name,
group_concat(concat(color,' ',size) separator ', ') AS Product_Name,
pl.supplier
FROM
product_list pl
LEFT JOIN
product_option po ON pl.Product_ID = po.Product_ID
GROUP BY pl.Product_ID, po.Product_ID,pl.Product_Name, pl.supplier
ORDER BY
pl.Product_ID;
Returns value like following:
+-----+------+---------------+---------------------------------+----------+
| pid | poid | Product_Name | Product_Name | supplier |
+-----+------+---------------+---------------------------------+----------+
| A1 | A1 | product1 | black M, black S, red M, red S | company1 |
.....
A fiddle of the tests
if ($result -> num_rows > 0)
mysqli_num_rows() This is a function so it is being ignored, it will always be "Zero" 0 so you will only get the indexed result which begins with '0'.. e.g [ 0,1,3]
This question already has answers here:
How to join two tables mysql?
(4 answers)
Closed 2 years ago.
I have two tables that looks something like this (made as example):
Table sales:
| ID | date | displayname | status |
| 1 | 2020/08/03 16:25:26 | Angel | OK |
| 2 | 2020/08/03 16:25:26 | Angel | OK |
| 3 | 2020/08/03 16:25:26 | Cabil | X |
| 4 | 2020/08/03 16:25:26 | Syed | OK |
...
Table users (all of the columns has value, but removed for GDPR reasons):
| ID | displayname | fullname | email |
| 1 | Angel | | |
| 2 | Nico | | |
| 3 | Raquie | | |
| 4 | Cabil | | |
| 5 | Syed | | |
...
I have a PHP script that looks like this:
<?php
$query = "SELECT * FROM sales WHERE status='OK' ORDER BY STR_TO_DATE(`date`, '%Y/%m/%d %H:%i:%s') DESC LIMIT 5";
if ($result = $link->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
echo '<div class="my-box">';
echo "{$row['id']}";
echo "{$row['date']}";
echo "{$row['dbirth']}";
echo "{$row['email']}";
echo "{$row['displayname']}";
echo '</div>';
}
$result->free();
}
?>
Now it currently displays each displayname for each sale in echo "{$row['displayname']}";, but insted of the displayname, I want to show the fullname for the user that has the current display name. How can I accomplish this?
You seem to be looking for a join:
select s.*, u.fullname
from sales s
inner join users u on u.displayname = u.displayname
Basically I want to do a search by category and title. My problem is that the two are located in separate tables. I was thinking of putting 2 variables in the mysqli_num_rows or mysqli_fetch_array but I don't think it's the right idea. The $search variable is working already but I don't know what I will do for $searchcat that is a different table.
<table border="1">
<tr>
<th>ID</th>
<th>Survey Title</th>
<th>Category</th>
</tr>
<?php
if (isset($_POST['submit']))
{
include 'testdb.php'; //connection is written in other page (db.php)
$var =$_POST['search'] ;
$searchtype = $_POST['searchtype'];
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
echo "<h3>Search results:</h3>";
while($show = mysqli_fetch_array($get_data))
{
$id = $show['survey_id'];
$title = $show['title'];
$category = $show['categoryname']; //
echo "<tr align='center'>";
echo "<td><font color='black'>" .$id. "</font></td>";
echo "<td><font color='black'>" .$title. "</font></td>";
echo "<td><font color='black'>" .$category. "</font></td>";
}
}
else{
echo "No Records found!";
}
}
?>
</table>
</body>
This is table category (categoryname is what I need)
+-------------+---------------+-------------+
| category_id | categoryname | datecreated |
| 1 | Philosophical | |
| 4 | Political | |
| 6 | Social | |
This is table survey (title is all I need)
| 1 | survey_id | title | description | duration | gender | age_group_from | age_group_to |
| 2 | 44 | game1 | description1 | 5 | male | 0 | 18 |
| 3 | 45 | game2 | description2 | 25 | female | 18 | 25 |
| 4 | 46 | game3 | description3 | 89 | female | 26 | 35 |
This is table survey_header (survey_id and category_id is what I need)
| 1 | survey_id | date_created | date_updated | opening_date | closing_date | category_id | topic_id |
| 2 | 33 | Not important | Not important | NULL | NULL | 1 | NULL |
| 3 | 45 | Not important | Not important | NULL | NULL | 6 | NULL |
| 4 | 46 | Not important | Not important | NULL | NULL | 4 | NULL |
Try this query :
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
/*create table*/
}
else
// do something else
How can I call the lowest ID (nNr) and the entry next to it ($cPfad) in PHP? I have 2 Tables and try to get from both of them the Details of an ID.
My SQL
SELECT
p1.kArtikel,
p1.cName,
p1.cKurzBeschreibung,
p1.dLetzteAktualisierung,
p1.dErstellt,
p1.cSeo,
p2.kartikelpict,
p2.nNr,
p2.cPfad
FROM tartikel AS p1
JOIN tartikelpict AS p2
ON (p1.kArtikel = p2.kartikelpict)
JOIN (SELECT kartikelpict, MIN(nNr) min_nNr FROM tartikelpict GROUP BY kartikelpict) p3
ON (p2.kartikelpict = p3.kartikelpict AND p2.nNr = p3.min_nNr)
ORDER BY p1.kArtikel
DESC LIMIT 10
DB Tables
+----------+-------+---------------------+------------+-------+
| kArtikel | cName | cKurzBeschreibung | dErstellt | cSeo |
+----------+-------+---------------------+------------+-------+
| 560 | Title | Short Description | 2014-03-25 | title |
+----------+-------+---------------------+------------+-------+
+--------------+--------------+-------+
| kartikelpict | cPfad | nNr |
+--------------+--------------+-------+
| 560 | picture4.jpg | 4 |
| 560 | picture3.jpg | 3 |
| 560 | picture2.jpg | 2 |
| 560 | picture.jpg | 1 |
+--------------+--------------+-------+
My PHP Code
while ($row = mysql_fetch_assoc($result)){
$cName = $row['cName'];
$cKurzBeschreibung = $row['cKurzBeschreibung'];
$dLetzteAktualisierung = $row['dLetzteAktualisierung'];
$dErstellt = $row['dErstellt'];
$cSeo = $row['cSeo'];
$date = strtotime($row['dErstellt']);
$pubdate = date(r, $date);
$id = $row['kArtikel'];
$nNr = $row['p3.min_nNr'];
$cPfad = $row['cPfad'];
echo"<item>";
echo"<title><?php echo $cName; ?></title>";
echo"<link>http://domain.com/".$cSeo."</link>";
echo"<guid isPermaLink='false'>http://domain.com/".$cSeo."></guid>";
echo"<pubDate>".$date."</pubDate>";
echo"<description><![CDATA[ <img src='http://domain.com/bilder/produkte/normal/".$row['nNr']."' />".$cKurzBeschreibung." ]]></description>";
echo"<enclosure url='http://domain.com/bilder/produkte/normal/'".$nNr." type='image/jpeg' />";
echo" </item>";
}
The call of $cPfad does not output anything, the call of of $nNr call each ID instead of the lowest one to get the correct $cPfad. What am I doing wrong im my PHP Code?
I have a car booking system database that i am in the process of building. I have 3 tables:
car_table:
ID | car_make | car_name
1 | Ford | Focus
2 | BMW | Z3
3 | Audi | A5
booking_table:
booking_ID | booking_time | total_cost | etc..
125674 | 2013-02-02 | 91.55
887463 | 2013-01-19 | 52.00
209930 | 2013-01-11 | 23.99
reservation_table:
ID | booking_ID | car_ID
1 | 125674 | 2
2 | 887463 | 2
3 | 209930 | 1
So the reservation_table links the cars with the bookings. Now i am wanting to output the following result in a table for the user:
booking_ID | booking_time | total_cost | car_ID | car_make | car_model
How can i output the results for this, so far i have this almost working:
$query = "
SELECT
booking_ID,
total_cost,
booking_time,
customer_ID,
car_ID
FROM
bookings_table as bb,
reservation_table as br
WHERE
bb.booking_ID = br.booking_ID AND
payment_success=true
ORDER BY
booking_ID desc
LIMIT
10
";
try {
$stmt = DB::get()->prepare($query);
$stmt->execute();
$rows = $stmt->fetchAll();
}
catch(PDOException $ex) {
// Failed
}
foreach($rows as $row):
<td>'.$row['booking_ID'].'</td>
etc...
endforeach;
Which displays as below:
booking_ID | booking_time | total_cost | car_ID | car_make | car_model
125674 | 2013-02-02 | 91.55 | 2 | |
So i can get the car_ID from the reservation_table but i don't know how i can also pull out the car_make/model by taking the car_ID from the reservation_table.