Set variable from alias in SQL query - php

I have a simple JOIN query:
$lstCheck = $dbWHMCS->query('SELECT * FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['ho.id'];
$clientid = $data['cl.id'];
}
My problem is that I have an "id" column in both the tblhosting and tblclients tables, so my variables both have the same id. I tried to set it using an alias in the example above (ho. and cl.) but it doesn't work. How can I do this in the example above?
Thank you!

How about this? (a bit rusty on php details, but this should do it):
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}

You are selecting the records, with a wild card *, so you can't select the fields like that.
As per your query h.hst_serverid & ho.userid have the exact same value as you want. SO simply do this
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hst_serverid'];
$clientid = $data['userid'];
}
However, selecting specific rows might sound better too
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid, ....');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}

Related

PHP SQL i another SQL query

Description:
table1 - columns: f1,f2,f3,f4,f5
in these columns I have rom_number for temperature sensors
table2 - columns: act_temperature, rom_number
I need act_temperature from table2 for each rom_number stored in table1 (cols f1,f2,...)
I have code:
// main loop
$rows = $db->query("SELECT * FROM tabela1 WHERE active='on' ");
$row = $rows->fetchAll();
foreach ($row as $a) {
$f1 = $a['f1'];
$f2 = $a['f2'];
$f3 = $a['f3'];
$f4 = $a['f4'];
$f5 = $a['f5'];
}
How to change the code ?
You have to LEFT JOIN on table 2 for each columns f1,f2,...
$sql = "SELECT t1.*,
t2_1.act_temperature as tmp1,
t2_2.act_temperature as tmp2,
t2_3.act_temperature as tmp3,
t2_4.act_temperature as tmp4,
t2_5.act_temperature as tmp4
FROM tabela1 t1
LEFT JOIN tabela2 t2_1 ON t2_1.rom_number = t1.f1
LEFT JOIN tabela2 t2_2 ON t2_2.rom_number = t1.f2
LEFT JOIN tabela2 t2_3 ON t2_3.rom_number = t1.f3
LEFT JOIN tabela2 t2_4 ON t2_4.rom_number = t1.f4
LEFT JOIN tabela2 t2_5 ON t2_5.rom_number = t1.f5
WHERE active='on' ";
Then, $a should have following keys : f1,f2,...,f5,tmp1,tmp2,...,tmp5.

How can I join 3 tables in SQL

Here is the query I created :
$query = "SELECT address_details.company, address_details.po_box,
address_details.town, letter_details.address_id,
letter_details.attn, letter_details.create_date,
letter_details.ref_no, letter_details.refference,
letter_details.letter_body, letter_details.print_date
FROM letter_details
join address_details ON address_details.id = letter_details.address_id
join signatories ON (letter_details.id = signatories.id)
WHERE letter_details.id='" .$_GET['id'] . "'";
Try using this query.
$query = "SELECT ad.company, ad.po_box,
ad.town, ld.address_id,
ld.attn, ld.create_date,
ld.ref_no, ld.refference,
ld.letter_body, ld.print_date
FROM letter_details ld
left join address_details ad ON (ad.id = ld.address_id)
left join signatories s ON (ld.id = s.id)
WHERE ld.id='" .$_GET['id'] . "'";
First, you should alias for better readibility,
check if you get something in $_GET[id]
echo $_GET[id];
then assign it, (or you may set it as well, if you're using a class)
$fltr = $_GET[id];
And, try to use LEFT JOIN ;
SELECT b.company, b.po_box,
b.town, a.address_id,
a.attn, a.create_date,
a.ref_no, a.refference,
a.letter_body, a.print_date
FROM letter_details AS a LEFT JOIN address_details AS b ON b.id = a.address_id
LEFT JOIN signatories AS c ON (a.id =c.id)
WHERE a.id='$fltr'

Setting PHP Query into a nested array

I am trying to use PHP to logically store all of that data in a nested associative arrays, and then whenever I loop through the data or need to reference the data I would refer to the array pointer rather than doing new queries each time.
For example:
My query is
$query = $db->query("
SELECT
c.id campaign_id,
c.campaign_title,
c.start_date campaign_start_date,
c.end_date campaign_end_date,
cat.id category_id,
cat.category_title,
n.id nominee_id,
n.title nominee_title,
u.id voter_id,
u.fullname voter_name
FROM
campaign c
LEFT JOIN categories cat ON cat.campaign_id = c.id
LEFT JOIN category_nominees cn ON cn.category_id = cat.id
LEFT JOIN nominees n ON n.id = cn.nominee_id
LEFT JOIN category_votes cv ON cv.campaign_id = c.id
AND cv.category_id = cat.id
AND cv.nominee_id = n.id
LEFT JOIN users u on u.id = cv.user_id
WHERE
c.active = 1
ORDER BY
u.fullname,
c.campaign_title,
cat.category_order,
cat.category_title,
cn.nominee_order,
n.title
") or die(mysqli_error());
and im trying to set up the nested array like
$array = array() ;
while($row = mysqli_fetch_assoc($query)) {
$array[$row['campaign_id']] = $row['campaign_id'];
$array[$row['campaign_id']]['campaign_title'] = $row['campaign_title'];
$array[$row['campaign_id']]['campaign_start_date'] = $row['campaign_start_date'];
$array[$row['campaign_id']]['campaign_end_date'] = $row['campaign_end_date'];
$array[$row['campaign_id']]['campaign_title'] = $row['campaign_title'];
$array[$row['campaign_id']]['category_id'] = $row['category_id'];
$array[$row['campaign_id']]['category_id']['category_title'] = $row['category_title'];
}
So whenever I point to:
$array[2][3]['category_title']
It would print the category title
Do you have any ideas? I literally been at it for months and can't figure it out.
Use the following:
while ($row = mysqli_fetch_assoc($query)) {
if (!isset($row['campaign_id'])) {
$array[$row['campaign_id']] = $row;
}
if (!isset($array[$row['campaign_id']]['categories'])) {
$array[$row['campaign_id']]['categories'] = array();
}
$array[$row['campaign_id']]['categories'][$row['category_id']] = array('category_id' => $row['category_id'], 'category_title' => $row['category_title']);
}
}
$row is already an associative array, you don't need to assign each element separately. You only need to deal specially with the category information, which has to be put into a nested associative array that I've called categories. So you would access it as
$array[0]['categories'][1]['category_title']
You can loop over all the categories with:
foreach ($array[$campaign]['categories'] as $cat) {
echo "Category ID: {$cat['category_id']} Title: {$cat['category_title']}<br>";
}

php mysql while loop query optimization

I have php/mysql script running which is like this:
$sql = "select a.column1 from table1 a";
$query = mysql_query($sql);
while ($fec = mysql_fetch_assoc($query)) {
$sub1 = "select column1,column2 from subtable1 where id=" . $fec['a.column1'];
$subquery1 = mysql_query($sub1);
while ($subfec1 = mysql_fetch_assoc($subquery1)) {
//all data .....
}
$sub1 = "select column1,column2 from subtable2 where id=" . $fec['a.column1'];
$subquery2 = mysql_query($sub2);
while ($subfec2 = mysql_fetch_assoc($subquery2)) {
//all data .....
}
$sub2 = "select column1,column2 from subtable3 where id=" . $fec['a.column1'];
$subquery3 = mysql_query($sub3);
while ($subfec3 = mysql_fetch_assoc($subquery3)) {
//all data .....
}
}
Now I've many many many records in table1, subtable1, subtable2 and subtable3. And problem is that it takes around 7 hours to display the results. Moreover the CPU Usage is 100%
Please suggest the optimization tips to overcome this issue.
Use a single query to get all records
SELECT
a.column1,
s.column1,
s.column2,
s2.column1,
s2.column2,
s3.column1,
s3.column2
FROM table1 a
LEFT JOIN subtable1 s ON s.id = a.column1
LEFT JOIN subtable2 s2 ON s.id = a.column1
LEFT JOIN subtable3 s3 ON s.id = a.column1
Here
$sql="
SELECT
a.column1 acolumn1,
s.column1 scolumn1,
s.column2 scolumn2,
s2.column1 s2column1,
s2.column2 s2column2,
s3.column1 s3column1,
s3.column2 s3column2
FROM table1 a
LEFT JOIN subtable1 s ON s.id = a.column1
LEFT JOIN subtable2 s2 ON s.id = a.column1
LEFT JOIN subtable3 s3 ON s.id = a.column1";
$query=mysql_query($sql);
while ($fec=mysql_fetch_assoc($query)) {
// display any info here
}
Use aliases for accessing different table columns

Mysql Join Question

What is incorrect?
I cant get any data...
SELECT
my_site_orders.order_id,
my_site_orders.order_orderer_id,
my_site_orders.order_ip,
my_site_orders.order_time,
my_site_orders.order_campaign_id,
my_site_orders.order_status,
my_site_orders.order_shipment_status,
my_site_orders.order_shipping_method,
my_site_orders.order1_id,
my_site_orders.order1_coupon_id,
my_site_orders.order1_amount,
my_site_orders.order1_price,
my_site_orders.order_cargo_name,
my_site_orders.order_cargo_surname,
my_site_orders.order_cargo_vatnumber,
my_site_orders.order_cargo_line1,
my_site_orders.order_cargo_line2,
my_site_orders.order_cargo_stateprovince,
my_site_orders.order_cargo_district,
my_site_orders.order_cargo_city,
my_site_orders.order_cargo_contactphone,
my_site_orders.order_invoice_name,
my_site_orders.order_invoice_surname,
my_site_orders.order_invoice_company,
my_site_orders.order_invoice_line1,
my_site_orders.order_invoice_line2,
my_site_orders.order_invoice_vatnumber,
my_site_orders.order_invoice_vat,
my_site_orders.order_invoice_stateprovince,
my_site_orders.order_invoice_district,
my_site_orders.order_invoice_city,
my_site_orders.order_invoice_contactphone,
my_site_orders.order_type,
my_site_orders.order_note,
my_site_orders.order_hash,
my_site_orders.order_bank_hash,
my_site_orders.order_phone_number,
my_site_orders.order_price,
my_site_orders.order_cc_name,
my_site_orders.order_instalment_amount,
my_site_orders.order_instalment_price,
my_site_users.user_name,
my_site_users.user_email,
my_site_orders.order1_type,
my_site_users.user_realname,
my_site_users.user_surname,
my_site_products.product_brand_id,
my_site_products.product_id,
my_site_products.product_cat_id,
my_site_products.product_title,
my_site_products.product_snippet,
my_site_products.product_description,
my_site_products.product_code,
my_site_products.product_normalprice,
my_site_products.product_price,
my_site_products.product_quantity,
my_site_products.product_image1,
my_site_products.product_image1_t1,
my_site_products.product_image4,
my_site_products.product_image4_t1,
my_site_products.product_image4_t2,
my_site_products.product_image_big4,
my_site_products.product_image5,
my_site_products.product_image5_t1,
my_site_products.product_image5_t2,
my_site_products.product_image_big5,
my_site_products.product_select_choice_title,
my_site_products.product_select_option_title,
my_site_products.product_select1,
my_site_products.product_select2,
my_site_products.product_select3,
my_site_products.product_select4,
my_site_products.product_select5,
my_site_products.product_select6,
my_site_products.product_select7,
my_site_products.product_select8,
my_site_products.product_select9,
my_site_products.product_select10,
my_site_brands.brand_name,
my_site_product_cats.product_cat_name
FROM
my_site_orders
INNER JOIN my_site_users ON (my_site_orders.order_orderer_id = my_site_users.user_id)
INNER JOIN my_site_products ON (my_site_orders.order1_id = my_site_products.product_id)
INNER JOIN my_site_brands ON (my_site_products.product_brand_id = my_site_brands.brand_id)
INNER JOIN my_site_product_cats ON (my_site_products.product_cat_id = my_site_product_cats.product_cat_id)
WHERE
my_site_orders.order1_type = my_site_products.product_select1
OR
my_site_orders.order1_type = my_site_products.product_select2
OR
my_site_orders.order1_type = my_site_products.product_select3
OR
my_site_orders.order1_type = my_site_products.product_select4
OR
my_site_orders.order1_type = my_site_products.product_select5
OR
my_site_orders.order1_type = my_site_products.product_select6
OR
my_site_orders.order1_type = my_site_products.product_select7
OR
my_site_orders.order1_type = my_site_products.product_select8
OR
my_site_orders.order1_type = my_site_products.product_select9
OR
my_site_orders.order1_type = my_site_products.product_select10
But i get the data with:
SELECT
my_site_orders.order_id,
my_site_orders.order_orderer_id,
my_site_orders.order_ip,
my_site_orders.order_time,
my_site_orders.order_campaign_id,
my_site_orders.order_status,
my_site_orders.order_shipment_status,
my_site_orders.order_shipping_method,
my_site_orders.order1_id,
my_site_orders.order1_coupon_id,
my_site_orders.order1_amount,
my_site_orders.order1_price,
my_site_orders.order_cargo_name,
my_site_orders.order_cargo_surname,
my_site_orders.order_cargo_vatnumber,
my_site_orders.order_cargo_line1,
my_site_orders.order_cargo_line2,
my_site_orders.order_cargo_stateprovince,
my_site_orders.order_cargo_district,
my_site_orders.order_cargo_city,
my_site_orders.order_cargo_contactphone,
my_site_orders.order_invoice_name,
my_site_orders.order_invoice_surname,
my_site_orders.order_invoice_company,
my_site_orders.order_invoice_line1,
my_site_orders.order_invoice_line2,
my_site_orders.order_invoice_vatnumber,
my_site_orders.order_invoice_vat,
my_site_orders.order_invoice_stateprovince,
my_site_orders.order_invoice_district,
my_site_orders.order_invoice_city,
my_site_orders.order_invoice_contactphone,
my_site_orders.order_type,
my_site_orders.order_note,
my_site_orders.order_hash,
my_site_orders.order_bank_hash,
my_site_orders.order_phone_number,
my_site_orders.order_price,
my_site_orders.order_cc_name,
my_site_orders.order_instalment_amount,
my_site_orders.order_instalment_price,
my_site_users.user_name,
my_site_users.user_email,
my_site_orders.order1_type,
my_site_users.user_realname,
my_site_users.user_surname,
my_site_products.product_brand_id,
my_site_products.product_id,
my_site_products.product_cat_id,
my_site_products.product_title,
my_site_products.product_snippet,
my_site_products.product_description,
my_site_products.product_code,
my_site_products.product_normalprice,
my_site_products.product_price,
my_site_products.product_quantity,
my_site_products.product_image1,
my_site_products.product_image1_t1,
my_site_products.product_image4,
my_site_products.product_image4_t1,
my_site_products.product_image4_t2,
my_site_products.product_image_big4,
my_site_products.product_image5,
my_site_products.product_image5_t1,
my_site_products.product_image5_t2,
my_site_products.product_image_big5,
my_site_products.product_select_choice_title,
my_site_products.product_select_option_title,
my_site_products.product_select1,
my_site_products.product_select2,
my_site_products.product_select3,
my_site_products.product_select4,
my_site_products.product_select5,
my_site_products.product_select6,
my_site_products.product_select7,
my_site_products.product_select8,
my_site_products.product_select9,
my_site_products.product_select10,
my_site_brands.brand_name,
my_site_product_cats.product_cat_name
FROM
my_site_orders
INNER JOIN my_site_users ON (my_site_orders.order_orderer_id = my_site_users.user_id)
INNER JOIN my_site_products ON (my_site_orders.order1_id = my_site_products.product_id)
INNER JOIN my_site_brands ON (my_site_products.product_brand_id = my_site_brands.brand_id)
INNER JOIN my_site_product_cats ON (my_site_products.product_cat_id = my_site_product_cats.product_cat_id)
First, you can use IN() instead of so many ORs. It will render your query shorter, and easier for reading. Next, obviously you have no orders with order1_type IN(product_select1,product_select2,product_select3,product_select4,product_select5,product_select6,product_select7,product_select8,product_select9,product_select10). Some sample data (from the first query) could help us to rewrite your query correctly

Categories