i am relatively new to php. I have a problem with displaying images in my browser ( google chrome) after retrieving blob data from mysql database.
Basically the following code works when the slashes are added in front of the echo at the bottom. However i have followed other online tutorials and the tutor has been able to display their images without the use of slashes whilst i am unable to get the image up. I just wondered what the standard rule is? Another thing to add - when i do fail to get the images up in the browser i get instead a ting thumbnail. I would really appreciate if anybody could tell me how to reliably display images. The site i wish to create is just about images. So its kind of fundamental. Thanks a lot in advance for your time.
<?php
$conn = mysql_connect ("localhost","root","arctic123");
$db = mysql_select_db ("user_images", $conn);
if(!$db) {
echo mysql_error();
}
$q = "SELECT * FROM images";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
//echo $row ['username'];
//echo "<br>";
header ("Content-type: image/jpeg");
echo $row ['logo'];
//echo "<br>";
}
}else{
echo mysql_error();
}
?>
You can't have header after any output in your code: http://php.net/manual/en/function.header.php
Best practice is to upload images to a directory and just store the image's path/file name in the database. Also makes it easier to manipulate the image, e.g. create different sizes and thumbnails with PHP. And it takes about four times less the disk space...
Hope you are not storing your images on MySQL, please if you do, desist from that detrimental act because it is going to make your database unnecessarily heavy...
I would recommend not storing your images in a database. While it is technically possible, it has serious performance concerns. Best practice would be to designate a folder for the images, then access them directly. If you'd like the filtering and sorting ablities of MySQL, then replace the BLOB column that currently stores the image data with a VARCHAR containing the file name.
<?php
$conn = mysql_connect ("localhost","root","arctic123");
$db = mysql_select_db ("user_images", $conn);
$imgdir = "/path/to/image/directory/";
if(!$db) {
echo mysql_error();
}
$q = "SELECT * FROM images";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
echo $row['username'];
echo "<br>";
echo "<img src='" . $imgdir . $row['logo'] . "' />";
echo "<br>";
}
}else{
echo mysql_error();
}
?>
Related
The main idea is being able to controll, upload and delete a slideshow's images from a database.
So, first I connect to the database like this, and with a while function I echo all the images I have stored in my db.
This is the code to do that:
<?php
$servername = "myservername";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT id, img FROM mytable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src=".$row['img'].">";
}
} else {
echo "0 results";
}
?>
In the table "mytable" I have 2 columns: an ID auto-inc and a column called img.
How can I store an image in the img column so that my function will echo all the images I have in my table?
All I found searching til now was a lot of methods that upload the image with an html form, but I'd prefer to upload my img directly on my database, without building an html+php form to upload all my images to my table.
Is that possible?
Storing entire images in a database is generally a bad idea. It is much simpler to simply store the path of the image inside your database, as has already been mentioned. You may have a bit of extra work deleting the images, as you would need a php system call to delete the image file.
If you really must store images inside a database, you can save them as base64 encoded text. I've seen this be done at a fairly successful company, but no particularly good reason was given for this. Your sql execution time will be substantially larger, because images are large files, and your table size will balloon.
I'm so sorry that that title probably makes no sense.
Basically whats going on is I have a page with images being displayed on it that were uploaded to a database from users. I want the user to be able to select one of those images which links them to another page that only displays the image they picked. Problem is that I cant get that selected image to display on the linked page.
Any idea whats going on?
// Database credentials
require("config.php");
// Connect to the database
$dbc = mysqli_connect ($db_host, $db_user, $db_password, $db_name) OR die ('Could not connect to MySQL: '. mysqli_connect_error());
// Get the image
$query = "SELECT
*
FROM
image
WHERE
image_id = '{$_GET['image']}' LIMIT 1";
$result = mysqli_query($dbc, $query) or die('Query failed: ' . mysqli_error($dbc));
$row = mysqli_fetch_assoc($result);
// Back to all images
echo "<p>Back to the gallery</p>";
// Display the chosen image image
echo "{$row['image']}";
I'm playing with something like this, but all its giving me it a broken url path icon.
echo '<img src="upload/'.$row["image"].'">';
You shd echo:
echo "<img src=".row['image']." />";
I am brand new to php/mysql, so please excuse my level of knowledge here, and feel free to direct me in a better direction, if what I am doing is out of date.
I am pulling in information from a database to fill in a landing page. The layout starts with an image on the left and a headline to the right. Here, I am using the query to retrieve a page headline text:
<?php
$result = mysql_query("SELECT banner_headline FROM low_engagement WHERE thread_segment = 'a3'", $connection);
if(!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo $row["banner_headline"];
}
?>
This works great, but now I want to duplicate that headline text inside the img alt tag. What is the best way to duplicate this queries information inside the alt tag? Is there any abbreviated code I can use for this, or is it better to just copy this code inside the alt tag and run it twice?
Thanks for any insight!
You are, as the comment says, using deprecated functions, but to answer your question, you should declare a variable to hold the value once your retrieve it from the database so that you can use it whenever your want.
<?php
$result = mysql_query("SELECT banner_headline FROM low_engagement WHERE thread_segment = 'a3'", $connection);
if(!$result) {
die("Database query failed: " . mysql_error());
}
$bannerHeadline = "";
while ($row = mysql_fetch_array($result)) {
$bannerHeadline = $row["banner_headline"];
}
echo $bannerHeadline; //use this wherever you want
?>
It is hard to help without knowing more. You are pumping the results into an array, are you expecting to only return one result or many banner_headline results? If you will only ever get one result then all you need to do is something like this:
PHP:
$result = mysql_query("
SELECT `banner_headline`
FROM `low_engagement`
WHERE `thread_segment` = 'a3'", $connection) or die(mysql_error());
// This will get the zero index, meaning first result only
$alt = mysql_result($result,0,"banner_headline");
HTML:
<html>
<body>
<!--- Rest of code -->
<img src="" alt="<?php echo $alt ?>">
On a side note, you should stop using mysql-* functions, they are deprecated.
You should look into PDO or mysqli
I have a couple of easy problems.
First I am trying to get names from database where surname='lion'. I wrote php a file but it didn't work:
$con = mysql_connect("localhost","yata_ali","password");
if (!$con){
die('error: ' . mysql_error());
}
mysql_select_db("yatanada_iBess", $con);
$degisken = mysql_query("select name from people where surname LIKE '%lion%'");
if(mysql_query){
return "$degisken";
}
mysql_close($con);
?>
I wrote this code and tried to use $degisken in my xcode project. But it didn't work.
shortly i am trying to use the names whichs surname =lion in my ios project and i know i should use url.but i couldn find the code part that return name what shall i write at the end of php code ? return or something else to use in xcode.
how can i send response in php? i wonder that. what shall i write "return $name" or something else. i know call url. but i dont know whats the full php code that i shall use
You can't use PHP in an iOS project. You'll need to write some objective-c to call a URL on a server which returns this data in some sort of format (xml? json?) and then have the iOS app parse the response.
I don't think you understand how to use the mysql_* functions in PHP. Take a look at the examples on this page for guidance: http://www.php.net/manual/en/function.mysql-query.php
$degisken= mysql_query("select name from people where surname='lion'");
if ($degisken){
while($row = mysql_fetch_assoc($degisken))
{
echo $row["name"] . "<br/>";
}
}
There are a lot of errors, in your code, but the most serious are that
(a) you are running an invalid test:
if (mysql_query){ //YOU CANNOT DO THIS
(b) You cannot return "$degisken"; because $degisken is a MySQL
resource, not a string.
(c) You should not close your mysql
connection after returning something. You don't necessarily need to
close it at all, but if you're going to, close it after the query
because anything after the return won't be evaluated (assuming the
return is triggered).
(d) If you're looking for cases where the surname='lion' then don't use wildcards in the MySQL query. where surname LIKE '%lion%' will match 'scalion','lioness','slioner', etc.
Your code should look something like this:
$con = mysql_connect("localhost","yatanada_ali","sifre");
if (!$con) {
die('error: ' . mysql_error());
}
mysql_select_db("yatanada_iBess", $con);
$degisken = mysql_query("select name from people where surname LIKE '%lion%'") or die('Error: '. mysql_error());
if (mysql_num_rows($degisken)){
//your query could return lots of results, so you may want to loop through results:
while($row = mysql_fetch_array($query)){
$name = $row['name'];
//do something with the name... I'm going to echo it.
echo $name . "<br />";
}
}
I have a mysql database with a table in the following format:
ID: 1
Date: 2010-12-19
Image: 5d61240f-7aca-d34b-19-12-10-15-36.jpg
Caption: Merry Xmas
I want to create a php script which checks through each row in this table and checks that the image is present in a gallery folder on my server. If the image is not in the gallery folder then I want to delete this row from my database. Some pointers on how to go about doing this would be very much appreciated.
Thanks!
try
<?php
define ("GALLERY_ROOT", "/path/to/gallery/" );
$mysqli = new MySQLi ($host, $username, $password, $db);
$result = $mysqli -> query ("
SELECT
id,
image
FROM
table
");
if ( $result ){
while ($row = $result->fetch_assoc()) {
if ( !is_file (GALLERY_ROOT . $row['image'] ) ){
$mysqli -> query ("
DELETE FROM
table
WHERE
id = '" . $row['id'] . "'
LIMIT 1
");
print "Deleted " . $row['id'] . "<br />";
}
}
$result -> free();
}
$mysqli -> close();
print "Congratz!! All invalid rows has been deleted!";
?>
That was a quick one, i didn't try running it actually.
Also if you have a lot of rows, then you might want to rethink about selecting all the rows at once. But again get back to me on how it works
Ok... use glob() or the DirectoryIterator, file_exists(), the mysql_ or mysqli_ functions, and 'DELETE FROM images WHERE id = ?'. :)
Hope this helps! If you supply some more information, I could give you more detailed advice. What have you tried so far?
EDIT: wait, I misread. You don't need the directory functions, as you already have the filename. dirname(__FILE__) might be useful.