0% found this document useful (0 votes)
47 views3 pages

SQL Syntax Examples for PHP

This document contains SQL syntax examples for performing various operations in PHP like selecting, updating, inserting, and joining data from database tables. The examples demonstrate retrieving data from multiple tables using joins, selecting data based on conditions, updating and inserting new records, and more. Common operations shown include selecting all records from a table ordered by a column, updating field values where a specific ID matches, and joining products and categories tables to retrieve associated data.

Uploaded by

Md Ziarul islam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

SQL Syntax Examples for PHP

This document contains SQL syntax examples for performing various operations in PHP like selecting, updating, inserting, and joining data from database tables. The examples demonstrate retrieving data from multiple tables using joins, selecting data based on conditions, updating and inserting new records, and more. Common operations shown include selecting all records from a table ordered by a column, updating field values where a specific ID matches, and joining products and categories tables to retrieve associated data.

Uploaded by

Md Ziarul islam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sql syntax for php

$sql="select * from categories order by categories_name asc";


$update_status_sql="update categories set status='$status' where id='$id' ";
chbrand = "update categories set categories_name='$categories_name' where id='$id'";
$chbrand = "update sub_categories set category_id='$category_id',
sub_categories='$sub_categories' where id='$id'";
$query = "insert into categories(categories_name,status) values('$categories_name','1')";
$update_status_sql="update categories set status='$status' where id='$id'";
$delete_sql="delete from categories where id='$id'";
$catName = "select * from categories WHERE categories_name = '$categories_name'";
"select id,categories_name from categories order by categories_name asc"
Most usefull join
$sql="select products.*,categories.categories_name,brands.brands_name
from products,categories,brands
where products.category_id=[Link] and products.brand_id=[Link]
order by [Link] desc";
//using aliases
$query = "SELECT p.*, [Link], [Link]
FROM tbl_product as p, tbl_category as c, tbl_brand as b
WHERE [Link] = [Link] AND [Link] = [Link]
ORDER BY [Link] DESC";

//using inner join


$query = "SELECT tbl_product.*, tbl_category.catName, tbl_brand.brandName
FROM tbl_product
INNER JOIN tbl_category
ON tbl_product.catId = tbl_category.catId
INNER JOIN tbl_brand
ON tbl_product.brandId = tbl_brand.brandId
ORDER BY tbl_product.productId DESC";
$sql="select * from admin_users where username='$username' and password='$password'";
"SELECT * FROM product WHERE status=1 AND categories_id = '$cat_id'"
SELECT product.*,[Link] FROM product,categories WHERE [Link]=1
AND [Link]='$product_id'
$sql="insert into
`order`(user_id,address,city,pincode,payment_type,payment_status,order_status,added_on,total_
price)
values('$user_id','$address','$city','$pincode','$payment_type','$payment_status','$order_status','$
added_on','$total_price') ";
$sql="select `order`.*,order_status.name as order_status_str from `order`,order_status where
`order`.user_id='$uid' and order_status.id=`order`.order_status");
"select distinct(order_detail.id) ,order_detail.*,products.product_name,[Link] from
order_detail,products ,`order` where order_detail.order_id='$order_id' and `order`.user_id='$uid'
and order_detail.product_id=[Link]"

function get_product($con,$cat_id='',$product_id='',$is_best_seller='',$limit='',$search_str='',
$sort_order=''){
$sql="select products.*,categories.categories_name from products,categories where
[Link]=1 ";
if($cat_id!=''){
$sql.=" and products.category_id=$cat_id ";
}
if($product_id!=''){
$sql.=" and [Link]=$product_id ";
}
if($is_best_seller!=''){
$sql.=" and products.best_seller=1 ";
}
$sql.=" and products.category_id=[Link] ";
if($search_str!=''){
$sql.=" and (products.product_name like '%$search_str%' or
products.product_details like '%$search_str%') ";
}
if($sort_order!=''){
$sql.=$sort_order;
}else{
$sql.=" order by [Link] desc ";
}
if($limit!=''){
$sql.=" limit $limit";
}

$res=mysqli_query($con,$sql);
$data=array();
while($row=mysqli_fetch_assoc($res)){
$data[]=$row;
}
return $data;
}
$sql="select products.*,categories.categories_name from products,categories where
[Link]=1 "; and $sql.=" and products.category_id=[Link] "; aituku common karon
products and categories table moddhe join hosse.

You might also like