Jump to content

Need help converting mysqli query to PDO


man5

Recommended Posts

So I am working with a script that allows load more scrolling, much like youtube's comments section. 

 

The script I found uses mysqli and I am using PDO.  So I need a bit of help converting some code below.

 

Origional mysqli

$results = mysqli_query($connecDB,"SELECT COUNT(*) FROM paginate");

$get_total_rows = mysqli_fetch_array($results); //total records

//break total records into pages
$total_pages = ceil($get_total_rows[0]/$item_per_page);	

My PDO code

$results = DB::getInstance()->query("SELECT COUNT(*) FROM guestbook");

$get_total_rows = $results->results();

//break total records into pages
$total_pages = ceil($get_total_rows[0]/$item_per_page);	

With the code above, I get the following error.

"Object of class stdClass could not be converted to int"

Here are the two functions in a DB.php class.  That's where I am getting those from.  and the line that's giving me error is " $get_total_rows = $results->results();"

       public static function getInstance() {
		if(!isset(self::$_instance)) {
			self::$_instance = new DB();
		}
		return self::$_instance;
	}

	public function query($sql, $params = array()) {
		$this->_error = false;
		if($this->_query = $this->_pdo->prepare($sql)) {
			$x = 1;
			if(count($params)) {
				foreach($params as $param) {
					$this->_query->bindValue($x, $param);
					$x++;
				}
			}
			
			if($this->_query->execute()) {
				$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
				$this->_count = $this->_query->rowCount();
			} else {
				$this->_error = true;
			}
		}
		
		return $this;
	}

So i sort of fixed the problem with this code.

$get_total_rows = $results->count();

and it works if I remove [0] from the code below.

$total_pages = ceil($get_total_rows[0]/$item_per_page);	

The only problem with that is, after getting second row of entries, the "load more" button becomes unactive.

Btw here is the full javascript for it.

		<script type="text/javascript">
		$(document).ready(function() {

			var track_click = 0; //track user click on "load more" button, righ now it is 0 click
			
			var total_pages = <?php echo $total_pages; ?>;
			$('#results').load("fetch_pages.php", {'page':track_click}, function() {track_click++;}); 

			$(".load_more").click(function (e) { //user clicks on button
			
				$(this).hide(); //hide load more button on click
				$('.animation_image').show(); //show loading image

				if(track_click <= total_pages) //make sure user clicks are still less than total pages
				{
					//post page number and load returned data into result element
					$.post('fetch_pages.php',{'page': track_click}, function(data) {
					
						$(".load_more").show(); //bring back load more button
						
						$("#results").append(data); //append data received from server
						
						//scroll page to button element
						$("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 500);
						
						//hide loading image
						$('.animation_image').hide(); //hide loading image once data is received
			
						track_click++; //user click increment on load button
					
					}).fail(function(xhr, ajaxOptions, thrownError) { 
						alert(thrownError); //alert any HTTP error
						$(".load_more").show(); //bring back load more button
						$('.animation_image').hide(); //hide loading image once data is received
					});
					
					
					if(track_click >= total_pages-1)
					{
						//reached end of the page yet? disable load button
						$(".load_more").attr("disabled", "disabled");
					}
				 }
			});
		});
		</script>

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.