How to fetch data from database in Magento with LIKE keyword? - php

I want a select query which fetch the data from database with "like" keyword
eg.
select product name, image , description
from table where product name LIKE "'.$name'";
I want it in Magento that how to fetch this fields from database in Magento. My existing query is:
$sql = "
SELECT value FROM catalog_product_entity_varchar
WHERE entity_type_id = (
SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product') AND attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'name' AND entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product')
)
";
Now the join query is :-
<?php
$sql = "
SELECT p.entity_id , pv.value as name , pt.value as description , GROUP_CONCAT (DISTINCT ( cp.category_id ) SEPARATOR ', ') as categories , GROUP_CONCAT (DISTINCT (pm.value) SEPARATOR ', ') as imagesPath
FROM catalog_product_entity as p
INNER JOIN catalog_product_entity_varchar as pv on pv.entity_id = p.entity_id and pv.attribute_id = 71
INNER JOIN catalog_product_entity_text as pt on pt.entity_id = p.entity_id and pt.attribute_id = 72
INNER JOIN catalog_category_product as cp on cp.product_id = p.entity_id
LEFT JOIN catalog_product_entity_media_gallery as pm on pm.entity_id = p.entity_id and pm.attribute_id = 88
GROUP BY cp.product_id , pm.entity_id " ;
foreach($readConnection->fetchAll($sql) as $orders)
{
?>
<?php echo $orders['name']; ?>
<?php echo $orders['description']; ?>
<?php
}
?>

This one is correct code to fetch the name and description of item in LIKE clause
Thank you so much #Halfer :)
$sql = SELECT p.`entity_id`, pv.`value` as name, pt.`value` as description
FROM `catalog_product_entity` as p
INNER JOIN `catalog_product_entity_varchar` as pv on pv.`entity_id` = p.`entity_id` and pv.`attribute_id` = 71
INNER JOIN `catalog_product_entity_text` as pt on pt.`entity_id` = p.`entity_id` and pt.`attribute_id` = 72
Where pv.value LIKE '%hp%';

$sql_jacket = "SELECT entity_id FROM customer_entity where email
like '".$_REQUEST['useremail']."'";
$result_jacket = $conn->query($sql_jacket);
if ($result_jacket->num_rows > 0)
{
while($row_jacket = $result_jacket->fetch_assoc())
{
$result1['user_status']=1;
}
}

Related

How to show all data from multiple table using sql JOIN query?

I have following Mysql table :
1) products
2) product_images
3) product_date_time
4) product_menu
5) chef_profile
I need to show all data from those 5 table to a page called menu-details.php
All table have foreign key as p_id of products table id and Some table have multiple data of same p_id.
To get this done I am using following mysql join query :
$query = mysqli_query($conn, "SELECT p.p_id, p.p_title, p.p_description,
p.p_price, p.p_availability, p.delivery_option, p.event_location,
pimg.p_large_image, pimg.p_small_image, pdt.date, pm.p_menu_title,
pm.p_menu_description, pm.p_menu_price, pm.p_menu_availability, chef.fname,
chef.lname, chef.chef_details FROM products as p LEFT JOIN product_images as
pimg ON p.p_id = pimg.p_id LEFT JOIN product_date_time as pdt ON pdt.p_id =
p.p_id LEFT JOIN product_menu as pm ON pm.p_id = p.p_id LEFT JOIN
chef_profile AS chef ON chef.u_id = p.u_id WHERE p.p_id = '$mid' ") or
die('wrong query');
To show data I am using following php code :
$result = mysqli_fetch_array($query);
$menu_title = htmlspecialchars($result['p_title']);
My Questions
Should I use 5 unique sql query to show data to that menu_details.php page ?
Now this query is showing single record from those table which have multiple data of same p_id. I need to show all data which have same p_id with multiple data. How can I do this ?
For example : I have following data in product_images table :
pimg_id p_large_image p_id u_id
1 name 203 25
1 name 204 26
1 name 204 25
1 name 205 27
1 name 205 28
So If I use php while loop it's showing all data from that table but it's should be show all data based on p_id for e.g p_id = 204
while($result = mysqli_fetch_array($query)) {
echo $menu_l_image = htmlspecialchars($result['p_large_image']);
echo '<br/>';
}
If the p.id is an integer remove the single quote form $mid
"SELECT
p.p_id,
p.p_title,
p.p_description,
p.p_price,
p.p_availability,
p.delivery_option,
p.event_location,
pimg.p_large_image,
pimg.p_small_image,
pdt.date,
pm.p_menu_title,
pm.p_menu_description,
pm.p_menu_price,
pm.p_menu_availability,
chef.fname,
chef.lname,
chef.chef_details
FROM products as p
LEFT JOIN product_images as pimg ON p.p_id = pimg.p_id
LEFT JOIN product_date_time as pdt ON pdt.p_id = p.p_id
LEFT JOIN product_menu as pm ON pm.p_id = p.p_id
LEFT JOIN chef_profile AS chef ON chef.u_id = p.u_id
WHERE p.p_id = $mid "
of previus a proper sanitize of the $mid vars you can use
"SELECT
p.p_id,
p.p_title,
p.p_description,
p.p_price,
p.p_availability,
p.delivery_option,
p.event_location,
pimg.p_large_image,
pimg.p_small_image,
pdt.date,
pm.p_menu_title,
pm.p_menu_description,
pm.p_menu_price,
pm.p_menu_availability,
chef.fname,
chef.lname,
chef.chef_details
FROM products as p
LEFT JOIN product_images as pimg ON p.p_id = pimg.p_id
LEFT JOIN product_date_time as pdt ON pdt.p_id = p.p_id
LEFT JOIN product_menu as pm ON pm.p_id = p.p_id
LEFT JOIN chef_profile AS chef ON chef.u_id = p.u_id
WHERE p.p_id = ". $mid . ";"

Issue with an mySQL sentence including two temporary table creation

I have a mySQL sentence that works like a charm if I execute it in my phpMyAdmin:
CREATE TEMPORARY TABLE hash1
SELECT * FROM
(
(
SELECT DISTINCT feature_id AS fl, feature_value AS fv FROM gf_product_features WHERE feature_id = '1' AND feature_value = 'No frost total'
) UNION
(
SELECT DISTINCT feature_id AS fl, feature_value AS fv FROM gf_product_features WHERE feature_id = '3' AND feature_value = '43'
)) AS q;
CREATE TEMPORARY TABLE hash2
SELECT * FROM hash1;
SELECT
p.id AS id,
p.main_image AS main_image,
p.type AS taxonomy,
p.name AS model,
p.sku AS sku,
p.price AS price,
b.brand_name AS brand_name,
b.brand_image AS brand_logo,
pf.feature_value AS feature_value,
f.feature AS feature_label,
f.id AS feature_id
FROM
(
SELECT a.*
FROM gf_product AS a
INNER JOIN
(
SELECT product_id
FROM
(
SELECT a.product_id , count(*) AS commons
FROM gf_product_features AS a
INNER JOIN hash1 AS b
ON a.feature_id = b.fl
AND a.feature_value = b.fv
GROUP BY a.product_id
) AS features
WHERE commons = (SELECT count(*) AS count FROM hash2)
) b1 ON a.id = b1.product_id
) AS p
INNER JOIN gf_brands AS b
ON p.brand_id = b.id
INNER JOIN gf_product_features AS pf
ON pf.product_id = p.id
INNER JOIN gf_features AS f
ON pf.feature_id = f.id
ORDER BY price ASC,
feature_id ASC
I want to execute a php function through Ajax request, that constructs dinamically the sql sentence above, but I'm always getting this error in my browser's console:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TEMPORARY TABLE hash2
SELECT * FROM hash1;
SELECT
' at line 12
And thus, the following error too:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /www/htdocs/example/inc/functions.php on line 538
Which corresponds to this line of my php code:
while ($row = mysqli_fetch_assoc($result))
Maybe clone hash2 table from hash1 table
CREATE TEMPORARY TABLE hash2
SELECT * FROM hash1;
sounds weird, but if I don't do this in that way, in my phpMyAdmin I get this error:
#1137 - Can't reopen table: 'b'
I can't realize why my sql sentence works fine in my phpMyadmin but, when I construct it on my php file it doesn't works. Can anybody help me, please?
For further information, this is my PHP code:
function getProductsFromFilteredQuery($connection, $filters, &$html)
{
$sql = '';
$m = count($filters); // $filters are an array of values like this: ['value1A, value2A', 'value1B, value2B', ...]
$sql = 'CREATE TEMPORARY TABLE hash1
SELECT * FROM
(';
for ($n = 0; $n < $m; $n++)
{
$string = explode(', ', $filters[$n]);
$feature_id = $string[0];
$feature_value = $string[1];
$sql .= "
(
SELECT DISTINCT feature_id AS fl, feature_value AS fv FROM gf_product_features WHERE feature_id = '" . $feature_id . "' AND feature_value = '" . $feature_value . "'
)";
if ($n < ($m - 1))
{
$sql .= ' UNION ';
}
}
$sql .= ') AS q;
CREATE TEMPORARY TABLE hash2 -- In this line I get an error
SELECT * FROM hash1;
SELECT
p.id AS id,
p.main_image AS main_image,
p.type AS taxonomy,
p.name AS model,
p.sku AS sku,
p.price AS price,
b.brand_name AS brand_name,
b.brand_image AS brand_logo,
pf.feature_value AS feature_value,
f.feature AS feature_label,
f.id AS feature_id
FROM
(
SELECT a.*
FROM gf_product AS a
INNER JOIN
(
SELECT product_id
FROM
(
SELECT a.product_id , count(*) AS commons
FROM gf_product_features AS a
INNER JOIN hash1 AS b
ON a.feature_id = b.fl
AND a.feature_value = b.fv
GROUP BY a.product_id
) AS features
WHERE commons = (SELECT count(*) AS count FROM hash2)
) b1 ON a.id = b1.product_id
) AS p
INNER JOIN gf_brands AS b
ON p.brand_id = b.id
INNER JOIN gf_product_features AS pf
ON pf.product_id = p.id
INNER JOIN gf_features AS f
ON pf.feature_id = f.id
ORDER BY price ASC,
feature_id ASC';
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) // In this line I get an error too
{
// Do some stuff... and at last, return the resulting $html
}
};
I finally could find the error. In my phpMyAdmin it worked as well because someone can execute several queries in the SQL console. There is no problem with it.
However, when coding an mySQL query through PHP you only can run one mySQL sentence at once. Well, there is an exception: You can use mysqli_multi_query + mysqli_more_results, or something like these. But as I was coded it, you can't.
So there is two options: rewrite the PHP code like described in the pages of the two links above, or doing several mysqli_query within the PHP function.
I decided to do it through the second option, so the working code is the following (Notice the comments after each mysqli_query):
function getProductsFromFilteredQuery($mysqli, $filters, &$html) {
$sql = '';
$m = count($filters);
$sql = 'DROP TEMPORARY TABLE IF EXISTS hash1;';
$result = mysqli_query($mysqli, $sql); // A single query
$sql = 'DROP TEMPORARY TABLE IF EXISTS hash2;';
$result = mysqli_query($mysqli, $sql); // Another single query
$sql = 'CREATE TEMPORARY TABLE hash1
SELECT * FROM
(';
for ($n = 0; $n < $m; $n++)
{
$string = explode(', ', $filters[$n]);
$feature_id = $string[0];
$feature_value = $string[1];
$sql .= "
(SELECT DISTINCT feature_id AS fl, feature_value AS fv FROM gf_product_features WHERE feature_id = '" . $feature_id . "' AND feature_value = '" . $feature_value . "')";
if ($n < ($m - 1))
{
$sql .= ' UNION ';
}
}
$sql .= ') AS q1';
$result = mysqli_query($mysqli, $sql); // Another single query
$sql = 'CREATE TEMPORARY TABLE hash2
SELECT * FROM hash1;';
$result = mysqli_query($mysqli, $sql); // Another single query
$sql = 'SELECT
p.id AS id,
p.main_image AS main_image,
p.type AS taxonomy,
p.name AS model,
p.sku AS sku,
p.price AS price,
b.brand_name AS brand_name,
b.brand_image AS brand_logo,
pf.feature_value AS feature_value,
f.feature AS feature_label,
f.id AS feature_id
FROM
(
SELECT a.*
FROM gf_product AS a
INNER JOIN
(
SELECT product_id
FROM
(
SELECT a.product_id , count(*) AS commons
FROM gf_product_features AS a
INNER JOIN hash1 AS b
ON a.feature_id = b.fl
AND a.feature_value = b.fv
GROUP BY a.product_id
) AS features
WHERE commons = (SELECT count(*) AS count FROM hash2)
) b1 ON a.id = b1.product_id
) AS p
INNER JOIN gf_brands AS b
ON p.brand_id = b.id
INNER JOIN gf_product_features AS pf
ON pf.product_id = p.id
INNER JOIN gf_features AS f
ON pf.feature_id = f.id
ORDER BY price ASC,
feature_id ASC';
$result = mysqli_query($mysqli, $sql); // Another single query. The last one.
while ($row = mysqli_fetch_assoc($result))
{
// My stuff here...
}
}; // #END of function

PHP Group by and list all the other column

I have a table:
The select db is:
$select_table2 = '
SELECT e.product_clicks
, e.product_id
, e.website_url
, u.name
, u.product_id
FROM `oc_aa_affiliatecollclicktracking` AS e
LEFT
JOIN `'.DB_PREFIX.'product_description` AS u
ON e.product_id = u.product_id
GROUP
BY e.website_url';
This will group it but won't list all product_id
I get:
http://127.0.01
36
I'd like to group it but see all the product_id
http://127.0.01
36
40
33
$select_table2 = 'SELECT `product_id` FROM `oc_aa_affiliatecollclicktracking` WHERE website_url LIKE '%http%';
if you dont want to repeat product_id
$select_table2 = 'SELECT DISTINCT(`product_id`) FROM `oc_aa_affiliatecollclicktracking` WHERE website_url LIKE '%http%';

Why is my member id column returning null?

For some reason the member id field(auto inc.) in my huge query is returning null.I've tried every which way of selecting it... m.member_id AS member_id, etc.I cannot figure out why it is returning null when there is a value for that field in the table.
<?php
public function get_info($criteria = 0){
if(is_numeric($criteria)){
$where = "WHERE m.member_id = ".$criteria;
} else {
$where = "WHERE email_address = '".$criteria."'";
}
$query_member = "
SELECT
m.member_id AS member_id, m.display_name, m.email_address, m.group_id, m.status, m.activation_code, UNIX_TIMESTAMP(m.date_joined) AS date_joined,
m.gender, m.location, m.biography, m.mantra, m.birth_date, m.results_per_page, m.admin_emails, m.member_emails, m.last_active, m.avatar_id,
m.banner_id, m.signature, m.newsletter_subscription, m.recruiting_status, m.facebook_username, m.website, m.steam_username, m.xboxlive_gamertag, m.psn_id,
g.group_id, g.title, g.description,
a.attachment_id, a.file_name,
f.message_id, f.author_id, COUNT(f.message_id) AS forum_count,
b.attachment_id AS banner_id, b.file_name AS banner_file,
mr.request_id, mr.author_id, mr.recipient_id, mr.status, COUNT(mr.request_id) AS total_friends,
tm.team_member_id, tm.member_id, tm.team_id
FROM members AS m
LEFT JOIN member_groups AS g ON (m.group_id = g.group_id)
LEFT JOIN attachments AS a ON (m.avatar_id = a.attachment_id)
LEFT JOIN forum_messages AS f ON (m.member_id = f.author_id)
LEFT JOIN attachments AS b ON (m.banner_id = b.attachment_id)
LEFT JOIN member_requests AS mr ON (m.member_id = mr.author_id OR m.member_id = mr.recipient_id) AND mr.status = 1
LEFT JOIN team_members AS tm ON (m.member_id = tm.member_id) AND date_left = ''
".$where."
GROUP BY m.member_id
LIMIT 1";
//show_error($query_member);
if($query_member = $this->db->query($query_member)){
if($query_member->num_rows() > 0){
var_dump($query_member->row_array());
Because you select two fields with the same name. So MySQL will return result of last one. Add aliases:
SELECT m.member_id AS member_id_1, tm.member_id AS member_id_2 ...

echo list of products by category

Can someone tell me how to echo data that is relating with another table?
I have a table
tbl_category_products:<
- id_categorys
- category
And other table for the products
tbl_products:
- title
- description
- id_categorys
How could I echo the list of products by category in PHP?
Here is some code, but no sucess.
Im trying but no sucess, here is the original code
Table names:
tbl_categorias_noticias
- id_categoria_noticia
- categoria
tbl_noticias
- id_noticia
- id_categoria_noticia
-titulo
-decricao
-msg
-nome_arquivo
-legenda
<?php
$categoryId = 1;
$sql_visualizar = mysql_query("SELECT * FROM tbl_noticias AS t1 JOIN tbl_categorias_noticias c
ON c.id_categoria_noticia=t1.id_categoria_noticia WHERE id_categoria_noticia = {$categoryId}");
while($linha = mysql_fetch_array($sql_visualizar)){
$titulo = $linha ['titulo'];
$descricao = $linha['descricao'];
$msg = $linha['msg'];
$legenda = $linha['legenda'];
$nome_arquivo = $linha['nome_arquivo'];
$id_noticia = $linha['id_noticia'];
?>
You need a join:
SELECT t1.title, t1.description, t2.category
FORM tbl_products t1
LEFT JOIN tbl_category_products t2 ON t1.id_categorys = t2.id_categorys
Something like this
First of all: mysql connect
$categoryId = 1;
$query = "SELECT t1.* FROM tbl_products AS t1 JOIN tbl_category_products c
ON c.id_categorys=t1.id_categorys WHERE id_categorys = {$categoryId}";
$result = mysql_query($query);
while($r = mysql_fetch_assoc($result)) {
//show your products
}
Use a JOIN to query both tables
SELECT p.title, p.decription, c.category
FROM tbl_products p
JOIN tbl_category_products c
ON c.id_categorys=p.id_categorys
ORDER BY c.category ASC
SELECT tp.title, tp.description, tcp.category FORM tbl_products tp,tbl_category_products tcp where tp.id_categorys = tcp.id_categorys
Try this
SELECT t1.title, t1.description, t2.category
FORM tbl_products t1
JOIN
tbl_category_products t2
ON t1.id_categorys = t2.id_categorys
WHERE t1.id_categorys = 'myCategory'

Categories