John Davidson

php - SQL search query where the product name

0 comments
Message:


Here the sample product name "SRM - DAZA RS3 TTRS GTX35 Turbo"


sql like query i'm using this


        ->where(function ($q) use ($query) {
foreach (explode(' ', trim($query)) as $word) {
$q->where('name', 'like', '%' . $word . '%')
->orWhere('description', 'like', '%' . $word . '%')
->orWhere('tags', 'like', '%' . $word . '%')
->orWhereHas('product_translations', function ($q) use ($word) {
$q->where('name', 'like', '%' . $word . '%')
->orWhere('description', 'like', '%' . $word . '%');
})
->orWhereHas('stocks', function ($q) use ($word) {
$q->where('sku', 'like', '%' . $word . '%');
});
}
})

this works perfect but still have an issue. if i search "SRM" im getting this specific product


but if i type mix match word "SRM turbo" im not getting this product.


so please let me know what is the right query that i need to use



My Answer:
To search for a product with a specific name in a SQL database using PHP, you can use a query like the following:

php
$productName = "Sample Product Name";
$sql = "SELECT * FROM products WHERE product_name = '$productName'";
$result = mysqli_query($connection, $sql);

if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
// Output or process the product data here
}
} else {
echo "No products found with the name: $productName";
}


In this query, replace "products" with the name of your table and "product_name" with the column name where the product names are stored. Make sure to sanitize the input to prevent SQL injection attacks.

Rate this post

4 of 5 based on 9151 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us