I know that this question has been asked before but I am finding it difficult to apply the principle to sort out my issue. I have 2 tables that I want to select data from.
I have tried using the inner join sql query below:
$query1 = "SELECT `entries.id`, `code_id`, FROM `entries` INNER JOIN `codes` ON `code_id` = `codes.id`";
$result = mysql_query($query1);
And My display is as follows:
while($row = mysql_fetch_array( $result ))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['msisdn'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "<td>" . $row['code_id'] . "</td>";
echo "</tr>";
}
I am trying to get the actual code from the table codes, and display it to a website, but I keep getting the id(number) instead of actual code.
Can someone please assist. I have checked around the internet and even got some ebooks, but I am still a newbie and would like someone to guide me through the process. All input appreciated.
You have to assign the fields a new name if there are fields with the same name in different tables like "id":
$query1 = "SELECT `entries.id` as entriesid, entries.name, entries.msisdn, entries.created, `codes.id` as codesid, FROM `entries` INNER JOIN `codes` ON `entries.id` = `codes.id`";
$result = mysql_query($query1);
...
echo "<td>" . $row['entriesid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['msisdn'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "<td>" . $row['codesid'] . "</td>";
You can do the following:
$sql = mysql_query("select entries.* , codes.* from entries,codes where entries.code_id=codes.id");
while($fetch = mysql_fetch_array($sql)) {
// Do Your Display
}
Hope this helps . Also mysql functions are degrading. So change that to mysqli / PDO
Related
I try to combine a SELECT and an AVG query into one. My query is working but the AVG result is not shown as expected. Error is:
Undefined index
I thought I can combine AVG and SELECT like this?
the result will be a list and should also show an average progress value. If I put this AVG query separately and inside the "fetch" it works but shows only the first result. So it is not a solution for me because I will have much more rows.
I hope someone can help me to rebuild this miracle :)
<?php
$statement = $pdo->prepare("
SELECT
audit.id as audit_id,
audit.uid,
audit.assigned_auditor,
audit.audit_req_comment,
audit.audit_req_date,
audit.audit_date_start,
audit.general_audit_status,
audit.audit_request_date,
audit.audit_type,
audit.audit_date_start,
questionaire.quest_name,
users.nachname,
suppliers.supplier_name,
suppliers.supplier_city,
suppliers.supplier_country,
(SELECT AVG(progress) AS progress FROM answers WHERE relevant = '1' AND audit_id = :audit_id AND rating != required_answer)
FROM audit
JOIN users ON audit.uid = users.id
JOIN suppliers ON audit.supplier_id = suppliers.id
JOIN questionaire ON audit.questionaire_id = questionaire.id
WHERE
audit.cid = :cid
AND audit.general_audit_status = 'Maßnahmenplan'
AND audit.assigned_auditor = :assigned_auditor"
);
$result = $statement->execute(array(':cid' => $cid, ':assigned_auditor' => $user['id'], ':audit_id' => 4));
$count = 1;
while ($row = $statement->fetch()) {
// Datum umwandeln
$original_date = $row['audit_date_start'];
// Creating timestamp from given date
$timestamp = strtotime($original_date);
// Creating new date format from that timestamp
$new_date = date("d.m.Y", $timestamp);
// Audit Typ Namensgebung
if ($row['audit_type'] == "AR") {
$audit_type = "Externes Audit";
}
if ($row['audit_type'] == "RA") {
$audit_type = "Remote Audit";
}
if ($row['audit_type'] == "IA") {
$audit_type = "Internes Audit";
}
if ($row['audit_type'] == "SAA") {
$audit_type = "Self Assessment Audit";
}
//Berechne die durchschnittliche Abarbeitung aller Maßnahmen dieses Audits
//$statement = $pdo->prepare("SELECT AVG(progress) AS progress FROM answers WHERE relevant = '1' AND audit_id = :audit_id AND rating != required_answer");
//$statement->execute(array(':audit_id' => $row['audit_id']));
//$total_progress = $statement->fetch();
echo "<tr>";
echo "<td>" . $row['audit_id'] . "</td>";
echo "<td>" . $new_date . "</td>";
echo "<td>" . $row['supplier_name'] . "</td>";
echo "<td>" . $row['supplier_city'] . " (" . $row['supplier_country'] . ")</td>";
echo "<td>" . $row['quest_name'] . "</td>";
echo "<td>" . $audit_type . "</td>";
echo "<td>" . round($row['progress'], 0) . " %</td>";
echo '<td align="center"><i class="fas fa-edit"></i></td>';
echo "</tr>";
}
?>
As I understand your question, you just need to alias the results of the subquery in the outer query (rather than in the inner query itself):
SELECT
audit.id as audit_id,
audit.uid,
audit.assigned_auditor,
audit.audit_req_comment,
audit.audit_req_date,
audit.audit_date_start,
audit.general_audit_status,
audit.audit_request_date,
audit.audit_type,
audit.audit_date_start,
questionaire.quest_name,
users.nachname,
suppliers.supplier_name,
suppliers.supplier_city,
suppliers.supplier_country,
(
SELECT AVG(progress)
FROM answers
WHERE relevant = 1 AND audit_id = :audit_id AND rating != required_answer
) AS progress --> here
FROM audit
JOIN users ON audit.uid = users.id
JOIN suppliers ON audit.supplier_id = suppliers.id
JOIN questionaire ON audit.questionaire_id = questionaire.id
WHERE
audit.cid = :cid
AND audit.general_audit_status = 'Maßnahmenplan'
AND audit.assigned_auditor = :assigned_auditor"
How do I get Direct access through the database query to product name , product price ,permalink , and image url of product in WordPress WooCommerce ?!
what's the sql query ?
global $wpdb;
$metas = array(
'_thumbnail_id', '_regular_price'
);
foreach ($metas as $i=>$meta_key) {
$meta_fields[] = 'm' . $i . '.meta_value as ' . $meta_key;
$meta_joins[] = ' left join ' . $wpdb->postmeta . ' as m' . $i . ' on m' . $i . '.post_id=' . $wpdb->posts . '.ID and m' . $i . '.meta_key="' . $meta_key . '"';
}
$request = "SELECT ID, post_title, " . join(',', $meta_fields) . " FROM $wpdb->posts ";
$request .= join(' ', $meta_joins);
$request .= " WHERE post_status='publish' AND post_type='product' AND $wpdb->posts.post_title like '%sample%'";
$product_details = $wpdb->get_row($request, OBJECT);
if(!empty($product_details)){
$qry = "SELECT guid FROM $wpdb->posts WHERE ID=" . $product_details->_thumbnail_id;
$uri_obj = $wpdb->get_row($qry, OBJECT);
$product_details->_thumbnail_id = $uri_obj->guid;
}
echo '<pre>';print_r($product_details);exit;
I have the script I use that displays things from a table in my database, but it keeps giving me this error:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\raynerblogger1\posts.php on line 21
someone please help
$sql = "SELECT * FROM blogs;";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<div id='message'>";
echo $row['author']; echo(':') . "<br>";
echo $row['post'] . "</div>" . "<br>" . "<br>" . "<br>";
}
Try this, I edited line 1 and 6:
$sql = "SELECT * FROM blogs";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<div id='message'>";
echo $row['author'].":". "<br>";
echo $row['post'] . "</div>" . "<br>" . "<br>" . "<br>";
}
I have this script that gets weather data from a API.
But I need to have the temperatures of every day in seperate strings.
$day1Max, $day1Min, $day2Max, $day2Min, etc...
Is it possible to get these variables from a foreach loop?
<?php
$conditions_week = $forecast->getForecastWeek($latitude, $longitude);
echo "Forecast this week:\n";
foreach($conditions_week as $conditions) {
?>
<tr>
<th><?php echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMaxTemperature() . "\n"; ?></th>
<th><?php echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMinTemperature() . "\n"; ?></th>
</tr>
<?php
}
?>
This is the output:
Here is your solution :
<?php
class Conditions{
function __construct($min,$max){
$this->max = $max;
$this->min = $min;
}
function getMaxTemperature(){
return $this->max;
}
function getMinTemperature(){
return $this->min;
}
}
$conditions_week = array(new Conditions(5,10),new Conditions(-5,3));
echo "Forecast this week:\n";
foreach($conditions_week as $k=>$conditions) {
$varMin = "day".($k+1)."min";
$varMax = "day".($k+1)."max";
$$varMin = $conditions->getMinTemperature();
$$varMax = $conditions->getMaxTemperature();
?>
<tr>
<th><?php echo "Max : ". $conditions->getMaxTemperature() . "\n"; ?></th>
<th><?php echo "Min : ". $conditions->getMinTemperature() . "\n"; ?></th>
</tr>
<?php
}
echo "day1min : " . $day1min;
echo '<br>';
echo "day1max : " . $day1max;
echo "<hr>";
echo "day2min : " . $day2min;
echo '<br>';
echo "day2max : " . $day2max;
You to generate your php variables $day1min, $day1max... inside the foreach and then you can use it outside the loop.
Here's the full example : https://3v4l.org/vX5bW
I have the below code where I am trying to get a list of orders.
So from the wcomm (woocommerce_order_itemmeta) table, I only want to display the meta_value where the meta_key is equal to _qty or _product_id and I need the _product_id to be filtered on a single value like I have. I can't seem to make it only do both things.. My output never shows the _qty value because I'm specifically filtering the value of _product_id but I don't understand what to change
global $wpdb;
$wpdb->woocommerce_order_items = $wpdb->prefix . 'woocommerce_order_items';
$wpdb->woocommerce_order_itemmeta = $wpdb->prefix . 'woocommerce_order_itemmeta';
$uvuorderposts = $wpdb->get_results(
"
SELECT p.ID, p.post_title, p.post_date, wcom.order_item_name, wcomm.meta_key, wcomm.meta_value
FROM $wpdb->posts AS p
INNER JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id
INNER JOIN $wpdb->woocommerce_order_items AS wcom ON p.ID = wcom.order_id
INNER JOIN $wpdb->woocommerce_order_itemmeta AS wcomm ON wcom.order_item_id = wcomm.order_item_id
WHERE (p.post_type = 'shop_order')
AND (pm.meta_key = '_wc_authorize_net_aim_charge_captured')
AND (pm.meta_value = 'yes')
AND (wcomm.meta_key = '_product_id')
OR (wcomm.meta_key = '_qty')
AND (wcomm.meta_value = '2193')
ORDER BY p.post_date
"
);
echo "<table>";
foreach ( $uvuorderposts as $uvuorderpost )
{
echo "<tr><td>" . $uvuorderpost->post_title . " - " . $uvuorderpost->ID . "</td><td>" . $uvuorderpost->post_date . "</td><td>" . $uvuorderpost->meta_key . ": " . $uvuorderpost->meta_value . "</td></tr>";
echo "<tr bgcolor='#eaeaea'><td colspan='3'>" . $uvuorderpost->order_item_name . "</td></tr>";
}
echo "</table>";
I tried to "filter" it on the output by using $uvuorderpost->meta_key['_qty'] but it did not work!
I fixed it with the following changes. Just joined that table twice and it works now. Not sure if it's best practice but yeah
global $wpdb;
$wpdb->woocommerce_order_items = $wpdb->prefix . 'woocommerce_order_items';
$wpdb->woocommerce_order_itemmeta = $wpdb->prefix . 'woocommerce_order_itemmeta';
$uvuorderposts = $wpdb->get_results(
"
SELECT p.ID, p.post_title, p.post_date, wcom.order_item_name, wcomm.meta_key, wcomm.meta_value, wcomm2.meta_key AS meta_key_qty, wcomm2.meta_value AS meta_value_qty
FROM $wpdb->posts AS p
INNER JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id
INNER JOIN $wpdb->woocommerce_order_items AS wcom ON p.ID = wcom.order_id
INNER JOIN $wpdb->woocommerce_order_itemmeta AS wcomm ON wcom.order_item_id = wcomm.order_item_id
INNER JOIN $wpdb->woocommerce_order_itemmeta AS wcomm2 ON wcom.order_item_id = wcomm2.order_item_id
WHERE (p.post_type = 'shop_order')
AND (pm.meta_key = '_wc_authorize_net_aim_charge_captured')
AND (pm.meta_value = 'yes')
AND (wcomm.meta_key = '_product_id')
AND (wcomm.meta_value = '2193')
AND (wcomm2.meta_key = '_qty')
ORDER BY p.post_date
"
);
echo "<table>";
foreach ( $uvuorderposts as $uvuorderpost )
{
echo "<tr><td>" . $uvuorderpost->post_title . " - " . $uvuorderpost->ID . "</td><td>" . $uvuorderpost->post_date . "</td></tr>";
echo "<tr bgcolor='#eaeaea'><td colspan='2'>" . $uvuorderpost->order_item_name . "</td></tr>";
echo "<tr bgcolor='#eaeaea'><td colspan='2'>" . $uvuorderpost->meta_key . " : " . $uvuorderpost->meta_value . "</td></tr>";
echo "<tr bgcolor='#eaeaea'><td colspan='2'>" . $uvuorderpost->meta_key_qty . " : " . $uvuorderpost->meta_value_qty . "</td></tr>";
}
echo "</table>";