I have MySQL join query and I want bind it with php variable. How can I do that?
I have tried to bind it by calling the variable of php and place it in MySQL query but I'm getting an error
$query = "SELECT p.temp,p.date,"pp."".$selectedSensorOption.",pp.date FROM `p1` p inner join p2 pp on p.date = pp.date and date(p.date) between '$dateFrom2' and '$dateTo2' and date(pp.date) between '$dateFrom2' and '$dateTo2'";
$result = $db_handle->runQuery($query);
if (! empty($result)) {
foreach ($result as $key => $value) {
?>
<tbody id="table1">
<tr>
<td><?php echo $result[$key][0]; ?></td>
<td><?php echo $result[$key][1]; ?></td>
<td><?php echo $result[$key][2]; ?></td>
<td><?php echo $result[$key][3]; ?></td>
<td><?php echo $result[$key][4]; ?></td>
</tr>
<?php
since its join table I want to bind php variable of MySQL to php variable
Seems like your query is wrong : please try below
$query = "SELECT p.temp,p.date,pp.".$selectedSensorOption.",pp.date FROM p1 p inner join p2 pp on p.date = pp.date and date(p.date) between '$dateFrom2' and '$dateTo2' and date(pp.date) between '$dateFrom2' and '$dateTo2'";
$result = $db_handle->runQuery($query);
Related
I have 2 tables tbl_category and tbl_food.
I sorted the items from tbl_food to tbl_category by adding category_id to tbl_food, where category_id in tbl_food is identical to id in tbl_category.
But how do I take title from tbl_category and display it in a table that represents items from tbl_food?
<?php
//query to get all admin from dtb
$sql = "SELECT * FROM tbl_food";
//Exectue the query
$res = mysqli_query($conn, $sql);
//check if executed
if($res==TRUE)
{
//count rows to check wether we have data
$count = mysqli_num_rows($res); //get all rows in dtb
$sn=1; //create a variable and assign the value
//check numb of rows
if($count>0)
{
//there is data
while($rows=mysqli_fetch_assoc($res))
{
//using while loop to get data from dtb
//get individual data
$id=$rows['id'];
$title=$rows['title'];
$description=$rows['description'];
$price=$rows['price'];
$active=$rows['active'];
$category_id=$rows['category_id'];
//display values in table
?>
<tr>
<td><?php echo $sn++; ?></td>
<td><?php echo $title; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $price; ?> kn</td>
<td><?php echo $active; ?></td>
<td><?php echo $category_id; ?></td>
</tr>
<?php
}}
else{
?>
<tr colspan="4">
<td class="error">No categories added</td>
</tr>
<?php
}
}
?>
I think you need to edit your query by adding JOIN to join tbl_category to tbl_food via category_id, at first, like this:
$sql = "SELECT food.*, category.title FROM tbl_food AS food INNER JOIN tbl_category AS category ON category.id=food.category_id";
Secondly it seems you have mistake in iterating query results. mysqli_fetch_assoc($res) returns an associated array, so it must be like this:
$rows = mysqli_fetch_assoc($res)
foreach($rows as $row){
$id=$row['id'];
$title=$row['title'];
$description=$row['description'];
$price=$row['price'];
$active=$row['active'];
$category_id=$row['category_id'];
//Doing stuff
}
You have to method for get title from tbl_category
1- JOIN Method: it is already mentioned above
2- Condition Method:
$sql = "SELECT tbl_food.*, tbl_category.title FROM tbl_food,tbl_category WHERE tbl_category.id=tbl_food.category_id";
foreach($sql as $row){
$id=$row['id'];
$title=$row['title'];
$description=$row['description'];
$price=$row['price'];
$active=$row['active'];
$category_id=$row['category_id'];
}
I'm working on a project where I have two tables 1 is the Users table and another one in the Department table. here I have given a foreign key connection to the department table to the user's table and checked that is working fine. (getting data of particular foreign key). now my requirement is in a single table I want to display all users along with that the foreign key value also. here.
require_once "database.php";
$result = "SELECT * FROM Users";
// , Department WHERE Department.Department_ID = Users.Dpt_id
$output = mysqli_query($conn, $result);
<?php
while ($single = $output->fetch_assoc()):?>
<tr>
<td><?php echo $single['firstname']; ?></td>
<td><?php echo $single['lastname']; ?></td>
<td><?php echo $single['email']; ?></td>
<td><?php echo $single['phnumber']; ?></td>
<td><?php echo $single['provider']; ?></td>
<td><?php echo $single['location']; ?></td>
<td><?php echo $single['Dpt_id']; ?>
</td>
<td><?php if ($single['Dadmin'] == 1){
echo '<p>Department admin</p>';
}
elseif($single['Superuser'] == 1){
echo '<p>SUPER</p>';
}
else{
echo '<p>USER</p>';
}
?></td>
<?php endwhile ?>
tried code:
$result = "SELECT * FROM Users, Department WHERE
Department.Department_ID = Users.Dpt_id";
$output = mysqli_query($conn, $result);
so here is my code. please help me to get the value from the foreign key.
I think you want this result but i don't know your column name:
SELECT u.*, d.department_name
FROM USERS u
LEFT JOIN Department d
ON u.dpt_id = d.department_id
Try this query:
SELECT u.*, d.department_id
FROM USERS u
INNER JOIN Department d
ON u.dpt_id = d.department_id
I need to run two statements to return the data I need. The below code will return all needed except the count column which can be retrieved from a different table.
How can I run these two statements in the same code to retrieve the count as well?
Here is the code I have:
<?php
$query = "SELECT * from $CLIENTS_DATABASE order by id DESC";
if ($result = mysqli_query($conn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?> </td>
<td><?php echo $row["url"]; ?> </td>
<td><?php echo $row["secret"]; ?> </td>
<td><?php echo $row["count"]; ?> </td>
</tr>
<?php
}
mysqli_free_result($result);
}
?>
The other statement that the count data I need is this:
$query = "SELECT COUNT(*) as count from visits where id_client='$result[id]'"
Thanks for your time and any help you can provide.
Join the two queries.
$query = "SELECT order.*, IFNULL(c.count, 0) AS count
FROM $CLIENTS_DATABASE AS order
LEFT JOIN (SELECT id_client, COUNT(*) AS count
FROM visits
GROUP BY id_client) AS c
ON c.id_client = order.id
ORDER BY order.id DESC";
<?php
$db = db_connect();
$SQLSELECT = "SELECT * FROM `order` INNER JOIN tb ON order.pc = tb.pc";
$result_set = mysqli_query($db, $SQLSELECT);
foreach($result_set as $row) {
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['add1']; ?></td>
<td><?php echo $row['add2']; ?></td>
<td><?php echo $row['prov']; ?></td>
<td><?php echo $row['pc']; ?></td>
<td><?php echo $row['tier']; ?></td>
</tr>
<?php
I have this code and it gets data from the database by comparing the 2 tables. So for example the one in order table is 5,6,2,1,4,3. So when the query compares the 2 tables it checks for 5 then 6 then 2 and so on. When the results come out, the results become sorted and the output becomes 1,2,3,4,5,6 but I want to output it in the order how I input it. It is somehow auto sorting. Is it possible to disable that?
Unless you explicitly specify a sort order with the user of ORDER BY. Mysql and other databases does not provide any guarantee about the order in what the data is returned.
If you have a table that hasn't had many deletes and updates, the order is likely the order that you inserted. But no guarantee. So use ORDER BY
i'm trying to get the sum of values in a column inside php but it's not working at all while the query works fine when testing in MySQL here's the code (updated with full code)
require('../_req/base.php');
$getCartQ = "select * from user_product inner join cart inner join products inner join users on user_product.User_ID = cart.User_ID and user_product.User_ID = users.User_ID group by cart.User_ID";
$getCartR = mysql_query($getCartQ);
?> <table align="center" width="1271" border="0" cellpadding="0" cellspacing="0">
<?php
while($cartRow = mysql_fetch_array($getCartR)){
$sumQ = "select SUM(Total) as total from user_product where Status = 'active' and user_product.User_ID = '$cartRow[User_ID]'";
$sumR = mysql_query($sumQ) or die(mysql_error());
$sumRow = mysql_fetch_assoc($sumR);
$cost = $sumRow['total'];
?>
<tr>
<td><?php echo $cartRow['Full_Name'] ?></td>
<td><?php echo $cartRow['State']; ?></td>
<td><?php echo $cost; ?></td>
</tr>
<?php
}
?>
</table>
<?php
mysql_close($connect);
when i try to echo $cost i get nothing just a blank space what's wrong with that code ?
user_product.User_ID = '$getCartR[User_ID]'";
to
user_product.User_ID = '{$getCartR['User_ID']}'";
do
echo mysql_error();
at the end and tell me what it's showing. You'll have your answer right there
I just deleted the cache and everything worked fine i don't know what this has to do with the cache thanks all for your help i appreciate it