Jump to content

getting error : Catchable fatal error


MsKazza
Go to solution Solved by mac_gyver,

Recommended Posts

Hi,

I'm hoping someone might be able to help me out with something, whilst trying to implement pagination I keep getting the following error :

 

Catchable fatal error: Object of class mysqli could not be converted to string in

 

I know that this is because of a mix of mysqli and mysql however I cannot figure out for the life of me where the mysql is. As far as I know the whole thing is in mysqli.

 

Was hoping someone could help me figure out why i'm getting this error.

 

Thanks,

 

MsKazza

$recordID = $_GET["recordID"];

$rs_result = mysqli_query($con,"SELECT COUNT(product_id) FROM products WHERE category=$recordID") or trigger_error("Query Failed! SQL: $con - Error: ".mysqli_error(), E_USER_ERROR); 
$row = mysqli_fetch_row($rs_result); 
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 18;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;


$rs_result = mysqli_query ($con, "SELECT * FROM products INNER JOIN images ON products.product_code=images.product_code WHERE sub_category=$recordID ORDER BY product_id ASC LIMIT $offset, $rowsperpage") or trigger_error("Query Failed! SQL: $con - Error: ".mysqli_error(), E_USER_ERROR); 
?> 
<?php


if ($rs_result == NULL) {
  echo "There are no products to display in this category";
  }
  
  else {  ?>
  
  
<table align="center" border=0 width="550px" cellpadding="5" cellspacing="5"><tr>
<?php
$count1 = 0;
$max1 = 3;

while($row1 = mysqli_fetch_assoc($rs_result))
    {
	
	$prod_name = $row1['product_name'];
if(strlen($prod_name)>24){
    $prod_name=substr($prod_name,0,24).' ...';
}

if (['$rs_result'] > 0){ 
$count1++;
echo '<td align="center" valign="top">';
echo '<table align="center" bgcolor="#ffffff" width="160"><tr><td height="5" align="center">';
echo '</td></tr><tr><td align="center">';
echo '<a href="product.php?name=';
echo $row1['slug'];
echo '" class="topbar">';
echo '<img src="eCommerceAssets/images/products/';
echo $row1['product_image_big1'];
echo '" width="150" height="200">';
echo '</a>';
echo '</td></tr><tr><td align="center">';
echo '<strong><a href="product.php?name=';
echo $row1['slug'];
echo '" class="prod-name">';
echo $prod_name;
echo '</a></strong><br />';
echo '<a href="product.php?name=';
echo $row1['slug'];
echo '" class="button">More Info</a><br />';
echo '</td></tr></table><br /><br />';
echo '</td>';

if($count1 >= $max1){
//reset counter
$count1 = 0;
//end and restart
echo '</tr><tr>';

}
} elseif ($rs_result = 0) {
echo 'There are no products available.';
}
}
?>
 </tr></table>

 <?php
}   // close while loop
echo "<br />";

/******  build the pagination links ******/
echo "<div class='pagination'>";
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo "<span class='rest'> <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> </span>";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo "<span class='rest'>  <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a></span> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo "<span class='active'> <b>$x</b> </span>";
      // if not current page...
      } else {
         // make it a link
         echo "<span class='rest'><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </span>";
      } // end else
   } // end if 
} // end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo "<span class='rest'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a></span>";
   // echo forward link for lastpage
   echo "<span class='rest'>  <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a></span>";
} // end if
echo "</div>";
/****** end build pagination links ******/

 // close if
?>
</div>

Link to comment
Share on other sites

Catchable fatal error: Object of class mysqli could not be converted to string in

 

What the line number does the error occur on?

 

 

 

I know that this is because of a mix of mysqli and mysql however I cannot figure out for the life of me where the mysql is. As far as I know the whole thing is in mysqli.

 

Did you try running a search on your the code for "mysql_"? You could also tell PHP to show all errors and warnings. If you add the following to the top of your script, PHP should show deprecated warnings for any calls to mysql_* functions.

error_reporting(E_ALL);
ini_set('display_errors', 1);

Side note: You'll want to avoid outputting the raw value of PHP_SELF to the screen. Lines like the following open your page up to XSS attacks:

echo "<span class='rest'><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </span>";

For what it's worth, the above line could be replaced with the following:

echo "<span class='rest'><a href='?currentpage=$x'>$x</a> </span>";
Link to comment
Share on other sites

Ahh...the error is likely coming from lines like this:

$rs_result = mysqli_query($con,"SELECT COUNT(product_id) FROM products WHERE category=$recordID") or trigger_error("Query Failed! SQL: $con - Error: ".mysqli_error(), E_USER_ERROR);

In the call to trigger_error(), you are trying to display $con which is an object. Objects cannot be outputted this way. However, I imagine that you actually want to output the query which caused the error. To do that, you'll need to modify the code. You could do something like this:

$query = "SELECT COUNT(product_id) FROM products WHERE category=$recordID";
$rs_result = mysqli_query($con, $query) or trigger_error("Query Failed! SQL: $query - Error: ".mysqli_error(), E_USER_ERROR);
 
Once you get the script working, you'll want to do something else with the error, such as logging it. Otherwise, you provide too much information to the bad guy. And you will want to look into protecting your queries from SQL injection attacks. The $recordID variable, for example, can be tampered with by a user since it comes from a GET variable. The value needs to be escaped before using it in a query.
 
More information can be found here:
 
And/or here:
Link to comment
Share on other sites

thank you for your help i think that is sorted, however now i am getting this error: 

Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in/home/adlantic1361/public_html/sub_category.php on line 87
You are viewing page 1 of 0
We found products for 

 

and the "viewing page 1 of * and found * products for tshirt" isn't working either.

I would assume that the count isn't working properly.

 

Also my recordID isn't being passed from page to page

 

I was following the tutorial here : http://www.phpfreaks.com/tutorial/basic-pagination#comment-805

Link to comment
Share on other sites

The tutorial is very old, and it's not enough to merely append an “i” to all mysql_* calls.

 

I suggest you learn to use a modern database interface, ideally PDO (it's much better than mysqli). Then try to understand how pagination works. And finally write your own code. I'm sure this will be a lot faster, informative and enjoyable than trying to copy-and-paste somebody else's code from 2008.

Link to comment
Share on other sites

Thank you to all who contributed a useful answer.  @Jacques1 I am well aware that you don't just append an i to mysql, I didn't.  I do understand the principal behind pagination but when I tried to look up an example of pagination or where i could learn it myself only these kinds of tutorials appeared.  I spent hours searching Google to try help me fix the code myself before posting on here.  I don't enjoy following this kind of tutorial, and I certainly don't copy and paste, I type out the examples trying to understand it line by line.  I was asking for help not criticism. 

 

Now i have the pagination more or less working with my grid layout, however I still cannot figure out how to pass the recordID along in the links I tried to append them to the pagination links to no avail, so if someone could please give me a clue as to where it might go i would really appreciate it.  The first page shows the correct results.  I am aware of the risk of sql injection and will amend my code once i have it working.

 

Below is the whole code as it is now.

<?php
$recordID = $_GET["recordID"];


// find out how many rows are in the table 
$query = mysqli_query($con, "SELECT COUNT(*) FROM products WHERE sub_category='$recordID'");
$r = mysqli_fetch_array($query);

$numrows = $r[0];



// number of rows to show per page
$rowsperpage = 19;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$query = "SELECT product_id, product_name FROM products WHERE sub_category=$recordID LIMIT $offset, $rowsperpage";
$result = mysqli_query($con, $query);
// always make sure you get a valid result!!!
if($result == false) {
    die("Query failed: ".mysqli_error().PHP_EOL.$query);
}

echo "<b>You are viewing page " . $currentpage . " of " . $totalpages . "</b><br />";
echo "We found " . $numrows . " products for " . $row_subcategory['subcat_name'] . "<br /><br />";

?> 
<?php


if ($result == NULL) {
  echo "There are no products to display in this category";
  }
  
  else {  ?>
  
  
<table align="center" border=0 width="550px" cellpadding="5" cellspacing="5"><tr>
<?php
$count1 = 0;
$max1 = 3;


while($row1 = mysqli_fetch_assoc($result))
    {
	
	$prod_name = $row1['product_name'];
	$slug = $row1['slug'];
	$image = $row1['product_image_big1'];
if(strlen($prod_name)>24){
    $prod_name=substr($prod_name,0,24).' ...';
}

if (['$result'] > 0){ 
$count1++;
echo "<td align='center' valign='top'>";
echo "<table align='center' bgcolor='#ffffff' width='160'><tr><td height='5' align='center'>";
echo "</td></tr><tr><td align='center'>";
echo "<a href='product.php?name=" . $slug . "' class='topbar'>";
echo "<img src='eCommerceAssets/images/products/" . $image . "' width='150' height='200'>";
echo "</a>";
echo "</td></tr><tr><td align='center'>";
echo "<strong><a href='product.php?name=" . $slug . "' class='prod-name'>";
echo $prod_name;
echo "</a></strong><br />";
echo "<a href='product.php?name=" . $slug . "' class='button'>More Info</a><br />";
echo "</td></tr></table><br /><br />";
echo "</td>";

if($count1 >= $max1){
//reset counter
$count1 = 0;
//end and restart
echo '</tr><tr>';

}
} elseif ($result = 0) {
echo 'There are no products available.';
}
}
?>
 </tr></table>

 <?php
}   // close while loop
echo "<br />";

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo "<span class='rest'> <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> </span>";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo "<span class='rest'>  <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a></span> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo "<span class='active'> <b>$x</b> </span>";
      // if not current page...
      } else {
         // make it a link
         echo "<span class='rest'><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </span>";
      } // end else
   } // end if 
} // end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo "<span class='rest'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a></span>";
   // echo forward link for lastpage
   echo "<span class='rest'>  <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a></span>";
} // end if

/****** end build pagination links ******/

 // close if
?>

Link to comment
Share on other sites

  • Solution

when you build the query string part of links, you should use a function like http_build_query(). this will let you take any existing $_GET parameters, add/remove/modify any of them, then produce the part of the link after the ? - 

$get = $_GET; // get a copy of any existing get parameters. you only need to do this once

// in your pagination link code, for each link you produce
$get['currentpage'] =1; // set the current page to whatever value you want
$qs = http_build_query($get, '', '&'); // produce the query string part of the link
echo "<span class='rest'> <a href='?$qs><<</a> </span>"; // output the link. note: i removed the use of $_SERVER['HTTP_SELF'] since it is open to cross site scripting and it's not necessary in modern browsers
Link to comment
Share on other sites

thank you for your reply, I couldn't quite figure out how to use what you had suggested, but you mentioned about the & 

so i've changed my links to <a href='?recordID=$recordID&currentpage=$x'> and it seems to be working ok for now.  Thanks very much :)

 

Just one last thing if someone wouldn't mind,

since changing the query, my images etc on the grid layout aren't displaying correctly, hoping someone could suggest why.

while($row1 = mysqli_fetch_assoc($result))
    {
	
	$prod_name = $row1['product_name'];
	$slug = $row1['slug'];
	$image = $row1['product_image_big1'];
if(strlen($prod_name)>24){
    $prod_name=substr($prod_name,0,24).' ...';
}

if (['$result'] > 0){ 
$count1++;
echo "<td align='center' valign='top'>";
echo "<table align='center' bgcolor='#ffffff' width='160'><tr><td height='5' align='center'>";
echo "</td></tr><tr><td align='center'>";
echo "<a href='product.php?name=" . $slug . "' class='topbar'>";
echo "<img src='eCommerceAssets/images/products/";
echo $row1['product_image_big1'];
echo "' width='150' height='200'>";
echo "</a>";
echo "</td></tr><tr><td align='center'>";
echo "<strong><a href='product.php?name=" . $slug . "' class='prod-name'>";
echo $prod_name;
echo "</a></strong><br />";
echo "<a href='product.php?name=" . $slug . "' class='button'>More Info</a><br />";
echo "</td></tr></table><br /><br />";
echo "</td>";

As you can see i've tried getting row directly from database, i've tried concatenating it to the link and assigning the row['x'] to a variable.  The variable prod_name shows just fine, but the others slug and image don't

 

suggestions? 

thanks

Edited by MsKazza
Link to comment
Share on other sites

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.