I am new to PHP and really just troubleshooting a migration issue (moving from live working server to new server with this error). I'm not well versed in coding so I apologize in advance if I'm overlooking something stupid simple.

Here's the error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /###/###/public_html/diss/modules/CMS/CMS_Class.php on line 134

Here's the code from line 134:

while ($block = mysql_fetch_assoc($blockresult)){
	 		$blockid = $block['blockid'];
	 		$blockorder = $block['blockorder'];
	 		$typeid = $block['typeid'];
			$title = $block['title'];

And lastly here is the entire code:

<?php

class CMS {
	private $id;	// page id
	private $editable = FALSE;
	public $title;
	public $parentid;
	public $pagetype;
	public $meta_desc;
	public $meta_keywords;

	public function __construct($virtual_file = NULL) {
		// use CMS to build page if we're loading page.php or virtual file is declared
		$CMS = ($_SERVER['PHP_SELF'] == '/page.php' || $virtual_file);
		// if CMS, make sure the page even exists before continuing further
		if ($CMS) {
			if ($virtual_file)
				$this->virtual_file = $virtual_file;
			else {
				$temp = explode('.', $_GET['id']);
				$this->virtual_file = mysql_escape_string($temp[0]);
			}

			$sql = "SELECT pageid, pagetype, parentid, title, meta_desc, meta_keywords, access FROM pages WHERE virtualfile='{$this->virtual_file}'";
			$result = mysql_query($sql);
	
			if ( mysql_num_rows($result) ){
				$row = mysql_fetch_assoc($result);
				$this->id = $row['pageid'];
				$this->title = $row['title'];
				$this->pagetype = $row['pagetype'];
				$this->parentid = $row['parentid'];
				$this->meta_desc = $row['meta_desc'];
				$this->meta_keywords = $row['meta_keywords'];
				$this->access = $row['access'];
			}
			else {
				die('PAGE NOT FOUND.');
			}
	
			global $Permissions, $User;
			$this->editable = $Permissions->check('cms');
			
			switch ($this->access) {
				case 'cms':	// permit only CMS admins
					if ( !$this->editable )
						header('Location: /index.html');
					break;
				case 'user': // permit only logged-in users
					if ( !$User->loggedIn() )
						header('Location: /' . CMS_REDIRECT . '?reroute=' . $_SERVER['REQUEST_URI'] );
					break;
				default:	// allow everyone to pass
					break;
			}
		}
	}


	public function make_admin_bar($thisstyle = ''){		
		if (!$this->editable) return;
		global $style;
	
		if ($thisstyle) echo str_replace('{title}', '&nbsp;', $style[$thisstyle]['block_top']);
		?>

		<script>
			var editor_width = <?php echo EDITOR_WITDH; ?>;
			var editor_height = screen.availHeight - 100;
		</script>
		<script src="cms/remote.js"></script>
		<script src="cms/edit.js"></script>
		<center>
		<input type="button" class="cmsedit" value="Logout" onclick="logout(this);">
		<input type="button" class="cmsedit" value="Admin" onclick="window.location.href='admin.html';">
		<input type="button" class="cmsedit" value="Media" onclick="window.location.href='media.html';">
		<input type="button" class="cmsedit" value="Edit Page Info" onclick="edit_page(<?php echo $this->id; ?>);">
		<input type="button" class="cmsedit" value="Delete Page" onclick="del_page(this, <?php echo $this->id; ?>);">
		<input type="button" class="cmsedit" value="New Page" onclick="new_page(<?php echo $this->id; ?>);">
		</center>
		<?php
			if ($thisstyle) echo $style[$thisstyle]['block_bottom'];			
	}


	public function make_links($location, $scope){
		global $link;

		if ($scope == 'thispage'){		// these links appear in this page only 
			if ($this->parentid)	// if this is a child page 
				$query = "SELECT href, linkname FROM links WHERE location='$location' AND linkparent={$this->parentid} ORDER BY linkorder";
			else	// if this is top-level
				$query = "SELECT href, linkname FROM links WHERE location='$location' AND linkparent={$this->id} ORDER BY linkorder";
		}

		else	// these links appear in all pages
			$query = "SELECT href, linkname FROM links WHERE location='$location' ORDER BY linkorder";

		$result = mysql_query($query);
		$numlinks = mysql_num_rows($result);
		if (!$numlinks && !$this->editable) return;

		echo $link[$location]['top'];

		$counter = 0;

		while ($row = mysql_fetch_assoc($result)){
			$counter++;
			if ($row['href'] == $this->virtual_file.'.html')
				$string = str_replace('{linkname}', $row['linkname'], $link[$location]['link_item_current']);
			else 
				$string = str_replace('{linkname}', $row['linkname'], $link[$location]['link_item']);
			echo str_replace('{href}', $row['href'], $string);
			if ($counter != $numlinks) echo $link[$location]['spacer'];
			
		}

		if ($this->editable)
			echo "<input type=\"button\" value=\"Edit Links\" class=\"cmsedit\" onclick=\"editlinks({$this->id}, '$location', '$scope');\">";
		echo $link[$location]['bottom'];
	}


	public function make_block($location, $bstyle, $scope){
		global $style;

		if ($scope == 'thispage')	// this block appears in this page only 
			$blockquery = "SELECT * FROM blocks WHERE location='$location' AND pageid={$this->id} ORDER BY blockorder DESC";
		else	// this block appears in all pages
			$blockquery = "SELECT * FROM blocks WHERE location='$location' ORDER BY blockorder DESC";

		$blockresult = mysql_query($blockquery);
		
		while ($block = mysql_fetch_assoc($blockresult)){
	 		$blockid = $block['blockid'];
	 		$blockorder = $block['blockorder'];
	 		$typeid = $block['typeid'];
			$title = $block['title'];
	
			if ($title) {
				$title = str_replace('{title}', $title, $style[$bstyle]['block_title']);
				echo str_replace('{title}', $title, $style[$bstyle]['block_top']);
			}

			if ($this->editable) { ?>
				<center>
				<select class="cmsselect" onchange="pickoption(this, <?php echo $blockid; ?>, '');">
					<option value="">BLOCK Options</option>
					<optgroup label="New">
						<option value="newsub">SubBlock</option>
					</optgroup>
					<optgroup label="Edit">
						<option value="editblocktitle">Title</option>
					</optgroup>
					<optgroup label="Delete">			
						<option value="delblock">This Block</option>
					</optgroup>
				</select>
 				<input type="button" class="cmsedit" value="Swap" onclick="reorder('blocks', <?php echo $blockid; ?>, <?php echo $blockorder; ?>, this);">
				<br clear="all"></center>
			<?php }
			switch ($block['type']){
				case 0:
					$this->make_subblock($blockid, $bstyle);
					break;	
				case 1: // Forms
					require_once( DIR_ROOT . 'modules/form/form_class.php' );
					$form = new Form();
					$form->fetchForm($typeid);
					break;
				case 2: // Register
					require_once( DIR_ROOT . 'modules/register/register_class.php' );
					$register = new Register();
					$register->form($typeid);
					break;
				case 3: // Gallery of Media
					require_once( DIR_ROOT . 'modules/gallery/gallery_class.php' );
					new Gallery($typeid);
					break;
				case 4: // Video Player
					require_once( DIR_ROOT . 'modules/gallery/gallery_class.php' );
					new Gallery($typeid);
					break;
				case 5: // Login Form
					require_once( DIR_ROOT . 'modules/login/user_utilities_class.php' );
					UserUtils::loginForm($typeid);
					break;
				case 6: // Change password form
					require_once( DIR_ROOT . 'modules/login/user_utilities_class.php' );
					UserUtils::passwordChangeForm($typeid);
					break;
				case 7: // Reset password form
					require_once( DIR_ROOT . 'modules/login/user_utilities_class.php' );
					UserUtils::passwordResetForm($typeid);
					break;
				case 8: // Reset password confirm
					require_once( DIR_ROOT . 'modules/login/user_utilities_class.php' );
					UserUtils::passwordResetConfirm();
					break;
			}

			echo $style[$bstyle]['block_bottom'];
		}

		if ($this->editable){
			if ($scope == 'thispage')
				echo "<center><input type=\"button\" class=\"cmsedit\" value=\"New Block\" onclick=\"newblock(this, {$this->id}, '$location');\"></center>";
			else
				echo "<center><input type=\"button\" class=\"cmsedit\" value=\"New Block\" onclick=\"newblock(this, 0, '$location');\"></center>";
		}		
	}


	private function make_subblock($blockid, $bstyle) {
		global $style;
		$subquery = "SELECT * FROM subblocks WHERE blockid='$blockid' ORDER BY ordernum DESC";
		$subresult = mysql_query($subquery);

		while ($subblock = mysql_fetch_assoc($subresult)){
			$subid = $subblock['subid'];
			$type = $subblock['type'];
			$ordernum = $subblock['ordernum'];
			$subcontent = $subblock['content'];

			if ($this->editable){ ?>
				<select class="cmsselect" onchange="pickoption(this, <?php echo $subid; ?>, <?php echo $type; ?>);">
					<option value="">SUB Options</option>
					<optgroup label="Edit">			
						<option value="edittitle">Title</option>
						<option value="edittext">Text</option>
					</optgroup>
					<optgroup label="Delete">
						<option value="delsub">This Subblock</option>
					</optgroup>
			<?php if ($type == 0){ ?>
					<optgroup label="Insert">
						<option value="insertGallery">A Gallery</option>
					</optgroup>
			<?php } ?>
				</select><input type="button" class="cmsedit" value="Swap" onclick="reorder('subblocks', <?php echo $subid; ?>, <?php echo $ordernum; ?>, this);">
				<br clear="all">
			<?php }

			echo str_replace('{title}', $subblock['title'], $style[$bstyle]['subblock_top']), $subcontent,
			$style[$bstyle]['subblock_bottom'];
		}
	}


	public function make_gallery($type, $subid){
		if ($this->editable){ ?>
			<select class="cmsselect" onchange="gallerySelect(this, <?php echo $subid; ?>, <?php echo $type; ?>);">
				<option value="">Gallery Options</option>
				<optgroup label="Edit">
					<option value="editgallery">Gallery</option>
				</optgroup>
				<optgroup label="Gallery Type">
					<option value="gallery1">Cluster</option>
					<option value="gallery2">List</option>
					<option value="gallery3">Slideshow</option>
					<option value="gallery4">Rotating</option>
				</optgroup>
				<optgroup label="Delete">
					<option value="delgallery">This Gallery</option>
				</optgroup>
			</select><br>
		<?php }
		
    	switch ($type){
			case 1:		// images in matrix fashion
				$query = "select * from gallery where subid=$subid ORDER BY ordernum";
				$result = mysql_query($query);

				while ($row = mysql_fetch_assoc($result)){
					$ordernum = $row['ordernum'];
					$filename = $row['filename'];
					$title = $row['title'];
					echo "<img src=\"imagevault/$filename\" class=\"gallery_img\" title=\"$title\" onclick=\"showimg($subid, $ordernum);\">";
				}
				break;
								
			case 2:		// one image per row
				$query = "select * from gallery where subid=$subid ORDER BY ordernum";
				$result = mysql_query($query);

				while ($row = mysql_fetch_assoc($result)){
					$ordernum = $row['ordernum'];
					$filename = $row['filename'];
					$title = $row['title'];
					$desc = $row['desc'];
					echo "<div class=\"gallery_img_box\"><img src=\"imagevault/$filename\" class=\"gallery_img\" align=\"left\" onclick=\"showimg($subid, $ordernum);\">"
					. "<div class=\"img_title\">$title</div>$desc<br clear=\"all\"></div>";
				}			
				break;

			case 3:		// images in viewer
				$query = "select * from gallery where subid=$subid ORDER BY ordernum";
				$result = mysql_query($query);
				?>

				<div class="gallery_viewer">
					<div class="viewer_bar">
						<span id="status<?php echo $subid; ?>"></span>
						<input type="button" value="Back" onclick="img(<?php echo $subid; ?>, 0);" class="button">
						<input type="button" value="Next" onclick="img(<?php echo $subid; ?>, 1);" class="button">
					</div>
					<div class="img_title" id="title<?php echo $subid; ?>"></div>
					<img src="" id="gallery<?php echo $subid; ?>" class="gallery_img">
					<div id="desc<?php echo $subid; ?>"></div>					
				</div>
				<script>
				<?php
					$string = "counter[$subid] = -1; gallery[$subid] = [";
					while ($row = mysql_fetch_assoc($result)){
						$filename = $row['filename'];
						$title = $row['title'];
						$desc = $row['desc'];
						$string .= "['$filename','$title','$desc'],";
					}
					echo (substr($string, 0, -1) . '];');
					?>		
					img(<?php echo $subid; ?>, 1);
				</script>
				<?php
				break;

			case 4:	// rotating gallery
				global $gallery_timer;
				$query = "select * from gallery where subid=$subid ORDER BY ordernum";
				$result = mysql_query($query);
				?>

				<img src="" id="gallery<?php echo $subid; ?>" class="gallery_img">
				<script>
				<?php
					$string = "counter[$subid] = -1; gallery[$subid] = [";
					while ($row = mysql_fetch_assoc($result)){
						$filename = $row['filename'];
						$title = $row['title'];
						$desc = $row['desc'];
						$string .= "['$filename','$title','$desc'],";
					}
					echo (substr($string, 0, -1) . '];');
					?>		
					img(<?php echo $subid; ?>, 1, <?php echo $gallery_timer; ?>, true);
				</script>
				<?php
				break;
		}
	}

}

?>

Thanks for any insight!

Recommended Answers

All 5 Replies

I can't see any connect information
you will need to connect to your daatbase at the top of the script,
then select the database when making the query

//Connect to database
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="members"; // Table name 
mysql_pconnect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

Since you're new we'll cut you some slack! But if you noticed there is a pinned thread at the top of the forum that talks specifically about this error. Be sure to check out this great thread. It's bound to have your solution!

Since you're new we'll cut you some slack! But if you noticed there is a pinned thread at the top of the forum that talks specifically about this error. Be sure to check out this great thread. It's bound to have your solution!

I've added the DB connection settings to the top of the script and it didn't help at all... I also read the FAQ (prior and post your response) - I'm clueless.

I am new to this, but I'm not really a programmer. I'm just troubleshooting this for a client and I would really like some help on this error. If you don't mind taking the time to explain how to troubleshoot, it would mean a lot.

I added the connection string, database select statement and the mysql_query() call with no success...

Again, thanks in advance for your patience and input :)

The mysql_fetch_assoc() error can also be due to an invalid mysql_query() syntax input. So try doing mysql_query($sql) or die(mysql_error()); so that you get debugging information. Then tell us what additional errors it reports back.
Also don't forget to use mysql_real_escape_string().

Thanks cwarn23!

The message displayed now reads "Query was empty"...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.