I have created a database and and its tables. Insertion has done successfully. Now I want to get data from two tables using left join.
The problem is some fields which are not matching are empty. I want to remove the empty fields and to show the matching fields data.
How can I do this?
My code is:
<?php
include 'connection.php';
//joining of two table column
$sql = "select * from oc4e_product left join item ON item.Description = oc4e_product.model";
$result = mysqli_query($conn, $sql);
echo "<table border = 1px>";
echo "<tr>
<th>S/No</th>
<th>Web Name</th><th>POS Name</th> <th>POS Name</th></tr>";
$i=1;
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $i . "</td>
<td>".$row['model'] ."</td>
<td>". $row['Description'] . "</td>
</tr>";
$i++;
}
}
echo "</table>";
?>
Maybe you need to modify your sql query:
//joining of two table column
$sql = "select * from oc4e_product left join item ON item.Description = oc4e_product.model where item.Description <> ''";
In this query i added filter not empty record item.Description <> ''
Related
Hi in the below i want to show Consultation Charges values for that i took the td.But in that td not displying anything even td also not showing.
After executing this query i want to find the no. of rows based on the rows i want to display the data.
My expected output:
Bill Particular Bill Sub Particular Doctor Date Dis. Amt.
Consultation Charges:
all the values based on no of rows.
php
<table width="100%">
<th>Bill Particular</th>
<th>Bill Sub Particular</th>
<th>Doctor</th>
<th>Date</th>
<th>Dis. Amt.</th>
<th>Charge</th>
<th>No. of Times</th>
<th>Amount</th>
</table>
<tr><th colspan=2>Consultation Charges:</th>
<?php
$div_options = array();
$sql = "SELECT ibp.ipd_bp_id, ibp.bp_id, bp.bp_name, ibp.bsp_id, bsp.bsp_name, ibp.doctor_id, ab.employee_name doctor, ibp.date date, ibp.amount charge, ibp.discount_amount discount, ibp.no_of_time, (ibp.no_of_time * ibp.amount) total_amount
FROM bill_particular_master bp
INNER JOIN ipd_bill_particular ibp ON ibp.bp_id = bp.bp_id
LEFT OUTER JOIN bill_sub_particular bsp ON bsp.bsp_id = ibp.bsp_id
LEFT OUTER JOIN address_book ab ON ab.employee_id = ibp.doctor_id
WHERE ibp.ipd_reg_no = '$ipd_no'
AND bsp.consultant =1
AND bsp.package = 0
AND bsp.admission = 0
AND bp.bp_name != 'Scan Charges'
AND bp.bp_name !='Procedure'";
$sth = $dbh->query($sql);
//$row=$dbh->fetch();
$i=1;
while($row=$sth->fetch(PDO::FETCH_ASSOC)){
$sub_arr['bp_name'] = $row['bp_name'];
$sub_arr['bsp_name'] = $row['bsp_name'];
echo "<tr>
<td>Here is the text - " . $sub_arr['bp_name'] . "</td>
<td>The ID of the text is - " .$sub_arr['bsp_name'] . "</td>";
if($i !== 0) {
echo "<td>The ID of the previous entry is - " .$sub_arr['bp_name'] . "</td>";
}
else {
echo "<td> </td>";
}
echo "</tr>";
$i = $row['bp_name'];
}
?>
</tr>
echo "<td>The ID of the previous entry is - " . $row['bp_name'] . "</td>";
You have there variable thats not even defined in your code.
I am trying to get all values shown in a table from mysql but getting one .
I want to get the rows of the mysql table at in the last table mentioned in the code
////////Here is a desc of no use --- blah for just posting this question / as i am getting an error msg for giving more details information about this question /////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is the code:
$sql = 'SELECT
item_added
FROM
products_added
ORDER BY id';
$results = mysqli_query($conn, $sql);
if(mysqli_num_rows($results) < 1){
echo "No items";
}else{
$new_sql = 'SELECT
item_added,
quantity,
amount,
sum(amount) as items_total
FROM
products_added
where `username` = "'.mysqli_real_escape_string($conn, $_SERVER["REMOTE_ADDR"]).'"
ORDER BY id';
$resu = mysqli_query($conn, $new_sql);
}
?>
<table>
<thead>
<tr>
<td>Item</td>
<td>Qyt</td>
<td>Price</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($resu)){
echo "<tr>";
echo "<td>" . $row['item_added'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td><a class=\"remove-from-cart\" href=\"\"><i class=\"fa fa-times\"></i></a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
It looks like its because you're using an aggregate function SUM() without a GROUP BY. In the $new_sql query, try adding "GROUP BY item_added" right before "ORDER BY id".
I want to count all user records and display them in tables, I am trying this code code, It displays the record for one user only, I want to display records from all users.
$u=$_POST['userid'];
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name where user_id=$u");
echo "<table border='1'>
</tr>
<tr>
<th>User ID</th>
<th>count</th>
</tr>";
while($row = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $u . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Try the following SQL Query:
SELECT `user_id`, COUNT(`user_id`) as `total` FROM `table-name` GROUP BY `user_id`;
Refer to the documentation of the GROUP BY clause.
Use below:
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name");
where clause use for filter the data.
refer http://www.w3schools.com/sql/sql_where.asp
i have a table being echo'd from a single query to a table in our database and i get it to echo out the following table;
http://www.skulldogs.com/dev/testview.php
i want it to sort the "yellow" rows under the correct green rows where the "mainToon" name matches for example:
high voltege
--REAL MCCOY
--Cpt Hook
riazall
-- Valeside
my code to echo the above page is;
<?php
$result = mysql_query("SELECT * FROM `members`");
echo "<table border='1'>
<tr>
<th>Character ID</th>
<th>Name</th>
<th>MainToon</th>
<th>toonCategory</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
$characterID = $row['characterID'];
$name = $row['name'];
$startDateTime = $row['startDateTime'];
$logonDateTime = $row['logonDateTime'];
$logoffDateTime = $row['logoffDateTime'];
$location = $row['location'];
$role = $row['role'];
$vouchedBy = $row['vouchedBy'];
$positionHeld = $row['positionHeld'];
$remarks = $row['remarks'];
$afkNotice = $row['afkNotice'];
$toonCategory = $row['toonCategory'];
$mainToon = $row['mainToon'];
$watch = $row['watch'];
if ($toonCategory == 'Main Toon') {
echo "<tr bgcolor='#00FF00'>"; }
else {
echo "<tr bgcolor='#FFFF00'>"; }
echo "<td>" . $characterID . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $mainToon . "</td>";
echo "<td>" . $toonCategory . "</td>";
echo "</tr>";
}
echo "</table>";
?>
at the moment i am not echo the other data until i can figure out how to display this table accordingly. can it be done this way?
this is how i want to display the table;
http://www.skulldogs.com/dev/mockup.php
Add an ORDER BY clause to your sql:
SELECT * FROM `members` ORDER BY toonCategory;
If there are other values above and below "Main Toon", You can order by a boolean:
SELECT * FROM `members` ORDER BY toonCategory = 'Main Toon' DESC;
EDIT:
Now I see what you are after as you have put up the example, try:
SELECT * FROM `members` ORDER BY CONCAT(MainToon, Name);
if the blank spaces are empty strings or:
SELECT * FROM `members` ORDER BY COALESCE(MainToon, Name) DESC, Name;
if the blank rows are null.
Try SELECT * FROM members ORDER BY toonCategory;
I am trying to take a user's input of #,#, or just normal statements and then pull those results from a SQL database if the search results exist in the "query" column of a table called "job" in my database. Here is the error I am getting:
Unknown column 'Array' in 'where clause'
Here's my code:
$result_all = $_POST['result_name'];
$result_all_array= explode(',',$result_all);
$query = "select last_count, query, job_id from twitterinblack46.job where
query in (".$result_all_array.") order by last_count desc;";
$result= mysql_query($query);
if($result === FALSE) {
die(mysql_error());
}
else{
//Sets up table
echo "<table border='1'
<tr>
<th>Job ID</th>
<th>Last Count</th>
<th>Result</th>
</tr>";
//Populates table
while($row = mysql_fetch_array($result)){
echo"<tr>";
echo "<td>" . $row["job_id"] . "</td>";
echo "<td>" . ltrim($row["last_count"],'0') . "</td>";
echo "<td>" . str_replace(array('%23', '%40', '%20', 'q='), array('#','#',' ',''),
$row['query']) . "</td>";
echo "<tr>";
}
echo "</table>";
Anyone know what I am doing wrong?
Update:
Here was the confusion, I did not forget to copy the $ for query. "query" is the name of the column I want to get the information from in my query. So what I am saying is, what is the difference between these two sets of code and why doesn't it work for the first query?
Code that doesn't work:
$result_all= $_POST['result_name'];
$query = "select last_count, query, job_id from twitterinblack46.job where
query in (".$result_all.") order by last_count desc;";
$result= mysql_query($query);
Code that does work:
$job_id_all= $_POST['job_id'];
$query = 'select last_count, query, job_id from twitterinblack46.job where job_id in
('.$job_id_all.') order by last_count desc;';
$result= mysql_query($query);
It is obviously a problem of the form input, being multiple checkboxes checked for example.
Try this in both queries:
$result_all= implode(',', mysql_escape_string($_POST['result_name']));
$query = "select last_count, query, job_id from twitterinblack46.job where query in (".$result_all.") order by last_count desc;";
$result= mysql_query($query);