Currently I'm trying to display an image from the database i created, using phpmyadmin to test my script. The database contains images in png and jpg. But the output are just nonsense characters because the browser cannot render the image. Can some one tell me where I get wrong?
I google the problem and I can see their pattern and mine are similar so I don't really under stand where I get wrong. I can get the name of the image in database, but not the image.
this is my script:
<html>
<head></head>
<body>
<?php
$con = mysqli_connect("localhost", "Dave", "password");
if (!$con){
die ("Could not connect to database: " . mysqli_connect_error());
}
mysqli_select_db($con, "snippet");
$res = mysqli_query($con, "select * from image");
echo "<table>";
while ($row = mysqli_fetch_array($res)){
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo $row["image"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
What format is your imaged stored as?
Raw bytes?
Base-64?
or a URL?
If it's a URL then just use the attribute 'src' of the html img element.
If it's Base-64 then use
<img src="data:image/jpg;base64,THE BASE 64 code here" />
If you have raw bytes, either convert them to Base-64 with PHP
$base64segment = base64_encode($data);
or you can call a custom url within the src attribute that will lead to a PHP script where you can create an image on the fly from the bytes by sending a custom header followed by the bytes:
<?php
header('Content-type: image/jpeg');
header('Content-Length: '.strlen($data));
echo $data;
?>
The disadvantage of the Base-64 based approach is that the data takes more space than the original binary segment because the encoding is done using 64 different signs instead of 256.
With this:
<img src="<?php echo $row["image"]; ?>
You are setting the source of the image. This should be either an URL or a path from your server root. What I usually did was save the file location and name (so for example "/images/imagename.png") in the database.
If you used MIME type or BLOB in the database, this code will not work.
Have you tried this?
$base64 = 'data:image/PNG;base64,' . base64_encode($row['image']);
echo "<img src=$base64 />" ;
In your code...
while ($row = mysqli_fetch_array($res)){
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo data:image/PNG;base64,'. base64_encode($row["image"]); ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
<html>
<head></head>
<body>
<?php
$con = mysqli_connect("localhost", "Dave", "password");
if (!$con){
die ("Could not connect to database: " . mysqli_connect_error());
}
mysqli_select_db($con, "snippet");
$res = mysqli_query($con, "select * from image");
echo "<table>";
while ($row = mysqli_fetch_array($res)){
$row["image"] = "data:image/png;base64,".base64_encode($row["image"]); //the trick!
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo $row["image"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
You need to store image MIME type in database, then you can display image like this:
echo $row["image"];
header("Content-Type: ".$row['mime_type']);
For example, PNG's MIME type is image/png;
Related
Right guys here is my big ass question, I'm just a begginer and all of that so it's probably just a silly and easy thing but how from this code
<?php
session_start();
if(!isset($_SESSION['password']))
{
header('Location:index.php');
exit();
if ($_SESSION['logged_in']= 0)
{
header('Location:index.php');
exit();
}
}
?>
<?php include 'includes/header.php';?>
<div class="page_menu_box">
<ul>
<li>USER PROFILE</li>
<li>ABOUT US</li>
</ul>
</div>
<div class="page_content">
<table class="viewbook_table">
<tr>
<th>ISBN</th>
<th>AUTHOR</th>
<th>DATE WHEN PUBLISHED</th>
<th>COVER</th>
</tr>
<?php
include("core/database/connect.php");
$connection = #new mysqli('localhost','root','','bs_admin_tools');
$sql = "SELECT * FROM books";
$query = $connection->query( $sql );
while ($row = mysqli_fetch_array($query))
{
echo "<tr>";
echo "<td width = 15%>";
echo $row['ISBN'];
echo "</td>";
echo "<td width = 15%>";
echo $row['author'];
echo "</td>";
echo "<td width = 15%>";
echo $row['datepublished'];
echo "</td>";
//check whether an image is available for the record chosen
if ( $row['imgdir'] )
{
//create a variable which holds data from the images folder
$imageDir = "image/";
//create a second variable which holds the value of the image linked to the record selected
$img = $imageDir . $row['imgdir'];
/*link to the directory*/
echo "<td width ='15%' padding='30%'>";
//display the image
echo "<left><a href='viewdetails'><img src='$img'></a>";
echo "</td></tr>";
}
//echo "<br />"; //display a line break
}//end of the while loop
//release connection from database
mysqli_close($connection);
?>
</table>
</div>
<?php
echo "<div class='page_menu_box'><ul><li>
".$_SESSION['login']."
</ul></li></div>'";
include 'includes/footer.php';?>
I just want to basically click on the $img and view the book details, in other words I make one of the variables into a hyperlink and whenever someone is transferred onto another page I want a variable to be sended to that file where I would facilitate the file to intake the variable and display other shit about that book using 'Select from books where $theVariable="XD"'. I would be really happy if anyone could help me, I'm bouncing my head over a wall for the 2nd day to work that out.
I can only do stuff on php so please solutions only involving php or html ;_;
I have an original index.php file about Forum installed at a HOST.
I need to make a table with two columns: one column for Ads and another column for the Forum.
When I use directly:
<?php
echo "<table>";
echo "<tr>";
echo "<td width='20%'>";
$dir = 'images/';
if ($opendir = opendir($dir))
{
$file = 'lenovo-a1000.jpg';
$type = 'image/jpeg';
echo "<img src='$dir/$file' width='240' height='401'";
}
echo "</td>";
echo "<td width='80%'>";
........
........ original code of the Forum;
........
echo "</td>";
echo "</tr>";
echo "</table>";
?>
it raises error messages, but the table appears;
Then to avoid error messages I decided to make two functions:
<?php
function tbl_open()
{
echo "<table>";
echo "<tr>";
echo "<td width='20%'>";
$dir = 'images/';
if ($opendir = opendir($dir))
{
$file = 'lenovo-a1000.jpg';
$type = 'image/jpeg';
echo "<img src='$dir/$file' width='240' height='401'";
}
echo "</td>";
echo "<td width='80%'>";
}
........
........ original code of the Forum;
........
function tbl_close()
{
echo "</td>";
echo "</tr>";
echo "</table>";
}
?>
Now there is not error messages, but the table does not appear.
How to solve my Problem ?
You dont need to echo HTML in PHP. Simply close the PHP tag, write yout HTML and open the PHP tag again when you want it again. EX:
<table>
<tr>
<td width='20%'>
<?php
$dir = 'images/';
if ($opendir = opendir($dir))
{
$file = 'lenovo-a1000.jpg';
$type = 'image/jpeg'; ?>
<img src='<?php echo $dir . $file ?>' width='240' height='401' />
<?php } ?>
</td>
<td width='80%'>
<?php
........
........ original code of the Forum;
........
?>
</td>
</tr>
</table>
The solution is not to make a table into a forum's index.php, because this will raise warnings and errors about header().
My opinion now is the solution is to have a previous .php file which to start two iframes vertically suited FRAMES, for example (iframe width=80%) for the Forum and (iframe width=20%) for ads. I tested this - it works without any warnings and without any errors.
How to display the particular image in case if that image is not available in database? I have
database name:project
table name: image
fields: id (int), file (varchar) [image url stored here], name (varchar) [image description]
PHP code is here:
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $row["file"]; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
simply use #getimagesize description w3school php.net this method will check if image actually exists . will return false if image deleted from db or from destination.
<?
$img="image url"; //orginal image url from db
if(!#getimagesize($img))
{
$img="default image" //if image not found this will display
}
?>
Update
for your code use like this
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
$img=$row["file"]; //orginal image url from db
if(!#getimagesize($img))
{
$img="default image" //if image not found this will display
}
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $img; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
Use a simple if condition
<img src="<? php if($row["file"]){ echo $row["file"] ; } else{// other image name}?>" height="100px" width="150px">
You can use ternary operator to do this, refer below and give a try
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
// USE TERNARY OPERATOR HERE
$imagePath = (isset($row["file"]) && !empty($row["file"]) && file_exists($row["file"]))?$row["file"]:'img.default.jpg'; // REPLACE YOUR IMAGE PATHE HERE
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $imagePath; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
If the file reference in the database is pointing to a file that may have been deleted, you have to check if the file still exists. Ideally, when he file is deleted, the reference in the database should be updated too, to avoid such problems, but that may not always be possible, especially when many people have access to FTP.
Check to see if file exists:
if(file_exists($yourImage)){
echo $yourImage;
}
else{
echo $defaultImage;
}
You're storing the directory to the image within the varchar? if so try changing your database to store the image itself using BLOB as explained here
Then you can use an if statement.
<img src="<? php if($row["file"]){echo $row["file"];} else { echo '/directory/to/image.jpg'}?>" height="100px" width="150px">
I would also recommend using a include, this may not be 100% relevant for your current project but can help down the line when needing to use more than one database connection.
create a dbconnect.php file //name it what ever you want
insert code into dbconnect.php file:
<? php $con=mysql_connect("localhost","root","") or die ("no connection"); mysql_select_db("project")or die("no database exit");?>
in any file you're trying to use a database use include_once 'dbconnect.php'
I have a table of data in my website and I want to add search functions which could be based on criteria such as Name, type and date because the data in table are about events.
Here is my table code:
<?php
$user="admin";
$pass="whatever";
$host="localhost";
$db_name="eventregisteration";
$con=mysqli_connect($host, $user, $pass, $db_name);
if(mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sqlget="SELECT * FROM eventform";
$sqldata=mysqli_query($con,$sqlget)
or die("Error Getting Data");
echo "<table border=2 bordercolor=#440000 cellpadding=2 bgcolor=#DDDDDD width=100%>";
echo "<tr bgcolor=#555555 style=font-size:18px><th>Event Code</th><th>Event Name</th><th>Event Type</th><th>Event Level</th><th>Start Date</th>
<th>End Date</th><th>Point</th><th>Picture</th><th>Video</th><th>Description</th></tr>";
while($row=mysqli_fetch_array($sqldata, MYSQLI_ASSOC)){
echo "<tr align=center><td>";
echo $row['event_code'];
echo "</td><td>";
echo $row['event_name'];
echo "</td><td>";
echo $row['event_type'];
echo "</td><td>";
echo $row['event_level'];
echo "</td><td>";
echo $row['start_date'];
echo "</td><td>";
echo $row['end_date'];
echo "</td><td>";
echo $row['points'];
echo "</td><td>";
echo $row['pic'];
echo "</td><td>";
echo $row['video'];
echo "</td><td>";
echo $row['description'];
echo "</td></tr>";
}
echo "</table>";
?>
I need a dropdown box with type, date , name and a textbox, I don't know how to connect these to each other so if the user wants to search by name they choose name and if they want to search by date a calendar pops for them.
You have to specify the path according to the location of the page in which you are calling the picture/video. Suppose that your folder structure is like
Project Folder
eventpic
videos
file.php
file1.php
and suppose that you are calling the image in file.php file. Then, you have to give path as
<img src="<?php echo $row['pic'];?>">
or
<img src="localhost/workspace/project/<?php echo $row['pic'];?>">
Now suppose that your files are stored in other folder say view, now your project folder structure will be
eventpic
videos
view
In this case, the pictures/videos are outside your file's folder. So you have to specify path as
<img src="../<?php echo $row['pic'];?>">
or
<img src="localhost/workspace/project/<?php echo $row['pic'];?>">
../ will move outside the current directory.
I am new to php.
I made a member registration on login page and adm too. So inside admin I wanted to get the list of the members and delete the members I dont want. So I took the a code from a sample code for phone book from http://localhost/xamp and editted it to my requirement I am able to retrieve the members but unable to delete the members. See the code below:
<?php
require_once('auth.php');
require_once('../config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
?>
<html>
<head>
<meta name="author" content="Kai Oswald Seidler">
<link href="../loginmodule.css" rel="stylesheet" type="text/css">
<title></title>
</head>
<body>
<p>
<h2><?php echo "User list"; ?></h2>
<table border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#f87820">
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib1']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib2']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib3']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="50" height="6"><br><b><?php echo $TEXT['phonebook-attrib4']; ?></b></td>
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
</tr>
<?php
$firstname=$_REQUEST['firstname'];
$lastname=$_REQUEST['lastname'];
$phone=$_REQUEST['phone'];
if($_REQUEST['action']=="del")
{
$result=mysql_query("DELETE FROM members WHERE member_id={$_REQUEST['member_id']}");
}
$result=mysql_query("SELECT member_id,firstname,lastname,login FROM members ORDER BY lastname");
$i = 0;
while($row = mysql_fetch_array($result)) {
if ($i > 0) {
echo "<tr valign='bottom'>";
echo "<td bgcolor='#ffffff' height='1' style='background-image:url(img/strichel.gif)' colspan='6'></td>";
echo "</tr>";
}
echo "<tr valign='middle'>";
echo "<td class='tabval'><img src='img/blank.gif' alt='' width='10' height='20'></td>";
echo "<td class='tabval'><b>".$row['lastname']."</b></td>";
echo "<td class='tabval'>".$row['firstname']." </td>";
echo "<td class='tabval'>".$row['member_id']." </td>";
echo "<td class='tabval'><a onclick=\"return confirm('".$TEXT['userlist-sure']."');\" href='userlist.php?action=del&member_1d=".$row['member_id']."'><span class='red'>[".$TEXT['userlist-button1']."]</span></a></td>";
echo "<td class='tabval'></td>";
echo "</tr>";
$i++;
}
echo "<tr valign='bottom'>";
echo "<td bgcolor='#fb7922' colspan='6'><img src='img/blank.gif' alt='' width='1' height='8'></td>";
echo "</tr>";
?>
</table>
</body>
</html>
I haven't editted it that properly and the looks in all.
Please help me in making it able to delete the members also.
I didn't understand what .$TEXT['userlist-button1'].,'".$TEXT['userlist-sure']. variables are?
I also want to include an approved and disapproved radio button in table for each members.
How can I do that?
Please if you can help me.
This should be a POST via a FORM not a href link (GET).
$TEXT is obviously an array holding the text you want printed.
You need to replace &member_1d in the href with a real & and a real i as &member_id.
$TEXT is an array contaning all the language strings for the selected language.
You find the strings defined unter /lang/yourlanguage.php
In general this is not a very good example to start coding with IMO.
But I think your app may start working, if you make sure, the language files and other include files are available and you change this &member_1d with &member_id
An example of a list of members with delete links:
$query = mysql_query("SELECT member_id,firstname,lastname,login FROM members ORDER BY lastname");
if(mysql_num_row($query)!= 0){ //only continue if there are members in the database
while($row = mysql_fetch_assoc($query)){ //loop through each row in the database
$member_id = $row['member_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
echo '<p>' . $firstname . ' - ' delete '</p>';
}
}
A simple script on delete_member.php to delete the member from the database.
if(isset($_GET['id'])){
$member_id = $_GET['id'];
$query = mysql_query("DELETE FROM members WHERE member_id='$member_id'");
echo '<p>This user was deleted from database</p>';
}
This code is only basic to give an example.
I would however prefer to use a simple form and $_POST for something like this instead of using $_GET which is very vulnerable in this kind of instance.
After getting the list of members use a form with input field to type the id you want to delete.