Here is the simple code what i tried.
I am not able to display the image presented in database
Please help me in this regard.
I am using two files
1) db.php
This is for connecting mysql database and getting a record.
Images will be saved in a jp2 format.
2) test.php
Displaying record using php.
**If images are saved in JP2 format then then how can i convert these images and display into a web page.
If iam using this code some junk(bytecode say) is displaying ian a web page**
This is my db.php
<?php
function getResidentPhoto() {
$dbcnx = #mysql_connect('127.0.0.1', 'root', 'root#19211');
if (!$dbcnx) {
die('<p>Unable to connect to the ' . 'database server at this time.</p>'
);
}
if (!#mysql_select_db('finaldb')) {
die('<p>Unable to locate the ' . 'database at this time.</p>');
}
$selectrc = 'select BPFT_Photo from photo_templates limit 1';
$result = #mysql_query($selectrc, $dbcnx);
if (!$result) {
die('<p>Error performing query: ' . mysql_error() . '</p>');
} else {
}
$n = mysql_num_rows($result);
$row = mysql_fetch_assoc($result);
if ($n > 0) {
$Photo_Details = $row['BPFT_Photo'];
return $Photo_Details;
}
}
?>
and my test.php is
<?php
echo' <html>';
echo'<body>';
echo' <table>';
echo' <tr><td>';
$residentPhoto = getResidentPhoto();
$image_type_to_mime_type0 =image_type_to_mime_type($residentPhoto)
header('Content-type:$image_type_to_mime_type0');
echo' '.$residentPhoto;
echo'</td>';
echo'</tr>';
echo'</table>';
echo'</body>';
echo'</html>';
?>
Judging by the content-type header, I'm going to guess that $residentPhoto is raw image data. If that is the case, then there should be absolutely no output other than the image itself.
In test.php you should include or require the db.php file.
make sure the path of image is given and make sure it is correct.
You're mixing HTML with the actual image.
You just have to output the image in a separate file, which you have to include in your html with an "img" tag.
For example:
<img src="image.php?imageId=1" alt="Image" />
There is another solution but it has some drawbacks:
You can embedd image data in your HTML by using Data URLs: http://davidwalsh.name/data-uri-php but this will increase your HTML code size and (maybe) you will get problems with older browsers (IE < 8).
Related
the below to functions contain the code to insert into the sql database but sadly the db is still unable to load it to the database.
if (isset($_POST['register'])){
if(registerNewUser($_POST['inv_amount_expected'],$_POST['uname'],$_POST['passwo rd'],$_POST['email'])){
echo "You can now log-in to your account.
<a href='./index.php'>Click here to login.</a>
";
}else {
echo "Registration failed! Please try again.";
show_registration_form();
}
} else {
// has not pressed the register button
show_registration_form();
}
function registerNewUser($inv_amount_expected,$uname,$password,$email)
{
$sql = sprintf("insert into borrow (inv_amount_expected,uname,password,email) value ('&inv_amount_expected','&uname','&password','&email')",
mysql_real_escape_string($username), mysql_real_escape_string(sha1($password . $seed))
, mysql_real_escape_string($email), mysql_real_escape_string($code));
if (mysql_query($sql))
{
$id = mysql_insert_id();
return true;
}
else
{
return false;
}
return false;
}
could you help me out in where i am going wrong since im unable to understand.
i'm still an amature in php so please help me out.
I am putting my ideas together and at the moment i have the following:
-a mysql database with 2 tables.
-the first table "downloads" contains 3 rows, each with a unique id that and each represent the type of file being downloaded. e.g. application or theme.
-the second table contains the information about each download, these fields are the models supported by the download, the title, a picture, a brief description and a download link. each download has a unique id.
Now what i am trying to do is insert the data into a set of divs, these divs are as follows:
<div class="dlcontainer">
<div class="dlitem">
<div class="dltitle"><php $row['title'] ?></div>";
<div class="dlimage"><php $row['image'] ?></div>";
<div class="dldescription"><php $row['description'] ?></div>";
<div class="dllink"><php $row['downlink'] ?></div>";
</div>
</div>
As you can see i have been trying to populate the divs with information called from the database and that was my first attempt.
I didn't feel like i was going about this with the right approach, so i ended up with this, which also does not seem to work:
<?php
$con = mysql_connect("test","test","test");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM `content` WHERE pid = '2' AND models = 'all' OR models = $chosen");
while($row = mysql_fetch_array($result))
{
echo "<div class=\\"dlcontainer\\">";
echo "<div class=\\"dlitem\\">";
echo "<div class=\\"dltitle\\">$row['title']</div>";
echo "<div class=\\"dlimage\\">$row['image']</div>";
echo "<div class=\\"dldescription\\">$row['description']</div>";
echo "<div class=\\"dllink\\">$row['downlink']</div>";
echo "</div>";
echo "</div>";
}
mysql_close($con);
?>
Once i get this initial download working, i can then set up a loop that will display all the contents that match belong to a specific "pid" and either match a "models" value of "all" or one that has been selected by the user.
I am creating a website that stores information of homes like address, city etc. I got the upload to database part done and I got the search the database and display the information done.
But now my plan with that is you get the search results and from there your suppose to get the option to view the full profile of that home via link. Displaying the full profile with the information gathered from the database is already done through a separate php file. Now when I link the php file it will only display the last column in the database. How can I link it so when I click on "view full profile" it will connect to the correct data/profile that corresponds to the different address or price. Or if i cant use an html link what can I use? Is that impossible?
here is the code that displays the search results I have successfully been able to search the database and display it
<?php
if($sql->num_rows){
while ($row = $sql->fetch_array(MYSQLI_ASSOC)){
echo '<div id="listing">
<div id="propertyImage">
<img src="images/'.$row['imageName1'].'" width="200" height="150" alt=""/>
</div>
<div id="basicInfo">
<h2>$'.$row['Price'].'</h2>
<p style="font-size: 18px;"># '.$row['StreetAddress'].', '.$row['City'].', BC</p>
<p>'.$row['NumBed'].' Bedrooms | '.$row['NumBath'].' Bathrooms | '.$row['Property'].'</p>
<br>
<p>View Full Details | Get Directions
</div>
</div>';
height="150" alt=""/>';
}
}
else
{
echo '<h2>0 Search Results</h2>';
}
?>
Here is the php to display the information outputtest.php
<?php
mysql_connect("localhost","root","31588patrick");
#mysql_select_db("test") or die( "Unable to select database");
$query="SELECT * FROM propertyinfo";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$varStreetAddress=mysql_result($result,$i,"StreetAddress");
$varCity=mysql_result($result,$i,"City");
$varProperty=mysql_result($result,$i,"Property");
$varNumBed=mysql_result($result,$i,"NumBed");
$varNumBath=mysql_result($result,$i,"NumBath");
$varPrice=mysql_result($result,$i,"Price");
$varEmail=mysql_result($result,$i,"Email");
$varPhone=mysql_result($result,$i,"Phone");
$varUtilities=mysql_result($result,$i,"utilities");
$varTermLease=mysql_result($result,$i,"TermLease");
$varNotes=mysql_result($result,$i,"Notes");
$image1=mysql_result($result,$i,"imageName1");
$image2=mysql_result($result,$i,"imageName2");
$image3=mysql_result($result,$i,"imageName3");
$image4=mysql_result($result,$i,"imageName4");
$i++;
}
?> html code will go after this
Thanks
edit its working
<?php
////////////using mysqli to connect with database
$mysqli = new mysqli("localhost","root","31588patrick", "test");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
///////////set variables
$record_id = $_GET['record_id'];
$sql = $mysqli->query("select * from propertyinfo where StreetAddress like '%$record_id%'");
if($sql === FALSE) {
die(mysql_error()); // TODO: better error handling
}
if($sql->num_rows){
while ($row = $sql->fetch_array(MYSQLI_ASSOC)){
$varStreetAddress=$row['StreetAddress'];
$varCity=$row['City'];
$varProperty=$row['Property'];
$varNumBed=$row['NumBed'];
$varNumBath=$row['NumBath'];
$varPrice=$row['Price'];
$varEmail=$row['Email'];
$varPhone=$row['Phone'];
$varUtilities=$row['utilities'];
$varTermLease=$row['TermLease'];
$varNotes=$row['Notes'];
$image1=$row['imageName1'];
$image2=$row['imageName2'];
$image3=$row['imageName3'];
$image4=$row['imageName4'];
}
}
If the link you are using is only fetching the last record in the database table, then the underlying query that is used must be fetching the last record instead of the intended record. Am I correct in understanding the problem you are having? That any link you use is just returning the last record in the database?
Posting a code snippet will help the community take a closer look.
in your php file, you run a query for all records, but ...
$record_id = mysql_real_escape_string($_GET["record_id"]);
$query="SELECT * FROM propertyinfo WHERE id=$record_id";
your link needs to attach a parameter that will allow you to query for a specific record.
a href="outputtest.php&record_id=8"
AND you would also want to check what is in the GET parameter before sticking it into the query! I know mysql_real_escape_string is outdated.
SOLVED!
so ive created an image upload page which saves the image to a folder and sends the file name to the DB, ive checked to see if they are actually being added to the DB and folder which they are, but when i call the data to another page to display the images i get broken images.
below is my code to call the images, its some code ive scraped together from various tutorials as they gave me the same problem as im having now.
UPDATE:
ive managed to get the images showing but now im faced with being shown the same image for each row of data called, the id and img_name and right for each row but the image is always the same as the first listed.
UPDATED CODE:
<?php
//connect to database
include ('connect.php');
//save the name of image in table
$query = mysql_query("select * from tbl_img") or die(mysql_error());
//retrieve all image from database and store them in a variable
while ($row = mysql_fetch_assoc($query))
{
$img_name = $row['img'];
}
?>
<?php
include ('connect.php');
$img_id = mysql_query("SELECT * FROM tbl_img");
while ($row = mysql_fetch_assoc($img_id))
{
$id = $row['img_id'];
echo "
$id<br>
$img_name<br>
<img src='http://localhost/testarea/include/site_images/$img_name' />
";
echo "<br><br><br></p>
";
}
?>
If you start the path of image with / you mean an absolute path where / is the DocumentRoot folder (or the directory of virtualhost)
With src ="includes/xxx/image.png" you mean that includes is in the same folder with the php script. If it is not you can use relative path like
src="../includes/xxx/image.png" for example
<?php
include ('connect.php');
$img_id = mysql_query("SELECT * FROM tbl_img");
while ($row = mysql_fetch_assoc($img_id))
{
$img_name = $row['img'];
$id = $row['img_id'];
echo "
$id<br>
$img_name<br>
<img src='http://localhost/testarea/include/site_images/$img_name' />
";
echo "<br><br><br></p>
";
}
How can i use php to get an image url from database (based on user id) and display it out as an image.
http://mysite.com/img.php?id=338576
The code below shows a php script goes to a database , check whether the specific user(using the id in the link above) exist then get the image url out of that user's row.
<?php
//connect to database
include ("connect.php");
//Get the values
$id = mysql_real_escape_string($_GET['id']);
//get the image url
$result = mysql_query("SELECT * FROM users WHERE id='$id'")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$img_url = row['imgurl'];
mysql_close($connect);
}
else{
}
?>
The $Img_url is an actual image link www.asite.com/image.jpg
The problem is how can i use php to make an image from the image link($img_url)? By which http://mysite.com/img.php?id=338576 will turn into an image.
I hope someone could guide me
Thanks!
The easiest way:
header('Location: url/to/image');
You could also proxy the request, which uses your bandwidth:
echo(file_get_contents('url/to/image'));
This is pretty basic stuff. Am I understanding you correctly? I would just do this:
<?php
$img_html = '<img src="' . $img_url . '" />';
echo $img_html;
?>
Or check this answer:
How do I script a php file to display an image like <img src="/img.php?imageID=32" />?
i will get right into the chase! i got the following code to display an image and some data stored in a table
<?php
if (isset($_GET['p'])){ //onclick i call that script with parameter
$d=$_GET['p']; //so p is the parametered passed to the script
$connect=mysql_connect("localhost","user","")
or die ("Check your server connection");
$db_found = mysql_select_db("mydb", $connect);
$SQL = "SELECT * FROM table
WHERE d ='$d'";
$result = mysql_query($SQL);
$row=mysql_fetch_array($result);
echo "DATA: $row[data]<br>";
header('Content-type:image/jpg');
$content = $row['image'];
echo $content;
}
?>
i get the name of the string.if i remove all the echo and leave only the echo $content; my image is shown.Furthermore if remove echo$content and leave all other echoes,my echoes is shown again!! why cant i have both image and echos together?what should i do ? thnx in advance!
You have to call headers before other outputs (for example echo). Also you should use image/jpeg . Try this :
header('Content-type:image/jpeg');
echo "DATA: $row[data]<br>";
$content = $row['image'];
echo $content;
Correct mime-tag for jpg images is "image/jpeg" - try that instead
As the other answer points out you need to have all your echos after the header, but if you have both echos the image probably won't display probably, you just need the one echo (echo $content) and the one header.