I have problem when I want to display data by json file. If I display data in one table it is ok, but when I want to join more than tables no data displayed
<?php
mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);
$query = "SELECT Product.Product_Name, Product.Price, Product.Image, Gender.Description, Age.Description, Status.Availability from Product join Age on Age.Age_ID join Gender on Gender.Gender_ID join Status on Status.ID";
$result = mysql_query($query);
//Create an array
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['Product_Name'] = $row['Product_Name'];
$row_array['Price'] = $row['Price'];
$row_array['Image'] = base64_encode($row["Image"]);
$row_array['Description'] = $row['Description'];
$row_array['Description'] = $row['Description'];
$row_array['Availability'] = $row['Availability'];
//push the values in the array
array_push($json_response,$row_array);
}
echo json_encode($json_response);
//Close the database connection
fclose($db)
?>
Your last join on Status.ID is the issue. Status table doesn't have ID column. Based on you diagram, you have Status.Status_ID (you don't have Status.ID) Also, your data have to have related data where each table has values in common, otherwise, you will get empty results
Your Diagram:
Change your Query
SELECT
Product.Product_Name,
Product.Price,
Product.Image,
Gender.Description,
Age.Description,
Status.Availability
FROM
Product
JOIN
Age ON Age.Age_ID
JOIN
Gender ON Gender.Gender_ID
JOIN
Status ON Status.ID
to
SELECT
Product.Product_Name,
Product.Price,
Product.Image,
Gender.Description,
Age.Description,
`Status`.Availability
FROM
Product
JOIN
Age ON Product.Age_ID = Age.Age_ID
JOIN
Gender ON Product.Gender_ID = Gender.Gender_ID
JOIN
`Status` ON Product.Status_ID = `Status`.Status_ID
Related
I apply LEFT JOIN and RIGHT JOIN query to get output from both table..
There is two table :
1st table for Product_list.. This table contain category_id which is foreign key and all other product detail..
2nd table for product_images.. This table contain product_id which is foreign key and product images separately..
I want product detail and product images for particular category_id..
I use this Query but not work properly :
SELECT *
FROM product_list
LEFT JOIN product_images ON product_list.pro_id = product_images.pro_id
WHERE product_list.cat_id = '.$id.'
UNION
SELECT *
FROM product_list
RIGHT JOIN product_images ON product_list.pro_id = product_images.pro_id
I get this output:
API Code
<?php
error_reporting(0);
$response = array();
require_once __DIR__ . '/db_Connect.php';
// check for post data
if (isset($_GET["cat_id"])) {
$id = $_GET['cat_id'];
// get a product from products table
//$q="SELECT * FROM product_list WHERE cat_id ='".$id."' ORDER BY pro_id DESC ";
$q="SELECT *
FROM product_list
LEFT JOIN product_images ON product_list.pro_id = product_images.pro_id
WHERE product_list.cat_id = '.$id.'
UNION
SELECT *
FROM product_list
RIGHT JOIN product_images ON product_list.pro_id = product_images.pro_id WHERE product_list.cat_id = '.$id.'";
//print($q);
$res = mysql_query($q);
print($res);
//exit();
if (mysql_num_rows($res) > 0) {
$responce["category_sub_list"]=array();
// check for empty result
while($result = mysql_fetch_array($res)) {
$product=array();
$product['pro_id']=$result['pro_id'];
$product['cat_id']=$result['cat_id'];
$product['product_name']=$result['product_name'];
$product['image']="http://friendzfashionz.com/pandora/admin/Sub_uploads/".$result['image'];
$product['product_desc']=$result['product_desc'];
array_push($responce["category_sub_list"],$product);
}
$responce["success"]=1;
echo json_encode($responce);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No user found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
Please try this query. You need to apply where condition in both queries.
SELECT *
FROM product_list
LEFT JOIN product_images ON product_list.pro_id = product_images.pro_id
WHERE product_list.cat_id = '.$id.'
UNION
SELECT *
FROM product_list
RIGHT JOIN product_images ON product_list.pro_id = product_images.pro_id WHERE product_list.cat_id = '.$id.'
I have 4 tables:
clients, pricelist, result and result_details
I want to generate a form where the transaction_id, name, test_name and result appear on it. I have done a select for the name but I can't figure out a select for the test_name and result.
I want something like this [pdf][5]
$result=mysqli_query($con,"SELECT r.transaction_Id,r.client_id,r.result_id,r.test_id,t.test_id,t.result,p.test_id,p.Name,c.Id,c.surName,c.firstName,c.dob,c.Gender FROM result r
JOIN result_details t
on r.test_id = t.test_id
left JOIN pricelist p ON r.test_id = p.test_id
left JOIN clients c ON r.client_id = c.Id")or die(mysqli_error());
$r=mysqli_query($con,"SELECT DISTINCT id,firstName,surName,Gender,dob FROM clients join result WHERE result.client_id=clients.Id")or die(mysqli_error());
while ($row = mysqli_fetch_array ($r)) {
$id=$row['id'];
$clientfirstname=$row['firstName'];
$surname=$row['surName'];
$gender=$row['Gender'];
$dob=$row['dob'];
$t=mysqli_query($con,"SELECT transaction_Id FROM transaction WHERE clientId=$id") or die(mysqli_error());
while ($test = mysqli_fetch_array ($t)) {
$transaction=$test['transaction_Id'];
$m=mysqli_query($con,"SELECT test_id,result_id from result where client_id=$id")or die(mysqli_error());
while ($q = mysqli_fetch_array ($m)) {
$test_id=$q['test_id'];
$result_id=$q['result_id'];
$z=mysqli_query($con,"SELECT Name from pricelist where test_id=$test_id")or die(mysqli_error());
while ($x = mysqli_fetch_array ($z)) {
$name=$x['Name'];
$query=mysqli_query($con,"SELECT result from result_details where result_id=$result_id")or die(mysqli_error());
while ($query1 = mysqli_fetch_array ($query)) {
$result=$query1['result'];
}
}
}
}
}
Just add:
GROUP BY AcctNo, OrderDate, Charge
HAVING COUNT(1) = 1
The GROUP BY groups all rows with the same field1, field2, ... and Charge together, then the HAVING COUNT(1) = 1 shows only the rows where there was just 1 progenitor. I hope it will work
I'm starting to learn php. I'm able to extract the list of CD from the SQL query in [in this table][1] but there's another query which contains the Category which is linked via ID in this table.
How do I get all the columns in the same result set using one query?
<?php
include 'database_conn.php';
$sql = "SELECT id, title, year, price FROM table_cd";
$queryresult = mysqli_query($conn, $sql)
or die (mysqli_error($conn));
while($row = mysqli_fetch_assoc($queryresult)) {
$iid = $row['id'];
$title = $row['title'];
$year = $row['year'];
$price = $row['price'];
echo "<div>
$title
</div>\n";
echo $row['year'];
echo $row['price'];
}
?>
editCDForm.php:
<?php
$code = $_GET['itemCode'];
$sql = "SELECT * FROM table_cd WHERE table_category.ID = $code
JOIN table_category ON (table_category.Desc = table_cd.ID)";
?>
What you're looking for is called a JOIN which allows you to merge the rows from two or more tables into the same result set.
You can modify your query as illustrated below, by using a LEFT JOIN of the nmc_cd table and the nmc_category table on the catID column, as the common primary attribute between them, giving you the desired result set...
$sql = "SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear,
nmc_cd.CDPrice, nmc_category.catDesc
FROM nmc_cd
JOIN nmc_category on nmc_cd.catID = nmc_cd.catID";
Here's a nice article that may help you visualize what a JOIN looks like in your SQL.
Syntax for joins:
SELECT * FROM table1
JOIN table2 ON (table2.colunmname = table1.columnname)
use can get the data from both tables when joining them:
$sql = "SELECT FT.CDID, FT.CDTitle, FT.CDYear, FT.CDPrice,
ST.catDesc FROM nmc_cd AS FT LEFT JOIN nmc_category as ST ON
FT.catID=ST.catID";
How can I achieve the following using only one database query?
$query = "SELECT `email`, `name` FROM `customers`";
$result = mysqli_query($link,$customer_query);
while($row = mysqli_fetch_array($result)){
echo "<p>".$row['name']."</p>";
echo "<ul>";
$query2 = "SELECT `sku` FROM `orders` WHERE `email` = '".$row['email']."'";
$result2 = mysqli_query($link,$query2);
while($row2 = mysqli_fetch_array($result2)){
echo "<li>".$row2['sku']."</li>";
echo "<li>".$row2['cost']."</li>";
}
echo "</ul>"
}
Use INNER JOIN on email field for this:
SELECT c.name, o.sku, o.cost FROM customers c INNER JOIN orders o ON o.email = c.email
Don't forget to add MySQL INDEX on email field for performance.
But this only select customers which have orders.
If you need to select all customers (even which does not have orders) use LEFT JOIN instead. In this case sku and cost fields for users without orders will be null.
Use this picture as hint for this.
I have managed to create a JOIN query for three tables and can successfully echo out the results in a echoed table, here is my code:
<?php
$sql="SELECT a.product_id, a.Options_id, b.product_name, b.product_price, c.Options_name, c.Price_diff
FROM ProductOptions a
JOIN Products b ON a.product_id = b.product_id
JOIN Options c ON a.Options_id = c.Options_id
ORDER BY product_name DESC";
$result = mysql_query($sql);
if (!$result)
{
echo "An error occurred ".mysql_error();
exit;
}
echo "<table border=1>\n<tr><th></th><th bgcolor=\"#DFE8EC\">Name</th><th>Flavors & Size</th><th bgcolor=\"#DFE8EC\">Price</th><th>Price Difference</th><th bgcolor=\"#DFE8EC\"></th></tr>\n";
while ($line = mysql_fetch_array($result)) {
$name = $line["product_name"];
$price = $line["product_price"];
$options=$line["Options_name"];
$difference=$line["Price_diff"];
echo "<tr><td></td><td bgcolor=\"#DFE8EC\">$name</td><td>$options</td> <td bgcolor=\"#DFE8EC\">£$price</td><td>£$difference</td><td bgcolor=\"#DFE8EC\"></td></tr>\n";
}
echo "</table>\n";
?>
My table works but it shows duplicate entries for product_name and I do not know how to remove them.
You have to use a GROUP BY clause in your query like this:
$sql = "SELECT a.product_id, a.Options_id, b.product_name, b.product_price, c.Options_name, c.Price_diff
FROM ProductOptions a
JOIN Products b ON a.product_id = b.product_id
JOIN Options c ON a.Options_id = c.Options_id
GROUP BY product_name
ORDER BY product_name DESC";