SQL code for search option in a php web page - mysql

I have tried to write the search.php file for an online library web page. This is what I have done , I can search books by titles ,but I also want to search them by author or description
if (isset($_GET['str'])) {
$category = htmlspecialchars($_GET['str']);
$qlib = 'SELECT * FROM book_description JOIN book_class ON book_class.series = book_description.series WHERE book_description.title LIKE "%'.$category.'%"'

SELECT * FROM book_description
JOIN book_class ON book_class.series = book_description.series
WHERE book_description.title LIKE "%'.$category.'%"'
and (or) book_description.author like "%'.$author.'%"'
...
and so on
Hope it helps! You can use and or or in where clause

Related

Pull SKU, Manufacturer and custom attribute from Magento database

I'm trying to pull a full list of products from the database with the SKU, manufacturer and a custom attribute called 'GTIN'.
I'm really struggling with the custom attribute part.
This statement works to pull the manufacturer and SKU
SELECT cpe.sku, m1.manufacturer,
FROM catalog_product_entity AS cpe
INNER JOIN exportview_manufacturer AS m1 ON cpe.entity_id = m1.entity_id
My MySQL is very poor and I can't seem to get this custom attribute. I've found the following statement online which I KNOW has all the details I need to make this work but thus far I've been getting my head in a mess trying to implement it
SELECT e.entity_id AS product_id, var.value AS product_name
FROM catalog_product_entity e, eav_attribute eav, catalog_product_entity_varchar var
WHERE
e.entity_type_id = eav.entity_type_id
AND eav.attribute_code = 'gtin'
AND eav.attribute_id = var.attribute_id
AND var.entity_id = e.entity_id
I simply need the first statement to also include the 'gtin' column, but joining is where I'm falling short. Can someone please assist?
You need to create one PHP script to get all product and it's attribute. So first create all_product.php file on your Magento root and add below code.
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../app/Mage.php';
Mage::app();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)
foreach ($collection as $product) {
echo $product->getName(); //get name
/*you can get any attribue you want like name above. Even you can get all custom attribule for product for that you need to use print_r($collection->getData()) above foreach*/
}
Hope it will work.

Reformat selected code in strings in phpStorm

We know that there is a way to reformat and rearrange codes via Ctrl+ alt + l in phpStorm and it works fine.
But this ability can not reformat codes in a simple String(codes that are surrounded by single or double quotes).
For example this code is to run a select Query on DB :
$getPro = $db->Query("select products.`product_id`,products.`pro_title`, products.`pro_quantity`,products.`new_price`,products.`addedPrice`,products.`discount`, product_pics.`pic_name`
from `products` left join `product_pics` on `product_pics`.`product_id` = products.`product_id`
where products.`product_id`=:p_id limit 1", array (':p_id' => $p_id));
and i want to reformat this code to bellow one on pressing Ctrl+ alt + l or Any other way:
$getPro = $db->Query("SELECT
products.`product_id`,
products.`pro_title`,
products.`pro_quantity`,
products.`new_price`,
products.`addedPrice`,
products.`discount`,
product_pics.`pic_name`
FROM `products`
LEFT JOIN `product_pics` ON `product_pics`.`product_id` = products.`product_id`
WHERE products.`product_id` = :p_id
LIMIT 1
", array (':p_id' => $p_id));
Of course I realized that we can do this via copy SQL string in phpStorm MySQL Console (Ctrl+shift+f10) and then use Ctrl+ alt + l shortcut.
What is the solution in your opinion?
Edit:
according to LazyOne comment, this is a screenshot of my code when pressing Alt+Enter . But there is no Edit MySQL Fragment option.

Using MD5 and CONCAT in MuSQL WHERE clause

I am creating a reset password procedure. this way:
1- Send reset email to the user (whom forgot his/her password), and this email contain a link to this address:
http://www.example.com/resetpassword.php?userid=1b2798bad6ee465d967cdb71ced504f7
The value of the parameter [userid] is generated by this PHP code:
<?php
md5($row_Recordset2['userID'].date('d-m-Y'));
?>
Note: I did this so that I can get a unique parameter value containing: The MD5 hash of the real user id + the current date (concatenation) , Why? so the link in the email will be available for the current date only. I do not want that link to work in the next day.
2- When the person click on the above link, he/she will be taken to the page [resetpassword.php]
3- In the page [resetpassword.php] page I have this piece of code:
$colname_Recordset1 = "-1";
if (isset($_GET['userid'])) {
$colname_Recordset1 = $_GET['userid'];
}
mysql_select_db($database_aaa_database, $aaa_database);
$query_Recordset1 = sprintf("SELECT * FROM users WHERE md5(CONCAT(userID,
DATE_FORMAT(NOW(),'%d-%m-%Y'))) = %s ", GetSQLValueString($colname_Recordset1,
"text"));
$Recordset1 = mysql_query($query_Recordset1, $aaa_database) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
The SELECT statement return [Query was empty] ... What is the problem and how to solve it?
I have tested this MySQL statement manually:
SELECT userFirstName, md5(CONCAT(userID, DATE_FORMAT(NOW(),'%d-%m-%Y'))) from users
And it return many rows, one of the rows is like this:
Sam 1b2798bad6ee465d967cdb71ced504f7
So, the:
md5(CONCAT(userID, DATE_FORMAT(NOW(),'%d-%m-%Y')))
works fine and it return the same exact value generated by the PHP code :
<?php
md5($row_Recordset2['userID'].date('d-m-Y'));
?>
PHP give: 1b2798bad6ee465d967cdb71ced504f7
MySQL give: 1b2798bad6ee465d967cdb71ced504f7
But this:
md5(CONCAT(userID, DATE_FORMAT(NOW(),'%d-%m-%Y')))
seems to be NOT WORKING in the WHERE clause:
sprintf("SELECT * FROM users WHERE md5(CONCAT(userID,
DATE_FORMAT(NOW(),'%d-%m-%Y'))) = %s ", GetSQLValueString($colname_Recordset1,
"text"));
By the way, I am using Dreamweaver.
I don't know how to solve this problem. Any help will be highly appreciated.

$sql = mysql_query("SELECT * FROM books WHERE tags LIKE '%$_GET[term]%' LIMIT 0,$_GET[results]");

I'm trying to make a Like search engine, something simple for my site. Here is the code I'm using:
$sql = mysql_query("SELECT * FROM books WHERE tags LIKE '%$_GET[term]%' LIMIT 0,$_GET
[results]");
while($ser = mysql_fetch_array($sql)) {
echo $ser['title']."<br />";
}
But when it searches, it will only look for a single Term within my tags. Any one have any good ideas on how to have it search through all my tags?
$sql = mysql_query("SELECT * FROM books WHERE tags LIKE '%$_GET[term]%'");
Remove the limit from your query, you should get all the matching rows from your query.
i highly recommend not to use value directly from user first parse it, to protect yourself from SQL injections
try removing the limit and then check is it give same result?
Use this:-
$keywords =$_GET[term];
$keywords = explode('delimiter', $keyword);
$searchTermKeywords = array();
foreach ($keywords as $word)
{
$searchTermKeywords[] = "tags LIKE '%$word%'";
}
$result= mysql_query("SELECT * FROM books WHERE ".implode(' AND ', $searchTermKeywords).");

MySQL question: Appending text to a wordpress post, but only if it's in a certain category

I need to amend (via CONCAT, presumably) something to every wordpress post if it belongs to a certain category (say, category ID 7), but I'm struggling to get it to work.
To test, I'm first trying to select all the relevant posts. So far, I have the following:
SELECT post_title
FROM cruise_wp_posts
LEFT JOIN cruise_wp_term_relationships
ON cruise_wp_term_relationships.object_id = cruise_wp_posts.ID
WHERE term_taxonomy_id = 87;
However, it only lists posts that are only in category 87 - I need all posts that are in category 87 (and possibly other categories too)
I'm a MySQL newbie, and this is really breaking my brain.
Any pointers would be passionately welcomed.
The best way to do it is to filter it in as needed. This way the addition is made everywhere the_content is used and not just in the templates you modify.
<?php
function my_content_concat($the_content) {
if (in_category(7)) {
$the_content .= '<br /><br />foo!';
}
return $the_content;
}
add_filter('the_content', 'my_content_concat', 9);
?>
in_category can take the id, name or slug of your target category.
I put the filter at 9 so that it runs before WordPress texturizes the content. If you don't need that run it at 11.
Why not just use get_the_category( $id ) and ammend the text when you output the post?
$cat = get_the_category( $postID );
if ($cat == 7) {
//Add text here
}