View image from MySQL database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

View image from MySQL database

Post by jeffyyy »

Here is my page code:

Code: Select all

<?php
/**
 * UserInfo.php
 *
 * This page is for users to view their account information
 * with a link added for them to edit the information.
 *
 * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
 * Last Updated: August 2, 2009 by Ivan Novak
 */
include("include/session.php");
$page = "profile.php";

?>
<!DOCTYPE html>  
<html lang="en-GB">  
<head>
	<title>Tag Texter | Homepage</title>
	<link rel="stylesheet" href="style.css" type="text/css" />>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body>
<div id="content_top_topimage"></div> 
<div id="content_top">   	
<?php
/* Requested Username error checking */
$req_user = trim($_GET['user']);
if(!$req_user || strlen($req_user) == 0 ||
   !eregi("^([0-9a-z])+$", $req_user) ||
   !$database->usernameTaken($req_user)){
   die("Username not registered");
}

/* Logged in user viewing own account */

if(strcmp($session->username,$req_user) == 0){
   echo "<h1>My Account</h1><p>
   <p>
 <form name='newad' method='post' enctype='multipart/form-data'  action='upload.php'>
 <table>
 	<tr><td><input type='file' name='image'></td></tr>
 	<tr><td><input name='Submit' type='submit' value='Upload image'></td></tr>
	<input type='hidden' name='username' value='$session->username' />
	<br /><label>Profile?</label><input name='profile' type='checkbox' value='yes' checked>
 </table>	
 </form>";
}
/* Visitor not viewing own account */
else{ ?>
 <div id="profile_title">User Profile</div>
 <?php


}
/* Display requested user information */
$req_user_info = $database->getUserInfo($req_user);

/* Name */
echo "<p><b>Name:</b> ".$req_user_info['name']."<br />";

/* Username */
echo "<p><b>Username:</b> ".$req_user_info['username']."<br />";

/**
 * Note: when you add your own fields to the users table
 * to hold more information, like homepage, location, etc.
 * they can be easily accessed by the user info array.
 *
 * $session->user_info['location']; (for logged in users)
 *
 * ..and for this page,
 *
 * $req_user_info['location']; (for any user)
 */

/* If logged in user viewing own account, give link to edit */
if(strcmp($session->username,$req_user) == 0){
   echo "[<a href=\"useredit.php\">Edit Account Information</a>]<br><br>";
}

/* Link back to main */
echo "[<a href=\"main.php\">Main</a>] ";


if($session->isAdmin()){
   echo "[<a href=\"admin/admin.php\">Admin Center</a>]&nbsp;";
}



?>
</div>
<div id="content_top_bottomimage"></div>
</body>
</html>
No matter what I do, I cannot get the picture (which has the path stored in a MySQL database) to show up. What do I do? Here is the table structure:

Code: Select all

CREATE TABLE IF NOT EXISTS `uploads` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL,
  `type` varchar(30) NOT NULL,
  `size` int(11) NOT NULL,
  `path` varchar(60) NOT NULL,
  `username` varchar(100) NOT NULL,
  `profile` varchar(3) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: View image from MySQL database

Post by greyhoundcode »

I can't see your code for querying the database (to fetch the img path) nor can I see the code where you output that image - so it's hard to say what's going wrong right now - can you provide a bit more information?

Or are you asking us how you should go about querying the db and handling the result?
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: View image from MySQL database

Post by jeffyyy »

Code: Select all

<?php
include('include/connect.php');

$term = $session->username;

$sql = mysql_query("select * from uploads where username like '$term'");

while ($row = mysql_fetch_array($sql)){
?>
    <img src="http://tagtexter.com/tag/<?php echo ''.$row['path'];?>">

<?php
}
?>
If I use this on its own in a separate page, then it works just fine. However, when I try to put it into the other page I posted, nothing happens. Im not sure where on the page I would have to put the code in order for it to always show up. The point is that this is a profile picture, and I need it to show up on the users profile page (which I previously posted a copy of)
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: View image from MySQL database

Post by jeffyyy »

OK, im making a little progress, here is where Im at:

Code: Select all

/* Display requested user information */
$req_user_info = $database->getUserInfo($req_user);

/* Name */
echo "<p><b>Name:</b> ".$req_user_info['name']."<br />";

/* Username */
echo "<p><b>Username:</b> ".$req_user_info['username']."<br />";
$term = $req_user_info['username'];

$sql = mysql_query("select * from uploads where username = $term");

while ($row = mysql_fetch_array($sql)){
?>
<?php echo $req_user_info['username']; ?>
    <img src="http://tagtexter.com/tag/<?php echo ''.$row['path'];?>">
    
<?php
}
?>
<?php
that is returning a "mysql_fetch_array(): supplied argument is not a valid MySQL result resource" error. But, if I change the $sql to: "select * from uploads where id=2" (the id of a picture I know to be in the database) it shows the picture exactly how its supposed to.
What am I doing wrong?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: View image from MySQL database

Post by s992 »

What do you get when you echo $term?

Also, try changing this line to add the die() clause so that you can see what the MySQL error is.

Code: Select all

$sql = mysql_query("select * from uploads where username = $term") OR die(mysql_error());
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: View image from MySQL database

Post by jeffyyy »

I get:
Unknown column 'jhoschak9' in 'where clause'

jhoschak9 is the value that $term is supposed to return, so that part is working...but it is not selecting it out of the database

Here is a copy of that whole part:

Code: Select all

$req_user_info = $database->getUserInfo($req_user);

/* Name */
echo "<p><b>Name:</b> ".$req_user_info['name']."<br />";

/* Username */
echo "<p><b>Username:</b> ".$req_user_info['username']."<br />";
$term = $req_user_info['username'];

$sql = mysql_query("SELECT * FROM uploads WHERE username = $term") OR die(mysql_error());

while ($row = mysql_fetch_array($sql)){
?>
<?php echo $term; ?>
    
<?php
}
?>
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: View image from MySQL database

Post by s992 »

Try wrapping it with quotes?
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: View image from MySQL database

Post by jeffyyy »

nope, putting quotes around it produces the same error.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: View image from MySQL database

Post by mikosiko »

try this:

Code: Select all

$query = "SELECT * FROM uploads WHERE username ='" .  $term . "'";
$sql = mysql_query($query) OR die(mysql_error());
Post Reply