Images from database column not loading - php

CODE:
<?php
$page = $_GET['page'];
$category = $_GET['cat'];
$your_db = # new mysqli("database","name","password");
if (mysqli_connect_errno())
{
echo 'ERROR!
'.mysqli_connect_errno()
.' - Not connected : '.mysqli_connect_error().'
';
die;
}
else
{
$db_connect = $your_db->select_db("databasename");
if (!$db_connect)
{
echo 'ERROR CONNECT DATA BASE';
die;
}
}
echo '<p>';
$query = "select distinct fldCity from info order by fldCity";
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
for ($i = 0; $i < $number_of_records; $i++)
{
$row = $result->fetch_assoc();
echo '<a href="index.php?cat='.stripslashes($row['fldCity']).'">';
echo stripslashes($row['fldCity']);
echo '</a> / ';
}
echo '</p>';
if ($category)
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where fldCity = '$category' and length(fldCompanyLogo) > 0 order by fldCity";
}
else
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where length(fldCompanyLogo) > 0 order by fldCity";
}
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
$num_pages = $number_of_records / 7;
if (($number_of_records % 7) > 0 )
{
$num_pages++;
}
if (strlen($page) == 0)
{
$page = 0;
}
else
{
$page = $page * 4;
}
echo '<table border="0" cellspacing="10" cellpadding="10" style="border-collapse: collapse" bordercolor="#111111">';
$row_num = 4;
$result->data_seek($page);
for ($i = $page; $i < $number_of_records; $i++)
{
if ($row_num <= 4)
{
echo '<tr>';
for ($col_num = 0; $col_num < 4; $col_num++)
{
$row = $result->fetch_assoc();
echo '<td>';
show_image(stripslashes($row['fldCompanyLogo']),stripslashes($row['fldCompanyName']));
echo '
';
echo '<b><font face="Tahoma" size="2"><a href="mysite.ca/'.stripslashes($row['fldMenuLink']).'" target="_blank"></font>';
echo '<font face="Tahoma" size="2"><a href="'.stripslashes($row['fldMenuLink']).'" target="_blank">';
echo stripslashes($row['fldCompanyName']);
echo '</a>';
echo '
';
echo stripslashes($row['fldCity']);
echo '
';
echo stripslashes($row['fldProvince']);
echo '
';
echo stripslashes($row['fldPostalCode']);
echo '</td>';
}
$row_num++;
echo '</tr>';
}
else
{
break;
}
}
echo '</table>';
for ($j = 0; $j < $num_pages; $j++)
{
$page_link = $j + 1;
echo '<font face="Tahoma" size="4"><b>'.$page_link.'</b></font> ';
}
echo ' '.$number_of_records;
function show_image($image_name)
{
if (file_exists("'$image_name'"))
{
$dim_img = getimagesize('/images/'.$image_name);
echo '<img src="/images/'.$image_name.'" alt = '.$alt.' border=0 align="bottom"';
echo 'width = '. $dim_img[100] .' height = ' .$dim_img[100] . ' />';
}
else
echo 'Add your image here!';
}
?>
I am totally lost and I can't find the error to why the images from the database isn't loading with the script. I highlighted the area in blue where I believe the error is to be. I just can't find any errors in that particular spot! All I want is the images from a column I have in my database to be fetched and connected to 'echo '
No images are being displayed, and I can't find the error as to why the images aren't showing up

try Convert Image to Base64 String and Base64 String to Image
also why echo ''; you use this its makes no sense

Related

I can't substitude values to a placeholder of SQL statement

I tried to separate SQL's data with the following code:
$count = $pdo->query('SELECT COUNT(*) FROM tweets');
foreach ($count as $row) {
$num = $row[0];
echo 'count:'.$row[0].'<br>';
}
echo 'num value: '.$num.'<br>';
$max_page = ceil($num / 10);
echo 'page: '.$max_page.'<br>';
if (!isset($_GET['page'])) {
$_GET['page'] = 1;
} else {
$now = $_GET['page'];
echo '$_GET has been set successfully<br>';
}
$disp_start = $num - 10 * ($now - 1);
echo 'disp_start value: '.$disp_start.'<br>';
$disp_end = $num - 10 * $now;
echo 'disp_end value: '.$disp_end.'<br>';
$disp = $pdo->prepare('SELECT * FROM tweets ORDER BY id DESC LIMIT 10 OFFSET ?');
//$disp -> bindValue(':st', $disp_start);
//$disp -> bindValue(':en', $disp_end);
if ($disp->execute([$disp_end])) {
echo 'success!';
foreach ($disp as $row) {
echo '<div class="breakwater2">';
echo '<div class="tweet">';
echo '<a href="./user/'.$row['uploader'].'.php">';
echo '<img src="', $row['avatar'], '" class="avatar1">';
echo '<div class="cont">';
echo '<b class="username">', $row['uploader'], '</b></a>';
echo '<p class="contents1">', $row['contents'], '</p>';
echo '<p class="time">', $row['time'], '</p>';
echo '</div>';//cont
echo '</div>';//tweet
echo '<form action="" method="post"><input type="hidden" name="del" value='.$row['id'].'>';
if ($_SESSION['user']['id'] == $row['userid']) {
echo '<input type="submit" value="Delete">';
}
echo '</form>';
echo '</div>';
echo '<hr class="division">';
}
} else {
echo 'failure';
print_r ($disp -> errorInfo());
}
for ($i = 1; $i <= $max_page; $i++) {
if ($i == $now) {
echo $now;
} else {
echo ''.$i.'';
}
}
However, when I execute this code an error occurs and it says
failureArray ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''53'' at line 1
But, if I don't use a placeholder and substitute a value directly to the 'OFFSET' value, it works. How can I solve it?

PHP - Search results not paginating

I'm trying to get search results to paginate if there are greater than 10 items found in the database. For some reason, even though the code recognises there are more than 10 items and creates links for subsequent pages, all search results are listed on the first page only. Anyone able to help please? Code is below:
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
for($i = 0; $i < $search_results_count; $i++)
{
$query = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "' LIMIT " . $search_page_first_result . ", 10");
while($row = $query->fetch_array())
{
echo "<h4>" . $row['prod_name'] . "</h4><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p> </p><p> </p>";
}
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
}
Ok so I found a solution to this problem and the full function is now as follows:
public function product_search($search_terms, $search_page, $search_flag_check)
{
if($search_flag_check == "invalid")
{
echo $this->err_handle->fetch_error_text("invalid_ns_term");
return;
}
if($search_terms == "")
{
echo $this->err_handle->fetch_error_text("no_search_string");
return;
}
$search_terms = htmlspecialchars($search_terms);
$search_terms = $this->mysqli_link->real_escape_string(filter_var($search_terms, FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES));
$search_terms_array = explode(" ", $search_terms);
$terms_count = count($search_terms_array);
$zerocount = 0;
$search_id_results = array();
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
$search_page_results_limit = 10;
if($search_page_first_result < 1)
{
$search_page_first_result = 1;
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
$search_page_upper_limit = $search_page_first_result + 9;
if(array_key_exists($search_page_upper_limit, $search_results))
{
$search_results_limit = $search_page_first_result + $search_page_results_limit;
}
else
{
end($search_results);
$search_results_limit = key($search_results);
reset($search_results);
}
for($i = $search_page_first_result; $i <= $search_results_limit; $i++)
{
$query2 = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "'");
$row = $query2->fetch_array();
echo "<h4>" . $row['prod_name'] . "</h4><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p> </p><p> </p>";
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
}
}
It took a bit of thinking and I was about to give up, but then I thought about using the array keys to calculate the limits for each search page and after a bit of googling on relevant PHP array functions it all fell into place quite well.
I understand the code may not be very tidy right now and there may be ways to optimize/improve it, however for the time being it does the job.

PHP get results from mysql in a for() loop

I have a 'map' kind of thing for something im working on. Im drawing each tile out and then checking against a database to see if somebody is located at that tile.
The code works but only for the first result in the db.
Can anyone help. many thanks.
$sqlw = "SELECT id, player_coord_x, player_coord_y FROM player_game WHERE world_id='$world'";
$world_result = $player_stat->query($sqlw);
?>
<div class='map-grid'>
<?
$id = '';
$size = 16;
for($i = 1; $i <= $size; $i++) {
echo "<div class='map-grid-row'>";
for ($j=1; $j <= $size; $j++) {
// check for player at location
if ($world_result->num_rows > 0) {
while($w_row = $world_result->fetch_assoc()) {
$player_coord_x = $w_row['player_coord_x'];
$player_coord_y = $w_row['player_coord_y'];
$id = $w_row['id'];
}
}
if ($player_coord_x == $i and $player_coord_y == $j){
echo "<div class='map-grid-cell high'>";
echo "XXX";
echo "</div>";
}else{
echo "<div class='map-grid-cell high'>";
echo "<span class=\"map-small\">(x-$i y-$j)</span>";
echo "</div>";
}
}
echo "</div>";
}
?>
</div>
$sqlw = "SELECT id, player_coord_x, player_coord_y FROM player_game WHERE world_id='$world'";
$world_result = $player_stat->query($sqlw);
if($world_result->num_rows > 0){
while($w_row = $world_result->fetch_assoc()){
//first type of array
$player[] = array(
'x' => $w_row['player_coord_x'], //your player X record
'y' => $w_row['player_coord_y'], //your player Y record
'id' => $w_row['id'] //your player id
);
//second type of array
// OR store player's X and Y as key of array
$player[$w_row['player_coord_x'] . '-' . $w_row['player_coord_y']] = $w_row['id'];
// This are only if the player record will never repeat
}
}
?>
<div class="map-grid">
<?php
$size = 16;
for($i = 1; $i <= $size; $i++){
echo '<div class="map-grid-row">';
for($j = 1; $j <= $size; $j++){
//with first type of array
$exists = false;
foreach($player as $play){
if($play['x'] == $i && $play['y'] == $j){
$exists = true;
break; // break the loop once you got the result.
}
}
if($exists){ //Player exists? if no detect in foreach loop above, default exists would return false.
echo '<div class="map-grid-cell high">';
echo 'XXX';
echo '</div>';
} else {
echo '<div class="map-grid-cell high">';
echo '<span class="map-small">(x-' . $i . ' y-' . $j . ')</span>';
echo '</div>';
}
//second type of array
//Check if the key exists and if it's empty.
if(isset($player[$i.'-'.$j]) && !empty($player[$i.'-'.$j])){
echo '<div class="map-grid-cell high">';
echo 'XXX';
echo '</div>';
} else {
echo '<div class="map-grid-cell high">';
echo '<span class="map-small">(x-' . $i . ' y-' . $j . ')</span>';
echo '</div>';
}
}
}
?>
</div>

PHP mysql display query in 4 columns

How to display this query result in 4 columns
<?php
$direction = $_POST['direction'];
$sumword = $_POST['sumword'];
$length = $_POST['length'];
$con = mysql_connect("localhost","elsha","12q(5PSZ.");
$db = mysql_select_db("elsha",$con);
$query = "SELECT answer FROM words WHERE direction = '$direction' AND sumword = '$sumword' AND sumletter = '$length'";
$result = mysql_query($query);
if(mysql_error()) {
//check that no error has occurred first; take this out in production or make more graceful handling
die(mysql_error());
}
if(mysql_num_rows($result) == 0) {
echo "No Results";
} else {
while($row = mysql_fetch_array($result)) {
echo '<img src="/images/'. $row['0'].'.jpg"><br> '. $row['0'].'<br>';
}
}
?>
like this :
result1 result2 result3 result4
result5 result6 result7 result8
Try this
$i = 0;
while($row = mysql_fetch_array($result)) {
echo '<img src="/images/'. $row['0'].'.jpg"> ';
$i++;
if($i % 4 == 1 && $i!=1){
echo '<br>';
}
}
Use flag like
$flag=0;
while($row = mysql_fetch_array($result)) {
if(($flag%4)==0) {
echo '<tr>';
}
echo '<td><img src="/images/'. $row['0'].'.jpg"><br> '. $row['0'].'</td>';
if(($flag%4)==3) {
echo '</tr>';
$flag=-1;
}
$flag++;
}
replace your while with the following code. try this
$a=1;
while($row = mysql_fetch_array($result)) {
if($a%4==0) {
echo '<img src="/images/'. $row['0'].'.jpg">'. $row['0'].'<br>';
} //4th line with break
else {
echo '<img src="/images/'. $row['0'].'.jpg"> '. $row['0'].' '; // prints first 3 lines
}
$a=$a+1;
}
I think you are trying to display the image name too below the image. Try this:
$i = 1;
while($row = mysql_fetch_array($result)) {
echo '<div class="imgs">';
echo '<img src="/images/'. $row['0'].'.jpg"><br> '. $row['0'];
echo '</div>';
if ($i === 4) {
echo '<div class="clear"></div>';
$i = 1;
} else {
$i++;
}
}
and then style div's
.imgs {
float: left;
/* other styles */
}
.clear {
clear: both;
}

PHP paging from MySQL

I have problem with my paging PHP script.
First page works perfectly, but next pages are blank.
Config.php include database setting like host etc.
Please help me solve my problem.
Thanks in advance.
Here is my code:
include 'config.php';
mysql_connect($iplogow, $userlogow, $haslologow) or die("Mysql error: " . mysql_error());
mysql_select_db($bazalogow)or die("Błąd bazy danych: " . mysql_error());
echo '<br>
<table class="table table-bordered table-striped" width="500px">
<thead>
<tr>
<th>table1</th>
<th>table2</th>
<th>table3</th>
<th>table4</th>
<th>table5</th>
<th>table6</th>
<th>table7</th>
</tr></thead>';
$result = mysql_query("SELECT Count(id) FROM `logi`");
$row = mysql_fetch_row($result);
$count_users = $row[0];
$per_page = 10;
$pages = ceil($count_users / $per_page);
$current_page = !isset($_GET['page']) ? 1 : (int)clear($_GET['page']);
if($current_page < 1 || $current_page > $pages) {
$current_page = 1;
}
if($count_users > 0) {
$result = mysql_query("SELECT * FROM `logi` ORDER BY `id` DESC LIMIT ".($per_page*($current_page-1)).", ".$per_page);
while($row = mysql_fetch_assoc($result)) {
echo '<tr>
<td>'.$row['nick'].'</td>
<td>'.$row['ip'].'</td>
<td>'.$row['password'].'</td>
<td>'.$row['productid'].'</td>
<td>'.$row['client'].'</td>
<td>'.$row['date'].'</td>
<td>'.$row['hour'].'</td>
</tr>';
}
} else {
echo '<tr>
<td colspan="3" style="text-align:center">Niestety nie znaleziono żadnych ataków.</td>
</tr>';
}
echo '</table>';
if($pages > 0) {
echo '<p>';
if($pages < 11) {
for($i = 1; $i <= $pages; $i++) {
if($i == $current_page) {
echo '<b>['.$current_page.']</b> ';
} else {
echo '['.$i.'] ';
}
}
} elseif($current_page > 10) {
echo '[1] ';
echo '[2] ';
echo '[...] ';
for($i = ($current_page-3); $i <= $current_page; $i++) {
if($i == $current_page) {
echo '<b>['.$current_page.']</b> ';
} else {
echo '['.$i.'] ';
}
}
for($i = ($current_page+1); $i <= ($current_page+3); $i++) {
if($i > ($pages)) break;
if($i == $current_page) {
echo '<b>['.$current_page.']</b> ';
} else {
echo '['.$i.'] ';
}
}
if($current_page < ($pages-4)) {
echo '[...] ';
echo '['.($pages-1).'] ';
echo '['.$pages.'] ';
} elseif($current_page == ($pages-4)) {
echo '[...] ';
echo '['.$pages.'] ';
}
} else {
for($i = 1; $i <= 11; $i++) {
if($i == $current_page) {
if($i > ($pages)) break;
echo '<b>['.$current_page.']</b> ';
} else {
echo '['.$i.'] ';
}
}
if($pages > 12) {
echo '[...] ';
echo '['.($pages-1).'] ';
echo '['.$pages.'] ';
} elseif($pages == 12) {
echo '[...] ';
echo '[12] ';
}
}
echo '</p>';
}
?>
Have you included the clear() function?
Look at line: $current_page = !isset($_GET['page']) ? 1 : (int)clear($_GET['page']);
If you have not included the function then you will get a fatal error in the execution of the script. Try to remove the clear() all together and see what happens, the changed line should be
$current_page = !isset($_GET['page']) ? 1 : $_GET['page'];

Categories