MySql prepared statement is not working for SELECT statement - php

When I'm using SQL select statements with prepared statements code works fine and display content when I'm run that in my computer localhost using WAMP server.
But when I'm upload this code to my web hosting. No any result display or no any error display.But without prepared statement code works fine in the web hosting and display results. here is my codes with and without prepared statements. Please tell me why that happens?
Code with prepared statements.
<?php
for($i=0;$i <$count; $i++){
require('connection.php');
$stmt = $connection->prepare("SELECT * FROM comnt WHERE status = 'Approved' limit 1 offset ?");
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0) {
while($row = $result-> fetch_assoc()){
$pst_content = $row['content'];
$author = $row['name'];
if($i==0){
echo '<div class="item active">';
echo ' <blockquote>';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo " <p style='color:#a07936'>$pst_content</p>";
echo "<small>$author</small>";
echo ' </div>';
echo ' </div>';
echo ' </blockquote>';
echo ' </div>';
}else{
echo '<div class="item">';
echo ' <blockquote>';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo " <p style='color:#a07936'>$pst_content</p>";
echo "<small>$author</small>";
echo ' </div>';
echo ' </div>';
echo ' </blockquote>';
echo ' </div>';
}
}
}
}
?>
Code without prepared statements.
<?php
for($i=0;$i <$count; $i++){
require('connection.php');
$qry = "SELECT * FROM comnt WHERE status = 'Approved' limit 1 offset $i";
$select_cmnt = mysqli_query($connection,$qry);
while($row = mysqli_fetch_assoc($select_cmnt)){
$pst_content = $row['content'];
$author = $row['name'];
if($i==0){
echo '<div class="item active">';
echo ' <blockquote>';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo " <p style='color:#a07936'>$pst_content</p>";
echo "<small>$author</small>";
echo ' </div>';
echo ' </div>';
echo ' </blockquote>';
echo ' </div>';
}else{
echo '<div class="item">';
echo ' <blockquote>';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo " <p style='color:#a07936'>$pst_content</p>";
echo "<small>$author</small>";
echo ' </div>';
echo ' </div>';
echo ' </blockquote>';
echo ' </div>';
}
}
}
?>

Related

Please help to fix pagination problem in php how to hide same pages

I'm working on a project fetching table records from mysql database using php and implementing the pagination, Code is working fine but it's showing all pages like <1 2 3 4 5 6 7 8 9 10> But I am trying to hide some pages like <1 2 3...9 10>,
<?php
$result = $con->prepare("SELECT COUNT(id) FROM ".$table." ");
$result->execute();
$row = $result->fetch();
$total_records = $row[0];
$total_pages = ceil($total_records / $limit );
$items_offset = $limit*($page-1);
$items= $limit+5;
echo '<ul class="pagination">';
if($page==1){echo '<li class="prev">
<span class="fa fa-chevron-left"></span>
</li>';
}else{echo '<li class="prev">
<span class="fa fa-chevron-left"></span>
</li>';
}
for ($i=1; $i<=$total_pages; $i++) { if ($i==$page) {
$active= " class='active'";
echo "<li ".$active." ><a href='add-expenses.php?page=".$i."'>".$i."</a></li>";
}else{ echo "<li><a href='add-expenses.php?page=".$i."'>".$i."</a></li>";
} }
if($page==$total_pages){ echo '<li class="next"><span class="fa fa-chevron-right"></span></li>';
}else{ echo '<li class="next"><span class="fa fa-chevron-right"></span></li>'; } echo '</ul>';
?>
<div class="dataTables_info" id="datatable-default_info" role="status" aria-live="polite">
<?php
if($page==1){echo "Showing";
echo " ";
echo "1";
echo " ";
echo "to";
echo" ";
if($page*$limit>=$total_records){ echo $total_records;
}else{ echo $page*$limit;
}
echo " ";
echo "of";
echo " ";
echo $total_records;
echo " ";
echo "Entries";
}else{
echo "Showing";
echo " ";
echo $items_offset+1;
echo " ";
echo "to";
echo" ";
if($page*$limit>=$total_records){ echo $total_records;
}else{ echo $page*$limit;
}
echo " ";
echo "of";
echo " ";
echo $total_records;
echo " ";
echo "Entries";
}
?>
</div>
Please help to fix it Thanks in advance.
at for ($i=1; $i<=$total_pages; $i++) {, modify it so not all of the page is displayed

Place the data from database to bootstrap

I want to get data from the database and write them to the following bootstrap structure: 1 row - 3 columns, 1 row - 3 columns, 1 row - 3 columns.
The following bootstrap structure
<div class="row">
<div class="col-sm-2">
some content
</div>
<div class="col-sm-2">
some content
</div>
<div class="col-sm-2">
some content
</div>
</div>
I just can not figure out how to construct a cycle. Stuck on this place.
while($row = mysqli_fetch_assoc($result)) {
$counter ++;
if($counter == 1 || $counter % 3 === 0) {
echo '<div class="row">';
}
echo '<div class="col-sm-2">';
echo '<div class="row">';
echo '<p>' . $row["name"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["email"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["comment"] . '</p>';
echo '</div>';
echo '</div>';
if($counter % 3 === 0) {
echo '</div>';
}
}
//$counter should be set out here to 1...
//Start here with row don't have to worry about 1
echo '<div class="row">';
while($row = mysqli_fetch_assoc($result)){
//Other echo stuff...
//Check if 3 yet
if($counter == 3) {
//reset to 0 cause you will incrment
$counter = 0;
//end and start new div
echo '</div>';
echo '<div class="row">';
}
$counter ++;
}
//Close it up unless it is always ending on 3?
//Probably want some more checking here somehow
echo '</div>';
$counter = 0;
while($row = mysqli_fetch_assoc($result)) {
if($counter % 3 === 0) {
echo '<div class="row">';
}
echo '<div class="col-sm-2">';
echo '<div class="row">';
echo '<p>' . $row["name"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["email"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["comment"] . '</p>';
echo '</div>';
echo '</div>';
if($counter % 3 === 2) {
echo '</div>';
}
$counter++;
}
//if last row has less then 3 items then close the outer row
if($counter % 3 !== 0) {
echo '</div>';
}
JSfiddle with simulated output for 7 items
This task led to the discovery that before composing algorithms in the code, I need to draw a scheme on a piece of paper. Than test the output on a piece of paper. As a result, I found such a simple solution.
$counter = 0;
while($row = mysqli_fetch_assoc($result)) {
$counter ++;
if($counter == 1) {
echo '<div class="row">';
}
echo '<div class="col-sm-2">';
echo '<div class="row">';
echo '<p>' . $row["name"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["email"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["comment"] . '</p>';
echo '</div>';
echo '</div>';
if($counter == 3) {
echo '</div>';
$counter = 0;
}
}

Cannot retrieve value from sql query

My query not return any value.
$minprice = itemDetails("SELECT MIN(extra_night) FROM hotel_room WHERE hotel_id ='".$row[hotel_id]."'");
Below is my site
https://www.borneoecotours.com/hotels/index--.php
<?php
//$sql_query = $querystring;
$j=0;
if(mysqli_num_rows(mysqli_query($dbhandler,$querystring)) > 0){ //if there is a record
$sql_result = mysqli_query($dbhandler,$querystring)or die(mysqli_error());
while($row = mysqli_fetch_array($sql_result)){
echo '<div class="summbox">';
echo '<div class="summheader">';
echo '<div class="summtitle"><a href="view.php?id='.$row[hotel_id].'" class="hotlink">'.$row[code].' : '.stripslashes($row[name]).' ';
for ($x=1; $x<=$row[star]; $x++){echo "<span class='icon-star blue'></span>";}
echo '</a></div>';
echo '<div class="summduration">'.$row[location].', '.ucwords($row[region]).'</div>';
echo '</div>';
echo '<br clear="right" />';
echo '<div class="summcenter">';
if ($row[pic]==""){
echo '<div class="summpic"><img src="image/'.$row[hotel_id].'/profile.jpg" width="200" height="150" /></div>';
}else{
echo '<div class="summpic"><img src="../image/tours/'.$row[pic].'" width="200" height="150" /></div>';
}
echo '<div class="summdetails">'.stripslashes($row[brief]).'</div>';
$minprice = itemDetails("SELECT MIN(extra_night) FROM hotel_room WHERE hotel_id ='".$row[hotel_id]."'");
echo '<div class="summprice" style="width:250px;">RATES FROM <br />MYR <span style="font-size:20px;">'.$minprice.'</span></div>';
echo '<br clear="right" />';
echo '<div class="summfooter">';
echo 'Details '; //details
if ($row[hotel_id] != '17')
echo 'Book Now'; //enquiry
else
echo 'Book Now'; //enquiry
//echo '<div class="summbutton_03"></div>'; //booknow
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="clear">';
}
} else{
echo '<div class="alert-msg error">No record. Please search again.</div>';
}
?>
Supposed to be the $minprice will return the value form my database. Any idea?
The output should shown MYR (value from database),Example: MYR 1200
function itemDetails($query){
$sql_query = mysql_query($query);
$item = mysql_fetch_array($sql_query);
return stripslashes($item[0]);
}

show user account in php crud

I'm building a php/sql crud.
sql query select: user = user id is not working...
What sql statement do i use to show only the users account info?
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($users as $user) {
echo '<tr>';
echo '<td>';
echo $user['id'];
echo '</td>';
echo '<td>';
echo $user['username'];
echo '</td>';
echo '<td>';
echo '<a href="detail.php?id=';
echo $user['id'];
echo '">';
echo 'detail';
echo '</a> ';
echo '<a href="bewerk.php?id=';
echo $user['id'];
echo '">';
echo 'bewerk';
echo '</a> ';
echo '<a href="delete.php?id=';
echo $user['id'];
echo '">';
echo 'delete';
echo '</a>';
echo '</td>';
echo '</tr>';
}
?>

Compare MySQL result in PHP

I had retrieve some result from database and want to display the result separately.
If the pb_title is same as temp_title then put it togather in a red color div
else is pb_title is not same as temp_title then put it in grey color div
Here my tried code :
$select_brand = "SELECT * FROM tblProduct_Brand WHERE pb_display='display'";
$result = mysqli_query ($mydatabase, $select_brand);
if($result)
{
while($row = mysqli_fetch_array($result))
{
$pb_feature_image = substr(($row['pb_feature_image']),3);
$pb_logo = substr(($row['pb_logo']),3);
$temp_result;
if($temp_result == $row["pb_title"])
{
//if product brand title is same, put it in same area
echo '<div style="padding:20px; background-color:red;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"];
}
else
{
//if not same, put in another area
echo '<div style="padding:20px; background-color:grey;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
}
}
Any mistake about the code please correct me.
Thanks.
The following code assumes that you did something like this with the data you fetched from the sql query:
$data = array();
while($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
Which returns an array like this:
Array (
[0] => Array(
'pb_title' => '',
'pb_content' => '',
//...etc
),
//.....etc
)
Now assuming that I understand your question correctly, you'd want to do something like this:
$i = array();
$key = 'A';
foreach ($data as $item) {
if ($item['pb_title'] == $key) {
$i['red'][] = $item;
} else {
$i['grey'][] = $item;
}
}
Which gives you an array of red (Matching) and grey (Not Matching).
Which you can loop through and generate your div's appropriately.
Something like this (pseudo code)
echo '<div style="padding:20px; background-color:red;">';
foreach($i['red'] as $row) {
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
}
echo '</div>';
Example
$temp_result;
if($temp_result == $row["pb_title"])
Before checking the condition you should have assigned some value to $temp_result , but you haven't and you are comparing it anyway.
EDIT :
What I understand is that initially you have not set any value of $temp_result and it is always coming in your Else Block where you do'not assign $temp_result = $row["pb_title"]; , So that it shoudl store the previous value , you only did it in the If Block , you need to place the same code in your else block too..
Simple Edit this section in your code
else
{
//if not same, put in another area
echo '<div style="padding:20px; background-color:grey;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"]; // added this to save previous result
}
Make sure that programing should be effective for that you need to have following ideas:
1) Here you no need to use if($result) because when the sql results are true then only it will allow inside the condition
2) While programing try to avoid more if else condition for making efficient.
<?php
$select_brand = "SELECT * FROM tblProduct_Brand WHERE pb_display='display'";
$result = mysqli_query ($mydatabase, $select_brand);
while($row = mysqli_fetch_array($result))
{
$pb_feature_image = substr(($row['pb_feature_image']),3);
$pb_logo = substr(($row['pb_logo']),3);
$temp_result;
if($temp_result == $row["pb_title"]) { $style = 'style="padding:20px; background-color:red;"'; }
else { $style = ' style="padding:20px; background-color:grey;"'; }
echo '<div '.$style.' >';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"];
}
?>

Categories