Jump to content

Error with linking users to gallery


silverglade

Recommended Posts

Hi I am getting the following errors when I try to output images that are linked to the current logged in user to the image gallery. Here is the error.

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/web173/b1739/sl.brendansite1/public_html/PHOTO_SITE/gallery/index.php on line 44

 

 

I don't see what is wrong with line 44, I commented it. Any help greatly appreciated. thank you.

 

<?php
include('globals.php');
session_start();

//if(isset($_POST['login'])) {
//protect DB with real_escape_string
//$user=mysqli_real_escape_string($link, $_POST['username']);
//$mdpass=MD5($_POST['password']);

//below is the login check.  You might have this somewhere else
//$query2 = "SELECT id FROM users WHERE password='$mdpass' AND username=\"$user\"";
//$result2 = mysql_query($query2);
//$query_data2 = mysql_fetch_row($result2);

//      IF (!$query_data2[0]) { 
//      }
//   ELSE {
//     session_start();
//      $_SESSION['current_id']=$query_data2[0];



      $_SESSION['current_id']=1;
//make session a variable that can be used anywhere on the page.
$userid=$_SESSION['current_id']; 
//In case you want user name to show on page etc. 
$getuser = mysql_query("SELECT firstname,lastname FROM users WHERE id=$userid");
WHILE($gtuser = mysql_fetch_array($getuser)) {//THIS IS LINE 44
$firstname=$gtuser['firstname']; 
$lastname=$gtuser['lastname']; 
}

$userimages = mysql_query("SELECT image_id,filename FROM images WHERE gallery_user=$userid");
//get user image count
$userimagecnt = mysql_num_rows($userimages);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Uploaded Images</title>
    </head>
    <body>
<div>
<?php echo "<p>Hello $firstname $lastname</p>";?>
<h1>Uploaded Images</h1>
<p><a href="upload.php">Upload an image</a></p>

<div class="container" style="float:left;width:880px"> 
<?php
IF ($userimagecnt == 0) { ?>
<li>No uploaded images found</li>
<?php 
} ELSE {
$i=0;
WHILE ($result = mysql_fetch_array($userimages)){
$id = $result['image_id'];
$imagename = $result['filename'];

$i++;
?>      
<div style="float:left;width:110px;">                    
<table cellpadding="1">
 <tr>
	  <td><a href="view.php?id=<?php echo $id ?>"> <img src="view.php?id=<?php echo $id ?> " width="100" height="100" /></a></td>
 </tr><tr>
	  <td align="center"><?php echo $imagename ?></td>
 </tr>
</table>
</div>					  
<?php
$show=8;//Number of images to show per line.
if(($i % $show) == 0) {
echo "<div style=\"clear:both\"> </div>";                      
}//end if((++$i % $show) == 0)
}// end WHILE ($result = mysql_fetch_array($userimages)){ 
}//end if ($userimagecnt is greater than 0 with ELSE line 66 
//}//end if user is logged in with ELSE line 26 
//}//end POST login... More than likely you have login on another page but as I put everything on this page I've included closing bracket here.
?>
</div><!-- end container --> 
</div><!-- end tag line 59-->         
</body>
</html>

 

here is the database structure.

 

-- Database: `photo_artists`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `images`

--

 

CREATE TABLE `images` (

  `image_id` bigint(20) unsigned NOT NULL auto_increment,

  `gallery_user` int(11) NOT NULL,

  `filename` varchar(255) NOT NULL,

  `mime_type` varchar(255) NOT NULL,

  `file_size` int(11) NOT NULL,

  `file_data` longblob NOT NULL,

  PRIMARY KEY  (`image_id`),

  UNIQUE KEY `image_id` (`image_id`),

  KEY `filename` (`filename`),

  KEY `gallery_user` (`gallery_user`)

) ENGINE=MyISAM AUTO_INCREMENT=29 DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;

 

--

--

-- --------------------------------------------------------

 

--

-- Table structure for table `users`

--

 

CREATE TABLE `users` (

  `id` int(11) NOT NULL auto_increment,

  `status` varchar(20) NOT NULL,

  `lastname` varchar(50) NOT NULL,

  `dob` date NOT NULL,

  `gender` varchar(10) NOT NULL,

  `username` varchar(20) NOT NULL,

  `password` varchar(60) NOT NULL,

  `email` varchar(20) NOT NULL,

  `activationkey` varchar(100) NOT NULL,

  PRIMARY KEY  (`id`),

  UNIQUE KEY `username` (`username`),

  UNIQUE KEY `email` (`email`),

  UNIQUE KEY `activationkey` (`activationkey`)

) ENGINE=MyISAM AUTO_INCREMENT=43 DEFAULT CHARSET=latin1 AUTO_INCREMENT=43 ;

Link to comment
https://forums.phpfreaks.com/topic/241356-error-with-linking-users-to-gallery/
Share on other sites

Also, does anyone know the mysql equivalent of the php manual? Ideally it would cover "foreign keys" as that is pretty obscure so it will be a good manual if it has that I think. Please. thank you. I don't see anything that makes sense or is useful at this link

 

http://dev.mysql.com/doc/refman/5.5/en/

Check and see if your query is spitting an error.

 

mysql_query() will return FALSE on error, and mysql_fetch_array() doesn't like to be given a boolean.

 

Check if your query returned FALSE, and output the error, returned by mysql_error().

 

MySQL statement index

http://dev.mysql.com/doc/refman/5.5/en/dynindex-statement.html

MySQL function index

http://dev.mysql.com/doc/refman/5.5/en/dynindex-function.html

that's interesting, replace your code with this

 

<?php
include('globals.php');
session_start();

//if(isset($_POST['login'])) {
//protect DB with real_escape_string
//$user=mysqli_real_escape_string($link, $_POST['username']);
//$mdpass=MD5($_POST['password']);

//below is the login check.  You might have this somewhere else
//$query2 = "SELECT id FROM users WHERE password='$mdpass' AND username=\"$user\"";
//$result2 = mysql_query($query2);
//$query_data2 = mysql_fetch_row($result2);

//      IF (!$query_data2[0]) { 
//      }
//   ELSE {
//     session_start();
//      $_SESSION['current_id']=$query_data2[0];



      $_SESSION['current_id']=1;
//make session a variable that can be used anywhere on the page.
$userid=$_SESSION['current_id']; 
//In case you want user name to show on page etc. 
$getuser = mysql_query("SELECT firstname,lastname FROM users WHERE id=$userid");
if(!$getuser){

   die(mysql_error());
}
WHILE($gtuser = mysql_fetch_array($getuser)) {
$firstname=$gtuser['firstname']; 
$lastname=$gtuser['lastname']; 
}

$userimages = mysql_query("SELECT image_id,filename FROM images WHERE gallery_user=$userid");
//get user image count
$userimagecnt = mysql_num_rows($userimages);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Uploaded Images</title>
    </head>
    <body>
<div>
<?php echo "<p>Hello $firstname $lastname</p>";?>
<h1>Uploaded Images</h1>
<p><a href="upload.php">Upload an image</a></p>

<div class="container" style="float:left;width:880px"> 
<?php
IF ($userimagecnt == 0) { ?>
<li>No uploaded images found</li>
<?php 
} ELSE {
$i=0;
WHILE ($result = mysql_fetch_array($userimages)){
$id = $result['image_id'];
$imagename = $result['filename'];

$i++;
?>      
<div style="float:left;width:110px;">                    
<table cellpadding="1">
 <tr>
	  <td><a href="view.php?id=<?php echo $id ?>"> <img src="view.php?id=<?php echo $id ?> " width="100" height="100" /></a></td>
 </tr><tr>
	  <td align="center"><?php echo $imagename ?></td>
 </tr>
</table>
</div>					  
<?php
$show=8;//Number of images to show per line.
if(($i % $show) == 0) {
echo "<div style=\"clear:both\"> </div>";                      
}//end if((++$i % $show) == 0)
}// end WHILE ($result = mysql_fetch_array($userimages)){ 
}//end if ($userimagecnt is greater than 0 with ELSE line 66 
//}//end if user is logged in with ELSE line 26 
//}//end POST login... More than likely you have login on another page but as I put everything on this page I've included closing bracket here.
?>
</div><!-- end container --> 
</div><!-- end tag line 59-->         
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.