sql query returning only one row - php

this code is returning only one row from the table , i am unable to figure it out
<?php
$sqlrest="Select `restaurant_name` , `restaurant_id` , `menuimage` from `restaurant` where `restaurant_id` = '".mysql_real_escape_string($_REQUEST['res_id'])."'";
$result=mysql_query($sqlrest) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$c=$row['restaurant_name'];
$id=$row['restaurant_id'];
<tr>
<td><?php echo $c ; ?></td>
<td><img src="image/<?php echo $resid=get::getimage($id); ?>" width="100" height="100" /></td>
<td>Edit Restraunt Details</td>
<td>Delete</td>
</tr>
<?php } ?>
function code that is being used
function getimage($catid) {
$image="select * from `restimage` where `res_id`='$catid' ";
$imageresult=mysql_query($image) or die(mysql_error()) ;
while($row=mysql_fetch_assoc($imageresult)){
return $getimage=$row['rest_image'];
}
}

If You want all images from one restaurant. Your Image Function should return all images and return it.
Maybe the whole img Tag.
function getimage($catid) {
$image="select * from `restimage` where `res_id`='$catid' ";
$imageresult=mysql_query($image) or die(mysql_error()) ;
$images = array();
while($row=mysql_fetch_assoc($imageresult)){
$images[] = '<img src="image/'. $row['rest_image'] .'" width="100" height="100" />';
}
return implode('', $images);
}

Related

How to fetch images from database in base64_encode in php

Thank you all in advance....
I am doing a project in which I have a field to store the image. In that form, the image uploaded is completed with croppie plugin. and I stored the data in base 64 encodings. But I cannot fetch it to a page called view.php but the images are fetched to the form after cropping.
Please help me to figure it out the mistake that I have done
<tbody>
<?php
$no = 1;
$data = mysqli_query($con, "SELECT * FROM `register` WHERE app_registration IS NULL ORDER BY `app_id` DESC ");
while ($row = mysqli_fetch_assoc($data)) {
?>
<tr>
<td><?php echo $row['app_id'] ?></td>
<?php if (!empty($row['image_reference_id'])) {
$data1 = mysqli_query($con, "SELECT * FROM `photo_table` WHERE image_unique_id = '" . $row['image_reference_id'] . "'");
$row1 = mysqli_fetch_assoc($data1)
?>
<td><img src="data:image/jpg;base64,'.base64_encode($row['images']).'"/></td>
<?php } else {
?>
<td><img src="../images/<?php echo $row['app_image'] ?>" style="width: 100px; height: 100px;"></td>
<?php } ?>
<td><?php echo $row['app_name'] ?></td>
<td><?php echo $row['app_mobile_no_1'] ?></td>
</tr>
<?php } ?>
</tbody>
Note: In db the images are in Long Blob
You will need to change the following:
<img src="data:image/jpg;base64,'.base64_encode($row['images']).'" />
to
<img src="data:image/jpg;base64, <?php base64_encode($row['images']);?>" />

Displaying an image in php

how to display blank if there is no image in a record.As i have inserted a record in database without an image but while fetching an record it is displaying an blank image in front end.It should not show any image if there is no image.Here is my code. If there is no image it should show only description.
Blogimage.php
<tbody>
<?php include "blogs.php" ;
while($row = mysql_fetch_array($result))
{?>
<tr>
<td><img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/></td>
</tr>
<tr>
<td><?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?></td>
</tr>
<?php }?>
</tbody>
Blogs.php
$id=$_GET['title'];
$res = "SELECT * FROM blogs
WHERE blog_title='$id'";
$result=mysql_query($res);
try changing this
<img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/>
to this
<?php if($row['image']) echo "<img src='admin/upload/".$row['image']."' height='100' style='width:60%;height:50%;' />"; ?>
A couple of general points before we go into this
1) The MySql library you are using is old and busted - you should be using something newer - read this - the options are Mysql PDO and MySqli - I promise you, you will be glad you did, i use IDIORM as an interface and find it very good (Also helps prevent stuff in point 2 below!)
2) Your code can be SQL injected - more about that here - this is very bad :)
Below is an example, I have fixed the SQL injection problem, and put in some logic that would test to see if there is an image, if not, it will dump out a 'noimage.jpg' src value.
I have removed the include, and just simply put the code for blogs.php inline.
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$iamgeSrc = 'admin/upload/' . $row['image'];
}
else
{
$imageSrc = 'admin/upload/noimage.jpg';
}
?>
<tr>
<td>
<img src="<?php echo $imageSrc; ?>" height="100" style="width:60%;height:50%;"/>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
If you want no image to show at all, then this would work
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
$image = '';
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$image = '<img src="admin/upload/'.$row['image'].'" height="100" style="width:60%;height:50%;"/>';
}
?>
<tr>
<td>
<?php echo $image; ?>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>

pagination always showing page first

hy, i have a problem to set the pagination using PHP and oracle database, the page only show the first page value. when i click button next, the page change from page 1 to page 2, page 3, etc, but the value still same with page 1. i dont know and i dont have any idea to fix this error..
here is my code for set up record set ..
<?php
// Set up recordset
define("ewSqlSelectCount", "SELECT count(*) count FROM sid_mst_dealer", true);
$sSql_count = BuildSqlang(ewSqlSelectCount, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sDbWhere, $sOrderBy);
//echo "$sSql_count" . "<br/ >";
$rs_count = moi_query($sSql_count , $conn) or die("Failed to execute query at line " . __LINE__ . ": " . moi_error($conn) . '<br>SQL: ' . $sSql);
//echo $rs_count;
oci_execute($rs_count);
$nTotalRecs = oci_fetch_array($rs_count);
$nTotalRecs = $nTotalRecs['COUNT'];
$rs = moi_query($sSql, $conn) or die("Failed to execute query at line " . __LINE__ . ": " . moi_error($conn) . '<br>SQL: ' . $sSql);
//echo $rs;
oci_execute($rs);
if ($nDisplayRecs <= 0) { // Display all records
$nDisplayRecs = $nTotalRecs;
}
$nStartRec = 1;
SetUpStartRec(); // Set up start record position
?>
this is the function..
function SetUpStartRec()
{
// Check for a START parameter
global $nStartRec;
global $nDisplayRecs;
global $nTotalRecs;
if (strlen($_GET[ewTblStartRec]) > 0)
{
$nStartRec = $_GET[ewTblStartRec];
$_SESSION[ewSessionTblStartRec] = $nStartRec;
} elseif (strlen($_GET["pageno"]) > 0)
{
$nPageNo = $_GET["pageno"];
if (is_numeric($nPageNo))
{
$nStartRec = ($nPageNo-1)*$nDisplayRecs+1;
if ($nStartRec <= 0)
{
// echo 'jangan ke sini';
$nStartRec = 1;
}
elseif ($nStartRec >= (($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1)
{
$nStartRec = (($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1;
}
$_SESSION[ewSessionTblStartRec] = $nStartRec;
}
else
{
$nStartRec = $_SESSION[ewSessionTblStartRec];
if (!(is_numeric($nStartRec)) || ($nStartRec == ""))
{
$nStartRec = 1; // Reset start record counter
$_SESSION[ewSessionTblStartRec] = $nStartRec;
}
}
}
else
{
$nStartRec = #$_SESSION[ewSessionTblStartRec];
if (!(is_numeric($nStartRec)) || ($nStartRec == "")) {
$nStartRec = 1; // Reset start record counter
$_SESSION[ewSessionTblStartRec] = $nStartRec;
}
}
}
and here is the query
<?php
define("ewTblVar", "sid_mst_dealer", true);
define("ewTblRecPerPage", "RecPerPage", true);
define("ewSessionTblRecPerPage", "sid_mst_dealer_RecPerPage", true);
define("ewTblStartRec", "start", true);
define("ewSessionTblStartRec", "sid_mst_dealer_start", true);
define("ewTblShowMaster", "showmaster", true);
define("ewSessionTblMasterKey", "sid_mst_dealer_MasterKey", true);
define("ewSessionTblMasterWhere", "sid_mst_dealer_MasterWhere", true);
define("ewSessionTblDetailWhere", "sid_mst_dealer_DetailWhere", true);
define("ewSessionTblAdvSrch", "sid_mst_dealer_AdvSrch", true);
define("ewTblBasicSrch", "psearch", true);
define("ewSessionTblBasicSrch", "sid_mst_dealer_psearch", true);
define("ewTblBasicSrchType", "psearchtype", true);
define("ewSessionTblBasicSrchType", "sid_mst_dealer_psearchtype", true);
define("ewSessionTblSearchWhere", "sid_mst_dealer_SearchWhere", true);
define("ewSessionTblSort", "sid_mst_dealer_Sort", true);
define("ewSessionTblOrderBy", "sid_mst_dealer_OrderBy", true);
define("ewSessionTblKey", "sid_mst_dealer_Key", true);
// Table level SQL
define("ewSqlSelect", "SELECT * FROM sid_mst_dealer", true);
if($_REQUEST[dealer_id]==""){
if($_REQUEST[x_status]!=""){
define("ewSqlWhere", " active_flag ='".$_REQUEST[x_status]."'", true);
}else{
define("ewSqlWhere", "active_flag='0'", true);
}
}else{
define("ewSqlWhere", "", true);
}
define("ewSqlGroupBy", "", true);
define("ewSqlHaving", "", true);
define("ewSqlOrderBy", "", true);
define("ewSqlOrderBySessions", "", true);
define("ewSqlKeyWhere", "dealer_id = '#dealer_id'", true);
define("ewSqlUserIDFilter", "", true);
?>
i really need help to fix this.. thank u :D
I think the problem is the lack of a limit within that obscure sql which is the key piece to the puzzle in terms of pagination. If you consider the following:
select * from `users` where `name`='fred' limit 0,10;
That would show the first 10 records where the user is called "Fred" and then
select * from `users` where `name`='fred' limit 10,10;
The second statement would show the next 10 records where the user is called "Fred"
I simply don't understand your methodology in constructing the sql but IMO you need to add a limit - and the logic associated to calculate which page in the recordset you are on so that you can add next/previous links.
here is the pagination..
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><span >Page </span> </td>
<!--first page button-->
<?php if ($nStartRec == 1) { ?>
<td><img src="images/firstdisab.gif" alt="First" width="16" height="16" border="0"> </td>
<?php } else { ?>
<td><img src="images/first.gif" alt="First" width="16" height="16" border="0"> </td>
<?php } ?>
<!--previous page button-->
<?php if ($PrevStart == $nStartRec) { ?>
<td><img src="images/prevdisab.gif" alt="Previous" width="16" height="16" border="0"> </td>
<?php } else { ?>
<td><img src="images/prev.gif" alt="Previous" width="16" height="16" border="0"> </td>
<?php } ?>
<!--current page number-->
<td><input type="text" name="pageno" value="<?php echo intval(($nStartRec-1)/$nDisplayRecs+1); ?>" size="4"> </td>
<!--next page button-->
<?php if ($NextStart == $nStartRec) { ?>
<td><img src="images/nextdisab.gif" alt="Next" width="16" height="16" border="0"> </td>
<?php } else { ?>
<td><img src="images/next.gif" alt="Next" width="16" height="16" border="0"> </td>
<?php } ?>
<!--last page button-->
<?php if ($LastStart == $nStartRec) { ?>
<td><img src="images/lastdisab.gif" alt="Last" width="16" height="16" border="0"> </td>
<?php } else { ?>
<td><img src="images/last.gif" alt="Last" width="16" height="16" border="0"> </td>
<?php } ?>
<td><span > of <?php echo intval(($nTotalRecs-1)/$nDisplayRecs+1);?></span> </td>
</tr>
</table>
<?php if ($nStartRec > $nTotalRecs) { $nStartRec = $nTotalRecs; }
$nStopRec = $nStartRec + $nDisplayRecs - 1;
$nRecCount = $nTotalRecs - 1;
if ($rsEof) { $nRecCount = $nTotalRecs; }
if ($nStopRec > $nRecCount) { $nStopRec = $nRecCount; } ?>
<span >Records <?php echo $nStartRec; ?> to <?php echo $nStopRec; ?> of <?php echo $nTotalRecs; ?></span>
<?php } else { ?>
<?php if ($sSrchWhere == "0=101") {?>
<span ></span>
<?php } else { ?>
<span >No records found</span>
<?php } ?>
<?php } ?> </td>
</tr>
</table>
As stated by #RamRaider your methodology for pagination is somewhat vague.
As of Oracle 12c, you could use the following example query to implement pagination.
SELECT fieldA,fieldB
FROM table
ORDER BY fieldA
OFFSET 5 ROWS FETCH NEXT 14 ROWS ONLY;
Visit this page to learn more about row limiting clause for top-N queries in Oracle Database 12c Release 1 (12.1)

Pagination always missing 1 row

I have a pagination code that fetch data from DB and show them in Table paginated, but always missing the last row added in the database, I have no idea why..
this my code ;
if(isset($_GET['page'])){
$page=$_GET['page'];
}else $page=1;
$obj=new action_a_DB();
$totall= $obj->getNmbLgForPages() ; // number of all articles.
$pagination=new pagination($totall,10 /*number of content per page*/,$page,5 /*number of button to show*/);
$start = $pagination->Start ;
$end = $pagination->End ;
// get the info from the db
$total_page = ceil($totall/10);
$result = $obj->getAllArticle($start,$end) ;
}
pagination.php
...
public function Show_Pagination($link,$get='page',$div_class_name='pagination')
{
if($this->Pages==1)return;
echo'<div class="'.$div_class_name.'">';
if($this->Page_number>1)echo 'Pre ';
else echo '<a >Pre</a> ';
//print button
$this->Buttons=(int)$this->Buttons;
$start_counter = $this->Page_number-floor($this->Buttons/2);
$end_conter = $this->Page_number+floor($this->Buttons/2);
if($start_counter<1) $end_conter=$end_conter+abs($start_counter);
if($end_conter>$this->Pages) $start_counter=$start_counter-($end_conter-$this->Pages);
if(($this->Page_number-floor($this->Buttons/2))<1)$end_conter ++;
for ($i=$start_counter;$i<=$end_conter;$i++)
{
if($i>$this->Pages || $i<1)continue; //no print less than 1 value or grater than totall page
if($i==$this->Page_number)echo ' <a class="cur">'.$i.'</a> ';
else echo ' '.$i.' ';
}
if($this->Page_number<$this->Pages)echo 'Suiv ';
else echo '<a >Suiv</a> ';
echo'</div>';
}
...
my funtions in action_a_DB.php
function getAllArticle($Start, $End){
$sql = "SELECT * FROM articles order by id desc limit $Start , $End";
$req = mysql_query($sql) or die('Req Invalide: ' . mysql_error());
//return mysql_fetch_array($req);
return $req;
}
function getNmbLgForPages(){
$query=mysql_query("SELECT count(id) from articles") or die('Erreur :'.mysql_error()); //
$totall=mysql_result($query,0);
return $totall;
}
My table
</tr>
<?php if(mysql_fetch_row($result)!=0){
while($data = mysql_fetch_array($result)) {?>
<tbody>
<tr>
<td class="first style1"><?php echo $data['titre']; ?> </td>
<td><?php echo $data['descrition']; ?></td>
<td><img src="img/edit-icon.gif" width="16" height="16" alt="" /></td>
<td><img src="img/hr.gif" width="16" height="16" alt="" /></td>
<td><img src="img/save-icon.gif" width="16" height="16" alt="save" /></td>
</tr></tbody> <?php }
?>
<tfoot><tr id="nav"><td colspan="5"><div> <?php
$pagination->Show_Pagination("index.php?param1=value1",'page','pagination');
?></div></td>
</tr>
<tr id="total"><td colspan="5"><?php echo 'Page : '.$page.' sur '.$total_page.' Nombre totales d\'articles: '.$totall.'' ; ?></td>
</tr>
<?php } else{ ?>
<tr><td align="center" colspan="5">Rien a afficher</td>
</tr></tfoot>
<?php } ?>
</table>
How can i Fix this problem ?
Thanks a lot,
My mistake was here
<?php if(mysql_fetch_row($result)!=0){
I have to use mysql_num_rows not mysql_fetch_row
So I use
<?php if(mysql_num_rows($result)!=0){
Why, When my query is executed, it returns a result set with a pointer to the first record. Then each fetch returns a line and moves the pointer to the next line or returns false if there is no next row in the result set, then the while to process all the results. In my case, the first line of my resource is used in if so it fails in the display.
Now work fine, thanks to Bovino for explain and to you all

How do I display a query in PHP table

My code
<?php
include('ConnectToDb.php');
$query = "SELECT * FROM News WHERE NewsFlag = 1 ORDER BY PostDate DESC";
$arrCount = -1;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$ID=$row['ID'];
$PostDate = $row['PostDate'];
$NewsHeader = stripslashes($row['NewsHeader'])
;
$NewsStart = stripslashes($row['NewsStart'])
;
echo "<hr>";
echo "<div>". date('j F Y',strtotime($PostDate)). "</div>";
echo "<p>";
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
if(count($array)>0) {
echo "<img src='". $array[0]. "' alt='' />";
}
echo "<h2 style='text-align:center'><u><a href='latestnews_full.php?ID=$ID'>". $NewsHeader. "</a></u></h2>";
echo "<div style='text-align:left'><h3>";
echo $NewsStart. " ......<a href='latestnews_full.php?ID=$ID'>(more)</a><br />";
echo "<div style='text-align:center'>";
echo "</div>";
echo "</h3></div>";
}
?>
displays my data nicely on four lines with date at the top, then a picture, title and then description.
However, I want to display the data as a table like this
<table style="width: 100%">
<tr>
<td colspan="2">postDate here</td>
</tr>
<tr>
<td rowspan="2">picture here</td>
<td>newsHeader here</td>
</tr>
<tr>
<td>newsStart here</td>
</tr>
</table>
I'm not sure how to echo the table cells correctly and all of my attempts so far have resulted in a white page. Could anyone please enlighten me?
I'd suggest you to make your logic separated from your presentable part. Close the PHP tag once you are ready fetching the results, assigning var's etc, then:
<table style="width: 100%">
<?php
//yourcode
//...
//...
$NewsStart = stripslashes($row['NewsStart']);
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
?>
<tr>
<td colspan="2"><?= date('j F Y',strtotime($PostDate)) ?></td>
</tr>
<tr>
<?php
if(count($array)>0) {
?>
<td rowspan="2"><img src='<?= $array[0] ?>' alt='' /></td>
<?php } ?>
<td><?= $NewsHeader ?></td>
</tr>
<tr>
<td><?= $NewsStart ?> </td>
</tr>
<?php } ?>
</table>
However, it's again not so clear, and I would suggest using a template engine. If you want I can post a code with assigning vars to Smarty and output your presentation in Smarty template

Categories