Only Half Image Retrieved from Blob Being Shown - php

When I retrieve a blob via PHP it only loads about half the image.
Here's my code:
<?php
require 'dbconnect.php';
$q="SELECT * FROM mtgcards WHERE Name LIKE '%".$_POST['search']."%'";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r)) {
echo '<br>'. 'Name: ' . $row['Name'] . ' Mana cost: ' . $row['Mana Cost'] . ' Colour: ' . $row['Colour'] . ' Set: ' . $row['Set'] . ' Ability: ' . $row['Ability']. '<img src="data:image/jpeg;base64,' . base64_encode($row['Image']) . '" width="223" height="311">';
}
mysqli_close($dbc);
?>
Here's what the full picture is and what the picture when I retrieve it comes out as.
Thanks in advance!

As it seems and was determined in comments, BLOB is too small a column type for the image's (raw) size, therefore you will need to use LONGBLOB.
Reference:
http://dev.mysql.com/doc/refman/5.7/en/blob.html
So you will need to alter your column, and resave your data then start over.

Related

SQL display one entry from table one, then display all the entries corresponding, no repeats?

This is my first time using SQL, I'm using PHPMyAdmin. My connection to the database works fine.
I have 8 database tables, but I'm currently just trying to display the data from 5 of them. This is my ER model. I want to display city, place, photo and cat tables in an RSS feed.
Currently when load up the XML it repeats some of the entries, for example for each place it repeats the city information. So for place 1 it shows all the city data, then the place data, and the same for place 2 ect.This is what the first few entries for the current output looks like, it's a long XML file due to the repeats.
I want the city information to be displayed once, then the corresponding place data to be displayed after. So City 1, then place 1,2,3,4,5,6 (as they correspond to city 1), then City 2, then place 7,8,9,10 (as they correspond to city 2).
This is what my current select query looks like :
SELECT s.city_id, s.city_name, s.country_name, s.population,
s.city_long, s.city_lat, s.woeid, s.currency, s.region,
s.time_zone, s.area, s.postal_code, n.place_name, n.capacity,
n.place_long, n.place_lat, n.city_id, c.place_id, c.category_id,
ss.category_id, ss.category_name, ss.category_icon, cc.photo_id,
cc.place_id, cc.photo_name
FROM city s
JOIN place n ON s.city_id = n.city_id
JOIN place_cat c ON c.place_id = n.place_id
JOIN cat ss ON ss.category_id = c.category_id
JOIN photo cc ON cc.place_id = n.place_id
I don't know if it's an issue with the query or with the way I'm echoing the data. This is how I'm echoing it :
foreach($results as $result)
{
//
echo '<item>'. PHP_EOL;
echo '<city>'. PHP_EOL;
echo '<CityName>' . $result['city_name'] . '</CityName>'. PHP_EOL;
echo '<CountryName>' . $result['country_name'] . '</CountryName>'. PHP_EOL;
echo '<Population>' . $result['population'] . '</Population>'. PHP_EOL;
echo '<CityLat>' . $result['city_lat'] . '</CityLat>'. PHP_EOL;
echo '<CityLong>' . $result['city_long'] . '</CityLong>'. PHP_EOL;
echo '<Woeid>' . $result['woeid'] . '</Woeid>'. PHP_EOL;
echo '<Currency>' . $result['currency'] . '</Currency>'. PHP_EOL;
echo '<Region>' . $result['region'] . '</Region>'. PHP_EOL;
echo '<TimeZone>' . $result['time_zone'] . '</TimeZone>'. PHP_EOL;
echo '<Area>' . $result['area'] . '</Area>'. PHP_EOL;
echo '<PostalCode>' . $result['postal_code'] . '</PostalCode>'. PHP_EOL;
echo '<place>'. PHP_EOL;
echo '<PlaceName>' . $result['place_name'] . '</PlaceName>'. PHP_EOL;
echo '<Capacity>' . $result['capacity'] . '</Capacity>'. PHP_EOL;
echo '<PlaceLat>' . $result['place_lat'] . '</PlaceLat>'. PHP_EOL;
echo '<PlaceLong>' . $result['place_long'] . '</PlaceLong>'. PHP_EOL;
echo '<Category>' . $result['category_name'] . '</Category>'. PHP_EOL;
echo '<Photo>' . $result['photo_name'] . '</Photo>'. PHP_EOL;
echo '</place>'. PHP_EOL;
echo '</city>'. PHP_EOL;
echo '</item>'. PHP_EOL;
}
Is there a way to display the data the way I want to (city information to be displayed once, then the corresponding place data to be displayed after)? If so how?

How to format mass data pulled from a database?

I am using a PHP script to pull large amounts of entries from a database, it looks like this:
foreach($events as $e){ //sets each set of data as a value of $e
$vidID = $e["Test Video YouTube ID"]; //stores each video ID as this variable, so that it can be called when embedding the video
echo "<br>" . '<iframe width="250" height="140" src="http://www.youtube.com/embed/' . $vidID . '?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>' . "<br>"
. '<strong> DOC: </strong>' . $e["Axial Cut"] . " " . '<strong> IPM: </strong>' . $e["Inches Per Minute"] . "<br>" .
'<strong> WOC: </strong>' . $e["Radial Cut"] . " " . '<strong> FPT: </strong>' . $e["Inch per Tooth"] . "<br>" .
'<strong> SFM: </strong>' . round($e["Surface Feet per Minute"]) . "<br>"; //each value here is displayed. use the form '$e["Column to be dislplayed"].'
This displays each field for the entry I need, then goes to the next entry and does the same, displaying every entry in the database.
My issue is that it only displays them in a vertical line, so what is the best way to format the output in such a way that it displays in a grid, with a set amount of columns?
*EDIT - To clarify, I don't want to use something like a CSS table for this. It limits me in that the data cannot automatically fill into columns, instead each entry would either appear in a single column or I would have to update each field every time the database had a new entry which would be quite tedious. So what is the best way to have data automatically flow into these columns and rows?

Error When Trying to Display Result

When I try to display results from a Mysql query with PHP, I get the following error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\results.php on line 5
Here's my source from results.php:
<?php
require 'dbconnect.php';
$q="SELECT Name, Mana Cost, Colour, Set, Ability FROM mtgcards WHERE Name LIKE '%".$_POST['search']."%'";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r)) {
echo '<br>'. 'Name: ' . $row['Name'] . ' Mana cost: ' . $row['Mana Cost'] . ' Colour: ' . $row['Colour'] . ' Set: ' . $row['Set'] . ' Ability: ' . $row['Ability'];
}
mysqli_close($dbc);
?>
I have no clue what my issue. I have double-checked my table names, and I have double checked other syntax issues. Please help me, thanks!
You need to properly quote columns names which contain space:
$q="SELECT Name, `Mana Cost`, Colour, Set, Ability FROM mtgcards WHERE Name LIKE '%".$_POST['search']."%'";

PHP while loop multiple conditions returning no data?

I have a PHP page that is returning data from a MySQL table. I decided to add a feature that would allow the user to set the number of results they want displayed, in case there are thousands of records for example.
Here is the PHP code that displays the data:
while ($row = mysql_fetch_array($result) && ($row <= $noOfResults)) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
I was hoping that this would only display the data up to the point that the user has selected. So if the user selects 10 results, $noOfResults is set to 10, and it will only fetch the first 10 results from the database. This is currently however only displaying a "-" and the $noOfResults variable (which is desired at this point). Is my syntax wrong or is this not the correct method of going about such a problem?
Thanks, got it working with LIMIT, didn't even think to do that.
Can someone explain why this was downvoted, just trying to learn, so as to write better questions in the future, thanks.
the best way to get limited data from database is LIMIT statement in query .
i assume that your query is
$result= "SELECT * FROM `mytable`";
Just add limit statement in
$result= "SELECT * FROM `mytable` LIMIT '$noOfResults'";
then your while loop will be
while ($row = mysql_fetch_array($result) ) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
You are doing it wrong $row will be an array. So to correct this use mysql_num_rows() function in php to check the number of the rows. Try the code below.
if(mysql_num_rows($result)<=$noOfResults){
while ($row = mysql_fetch_array($result) ) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
}
Hope this helps you

How to loop though data in the database while only querying columns with certain values?

This is part of an issue that I had half of resolved in a previous question but this is another question entirely.
My previous question to give you a basis for what I am trying to achieve:
Using MySQLI how can I display data from my database on my website
NOTE: Column 17 in the table has a value of a number ranging 1-10. These are 10 different sets of information. With the code below I am only interested in query and displaying the results in the table that have the value of 1 in that specific column
How can I use the following code or modify it so that ONLY rows/items in the table that have a value of 1 (or w/e value i choose to run the script with) will be displayed as results when the loop iterates through.
IE: there are 5 columns one, two, three, four, and blooean. All the values in the boolean column are either true or false. I may only want to display results that have a true value. But instead used with my code below and have a vlue of 1.
My Code:
<?php while($row = $result->fetch_assoc()) {
echo
'<div class="list-item-container">'
. '<img class="favicon" src="http://www.google.com/s2/favicons?domain='. $row['favicon_url'] . '" />'
. '<div class="list-item"><a title="' . $row['affiliate_name_title'] . '" href="' . $row['affiliate_url'] . '">' . $row['affiliate_name'] . '</a><span class="tld">' . $row['tld'] . '</span><span class="list-item-note"> ' . $row['country'] . '</span></div>'
. '<div class="unverified_icon"><img title="' . $row['verified_title'] . '" src="img/icons/' . $row['verified_image'] . '" /></div>'
. '<div class="hover-img"><img class="games-icon" src="img/icons/games.png" alt="" title="' . $row['games_title'] . '"/><img class="payment-icon" src="img/icons/payment.png" alt="" title="' . $row['payment_title'] . '"/><img src="blank.gif" class="flag ' . $row['country_image'] . '" alt="Czech Republic" title="' . $row['flag_title'] . '"/></div>'
. '</div>';
}
?>
As always, thanks!
EDIT: No one seems to understand what I mean. Sorry, not sure how else to explain/ask it.
I am running this same script in a lot of different locations on the same page.
On each location of the page I want to run the same script
Each place I run it I would like to be able to display only certain items from the table based on a option(not sure what its called) i give it. In div_1 i run the script and I need to display all results with a column 17 value of 1.
In div 2 I run the script but want to display only results/values from the table that have a column 17 value of 2, etc, etc,.
Why not do it in your query ahead, like:
$query = "SELECT * FROM yourTable WHERE value=1";
Or using your own code, it would be:
<?php
// Connect to and select a database
$db = new mysqli('localhost', 'root', '', 'DATABASE');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
// Query the table 'TABLES' (I'm also assuming I only need to query just the table from here)
$sql = <<<SQL
SELECT *
FROM `TABLE`
WHERE `Column17` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()) {
echo
'<div class="list-item-container">'
. '<img class="favicon" src="http://www.google.com/s2/favicons?domain='. $row['favicon_url'] . '" />'
. '<div class="list-item"><a title="' . $row['affiliate_name_title'] . '" href="' . $row['affiliate_url'] . '">' . $row['affiliate_name'] . '</a><span class="tld">' . $row['tld'] . '</span><span class="list-item-note"> ' . $row['country'] . '</span></div>'
. '<div class="unverified_icon"><img title="' . $row['verified_title'] . '" src="img/icons/' . $row['verified_image'] . '" /></div>'
. '<div class="hover-img"><img class="games-icon" src="img/icons/games.png" alt="" title="' . $row['games_title'] . '"/><img class="payment-icon" src="img/icons/payment.png" alt="" title="' . $row['payment_title'] . '"/><img src="blank.gif" class="flag ' . $row['country_image'] . '" alt="Czech Republic" title="' . $row['flag_title'] . '"/></div>'
. '</div>';
}
?>

Categories