showing data from database using bindParam - php

i want to try to showing my data from database using bindParam but i get some error.
Recoverable fatal error: Object of class PDOStatement could not be converted to string in C:\xampp\htdocs\piratefiles\search.php on line 15
here my code
$category = htmlentities($_GET['c']);
$query = htmlentities($_GET['q']);
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = 20;
$limit_start = ($page - 1) * $limit;
$query = $db->prepare ("SELECT * FROM `posting` WHERE 'category' = :category AND 'file_name' like :query ORDER BY date DESC LIMIT ".$limit_start.",".$limit);
$query->bindParam(":category", $category);
$query->bindParam(":query", $query);
$query->execute();

$query was the user input, then you assigned it as the PDOStatement, then your the passing it back to bindParam
Change the var name.
$category = htmlentities($_GET['c']);
$query = htmlentities($_GET['q']);
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = 20;
$limit_start = ($page - 1) * $limit;
$stmt = $db->prepare ("SELECT * FROM `posting` WHERE 'category' = :category AND 'file_name' like :query ORDER BY date DESC LIMIT ".$limit_start.",".$limit);
$stmt->bindParam(":category", $category);
$stmt->bindParam(":query", $query);
$stmt->execute();

Since im using LIKE so, need to make another variable.
$keyword1 = "%".$category."%";
$keyword2 = "%".$query1."%";
Here's Full code.
$category = htmlentities($_GET['c']);
$query1 = htmlentities($_GET['q']);
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = 20;
$limit_start = ($page - 1) * $limit;
$query = $db->prepare ("SELECT * FROM `posting` WHERE category LIKE :category AND file_name LIKE :query1 ORDER BY date DESC LIMIT ".$limit_start.",".$limit);
$keyword1 = "%".$category."%";
$keyword2 = "%".$query1."%";
$query->bindParam(":category", $keyword1);
$query->bindParam(":query1", $keyword2);
$query->execute();

Related

PDO pagination with LIKE

The following PHP SQL code shows error
Recoverable fatal error: Object of class PDOStatement could not be converted to string in /home/customer/xxxx/fetch_data.php on line 28
I was trying to display products information from the table filter
There are two $statement->execute();, one is to count total search results and another one is to display products for the current page.
I new to PDO method and not an expert in overall coding.
The $filter_query = $stmt . 'LIMIT '.$start.', '.$limit.''; is causing issues.
With that and related functions, the code works and displays data. But if I enable it, the error shows up.
$limit = '5';
$page = 1;
if($_POST['page'] > 1)
{
$start = (($_POST['page'] - 1) * $limit);
$page = $_POST['page'];
}
else
{
$start = 0;
}
$search = "%samsung%";
$stmt = $connect->prepare("SELECT * FROM filter WHERE product_name LIKE :needle");
$stmt->bindParam(':needle', $search, PDO::PARAM_STR);
##
##
$filter_query = $stmt . 'LIMIT '.$start.', '.$limit.'';
$statement->execute();
$total_data = $stmt->rowCount();
$stmt = $connect->prepare($filter_query);
$stmt->execute();
$result = $stmt->fetchAll();
$total_filter_data = $stmt->rowCount();
$output = '
<h3> '.$total_data.' results found </h3>
and display each product
I tried the following code as suggested by Your Common Sense and the pagination and total search result count is working fine, but no products are getting displayed.
$limit = '5';
$page = 1;
if($_POST['page'] > 1)
{
$start = (($_POST['page'] - 1) * $limit);
$page = $_POST['page'];
}
else
{
$start = 0;
}
$name=str_replace(' ', '%', $_POST['query']);
$search = "%$name%";
$base_sql = "SELECT %s FROM filter WHERE product_name LIKE ?";
##
##
####
$count_sql = sprintf($base_sql, "count(*)");
$stmt = $connect->prepare($count_sql);
$stmt->execute([$search]);
$total_data = $stmt->fetchColumn();
####
$data_sql = $count_sql = sprintf($base_sql, "*")." LIMIT ?,?";
$stmt = $connect->prepare($data_sql);
$stmt->execute([$search, $start, $limit]);
$result = $stmt->fetchAll();
##
$output = '
<h3> '.$total_data.' results found </h3> ';
if($total_data > 0)
{
foreach($result as $row)
{
and display each product
Using the following line makes the code show some data, but the pagination is not working.
$data_sql = sprintf($base_sql, "*");
$stmt = $connect->prepare($data_sql);
$stmt->execute([$search]);
$result = $stmt->fetchAll();
That's a not a trivial task as we need to run the same query twice but with minor differences. The first query will use count(*) SQL function to get the total number of records matching the search criteria. The second query will select actual fields with LIMIT clause to get the data for the single page.
$base_sql = "SELECT %s FROM filter WHERE product_name LIKE ?";
$count_sql = sprintf($base_sql, "count(*)");
$stmt = $connect->prepare($count_sql);
$stmt->execute([$search]);
$total = $stmt->fetchColumn();
$data_sql = $count_sql = sprintf($base_sql, "*")." LIMIT ?,?";
$stmt = $connect->prepare($data_sql);
$stmt->execute([$search, $start, $limit]);
$data = $stmt->fetchAll();
Important: In order for this code to work, make sure you are connecting to mysql properly. the proper connection will let a LIMIT clause to accept placeholders and also will warn you in case of any problems.

remove duplicate camma from mysql query

I am trying to fetch data with mulyiple value with Mysql IN operator but problem I am getting to much (,,,).
Here is my query :
$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id.= $page_id['page_id'];
$rel_b.= $page_id['rel_blog'];
}
$p_id."<br>";
echo $rel_b."<br>";
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
Ouery output is Like this this is not proper form so please help me .
SELECT * FROM pages WHERE rel_blog IN
(,114,93,126108default,,,,default,,,,default,,default,,,default,,default,,,,,,,default,,default,,,default,,default,,,,,,,,,,,,,,,,,,,,,,,,,,123,145,144,148,81,107default,,,,,default,,,,,,145,138,118default,,,,,,default,,,,,,,default,,,,,,,default,,,,,,,default,,,118default,,,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,default,,,,,,default,,,,,,,,,default,,,,,,,,default,,,,,default,,,,,,,123,122,125,113default,,,default,,,default,,124,145140default,,,,,,,,,,,,,default,,,,,,,,default,,,default,,,default,,,,default,,,,default,,default,,,,default,,,default,,,default,,default,,,,,,,default,,,,,,default,,,,default,,,,,,default,,,default,,,,137default,,,,,,,default,,,default,,,default,,,,,,default,,,,,,,,,,default,,,,,,,,82,83default,,,,,,default,,,,default,,,,default,,,default,,,default,,,,default,,,,default,,,,default,,,,default,,,default,,,,default,,,,default,,,default,,,default,,,,,,default,,,,,,default,,,,,default,,,default,,,,,default,,,,,default,,,,default,,,default,,default,,default,,default,,,,,,,default,,,,default,,default,,,,default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,,,,default,,,,default,,,,,default,,,,,,default,,,,,,,default,,,,,,default,,,,,,,default,,default,,default,,,,,default,,,default,,,default,,,default,,,,default,114,default,,,,,,117,116default,,,,,default,,,,117,116default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,default,,,default,,default,,,,,,default,,,,,,default,,,,,108default,,,,118,129,121,143,80,82,81,148,117,146,107,88,118,82,80,81,148,143,123,118,107,142,116,110,128,127,111,109,125,113,123,118,85,118,128,109,126,93,114,82,142,108,111,90,113,123,144,148,108,107,142,116,115,110,128,127,113,118,144,146,80,148,107,143,135,125,143,110,115,118,132,146,80,123,81,148,107,116,108,109,125,135,113,123,144,118,116,108,127,147,125,123,144,129,107,142,112,116,135,113,123,144,132,118,107,85,123,144,118,82,148,107,123,132,146,148,107,108,122,89,136,146,80,81,117,148,142,107,110,147,119,82,80,145,129,146,80,148,107,111,109,125,113,123,118,148,81,107,131,115,109,121,125,135,113,123,144,107,131,126,93,141,114,124,139,113,91,144,118,81,117,148,107,142,115,143,135,91,118,142,116,87,143,140,123,144,132,145,129,146,82,80,117,91,118,148,137,113,141,93,143,136,123,144,146,82,118,148,85,112,108,127,122,87,143,141,91,144,146,80,82,81,117,148,107,115,109,91,129,146,82,80,117,148,81,142,107,110,130,138,89,126,113,default,default,118,146,82,81,148,107,142,115,109,126,default,123,144,145,80,146,82,81,117,142,128,138,135,93,140,123,144,145,132,82,80,117,83,142,130,140,145,129,82,80,81,108,111,138,89,143,125,145,123,118,82,80,81,117,88,112,108,147,139,119,140,123,144,118,80,82,81,142,107,85,108,115,114,144,118,129,146,80,81,83,120,148,107,142,128,127,110,115,91,123,145,82,117,146,81,80,148,82,80,117,81,148,83,120,107,142,88,131,118,129,146,80,117,107,83,88,142,130,89,125,139,134,113,145,144,118,132,123,82,80,146,81,148,83,123,91,137,140,134,139,113,141,124,93140,150,150)
order by add_date desc limit 7
Actually I can't figure out how you are getting even a single comma... This is how you can try it out.
$get_id = "select rel_blog,page_id from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id.= $page_id['page_id'];
if($page_id['rel_blog']!=="" && $page_id['rel_blog']==="default") {
$rel_b.= $page_id['rel_blog'] .",";
}
}
$p_id."<br>";
$rel_b = substr($rel_b,0,-1); // get rid of the last comma
echo $rel_b."<br>";
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
You can do this before your query.
$rel_b = preg_replace('/,+/', ',', trim($rel_b, ','));
This will remove repeated commas as well as commas from beginning and end of the string.
Try using implode():
$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id = array();
$rel_b = array();
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id[] = $page_id['page_id'];
$rel_b[] = $page_id['rel_blog'];
}
$p_id = implode(',', $p_id);
$rel_b = implode(',', rel_b);
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";

How to skip rows from resultset in PHP?

i am working with search function and pagination.
heres my code for search function
if( isset($_POST['searchBut']) ) {
$name = $_POST['inputSearch'];
$pageSearch = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
//lets say there are 10 items selected with this query
$searchQuery = "SELECT name,image,price,description FROM image WHERE name LIKE '%".$name."%' ";
$result = $db->query($searchQuery);
//getting total rows
$fetchTotalRow = mysqli_num_rows($result);
//getting total pages
$totalPages = ceil($fetchTotalRow / $perPage);
$links = "";
for ($i = 1; $i <= $totalPages; $i++) {
$links .= ($i != $pageSearch ) ? "<li><a href='collections.php?page=$i'>Page $i</span></a></li> " : "<li class='active'><a href='collections.php?page=$i'>Page $pageSearch</a></li> ";
}
}
now how do i limit my page to only 9 items in first page, and the last one will be on second page.
i've succesfully apply this to normal pagination with LIMIT($start,$offset) function, but this is for database, how to do this with resultset ?
You need to add LIMIT to your query.
//page 1
$searchQuery = "SELECT name,image,price,description FROM image WHERE name LIKE '%".$name."%' LIMIT 0, 9";
//page 2
$searchQuery = "SELECT name,image,price,description FROM image WHERE name LIKE '%".$name."%' LIMIT 1, 9";
MySQL will search the entire set first and then only return the specified LIMIT. LIMIT does not search only the first 9 posts...
Use LIMIT 0,9 or LIMIT 9 OFFSET 0
In your case,
$pageSearch = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$pageOffset = $pageSearch - 1;
$limitQuery = "LIMIT $pageOffset , 9 ";
//lets say there are 10 items selected with this query
$searchQuery = "SELECT name,image,price,description FROM image WHERE name LIKE '%".$name."%' " . $limitQuery;
You have to use the LIMIT for that. You have to use LIMIT like LIMIT ".($pageSearch-1).", ".$perPage that's all.... Do as below:
$searchQuery = "SELECT name,image,price,description FROM image WHERE name LIKE '%".$name."%' LIMIT ".($pageSearch-1).", ".$perPage;

PHP MySQL Results num total rows and split for pages

Is there any way to count and split results without doing 2 query,
im using a query something like this:
$result = mysqli_query($con,"SELECT * from articles WHERE category = '$category'");
$row = mysqli_fetch_row($result);
$rows = $row[0];
$page_rows = 20;
$last = ceil($rows/$page_rows);
$pagenum = 1;
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result2 = mysqli_query($con,"SELECT * FROM articles order by id desc $limit");
while($row = mysqli_fetch_array($result2)) {
$id = $row['id'];
}
this is working but i dont like that it has 2 queries, any better idea? thank you
$pagenum = 1;
$rows_on_page = 20;
$start = (($pagenum - 1) * $rows_on_page);
$end = ($pagenum * $rows_on_page);
$result = mysqli_query($con, "SELECT * from articles WHERE category = '$category' ORDER BY id DESC LIMIT $start, $end");
while ($row = mysqli_fetch_array($con,$result) {
... do stuff with articles ...
$pagenum++;
}
The while loop will protect you from going past the end of the records.
try this query,it will return count of records and pagination (title in query is a field name, change it based on your table):
SELECT aa.countt, title FROM articles , (SELECT COUNT(*) AS countt FROM articles WHERE category = '$category' ) AS aa ORDER BY id LIMIT 5,10

PHP/MySQL using ORDER BY with a LIMIT

I created a pagination by roughly following this link:
http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en#toggle
quite cool. Although I have an issue with my query.
It works fine like this:
require 'includes/function.php';
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 8;
$startpoint = ($page * $limit) - $limit;
$statement = "cars WHERE deleted = 'no'";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ");
while ($row = mysql_fetch_assoc($query)) {
However when I try to add an ORDER BY to this, like so:
require 'includes/function.php';
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 8;
$startpoint = ($page * $limit) - $limit;
$statement = "cars WHERE deleted = 'no'";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ORDER BY model DESC");
while ($row = mysql_fetch_assoc($query)) {
or just change the statement like this:
$statement = "rcr_cars WHERE deleted = 'no' ORDER BY model DESC";
I get this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in filepath/myfile.php on line 79.
Line 79 is this line:
while ($row = mysql_fetch_assoc($query)) {
Can anyone tell me how I am not using the ORDER BY correctly, its got me puzzled. :/
Try the query as below
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");
Where you have gone wrong is LIMIT should come after ORDER BY. Read more
Change the query:
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit}") ;
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");

Categories