Jump to content

Need help converting mysqli query to PDO


Go to solution Solved by 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;
	}
Edited by man5

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.

  • Solution

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>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.