Jump to content

errors when trying to pagenate results


paulbray

Recommended Posts

Hi,

 

If anyone can assist I am receiving these errors

 

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /websites/123reg/LinuxPackage21/co/ns/ol/console-entertainment.co.uk/public_html/Test/view_products.php on line 80

 

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in /websites/123reg/LinuxPackage21/co/ns/ol/console-entertainment.co.uk/public_html/Test/view_products.php on line 102

 

My Code is

 

<?php # View products

$page_title = 'View Inventory';

require_once ('connect.php');

$display = 35; // Number of records per page

if (isset($_GET['p']) && is_numeric($_GET['p'])) { // How many pages there are

$pages = $_GET['p'];

} else {

// Count number of records

$q = "SELECT COUNT(Game_Title) FROM Products";

$r = @mysqli_query ($dbc, $q);

$row = @mysqli_fetch_array ($r, MYSQLI_NUM);

$records = $row[0];

// Calculate number of pages

if ($records > $display) {// More than one page

$pages = ceil ($records/$display);

} else {

$pages = 1;

}

} // End of p IF

if (isset($_GET['s']) && is_numeric ($_GET['s'])) {

$start = $_GET['s'];

} else {

$start = 0;
}

//make query

$q = "SELECT SKU_ProductID, SKU_MerchSKUID, Game_Title, Platform, Genre, Weight, Supplier, Supplier_Price, Cashback, Cashback_Amount AS dr, FROM Products ORDER BY Game_Title ASC LIMIT $start, $display";

$r = @mysqli_query ($dbc,$q);

echo '<TABLE ALIGN="CENTER" cellspacing="0" cellpadding="5" width="75%">

<TR>
<TD align="left"><b>SKU_ProductID</b></td>
<TD align="left"><b>SKU_MerchSKUID</b></td>
<TD align="left"><b>Game_Title</b></td>
<TD align="left"><b>Platform</b></td>
<TD align="left"><b>Genre</b></td>
<TD align="left"><b>Weight</b></td>
<TD align="left"><b>Supplier</b></td>
<TD align="left"><b>Supplier_Price</b></td>
<TD align="left"><b>Cashback</b></td>
<TD align="left"><b>Cashback_Amount</b></td>
</TR>
';

$bg = '#eeeeee';

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch background color

echo ' <tr bgcolor="' . $bg . '">

<td align="left">' . $row['SKU_ProductID'] . '</td>
<td align="left">' . $row['SKU_MerchSKUID'] . '</td>
<td align="left">' . $row['Game_Title'] . '</td>
<td align="left">' . $row['Platform'] . '</td>
<td align="left">' . $row['Genre'] . '</td>
<td align="left">' . $row['Weight'] . '</td>
<td align="left">' . $row['Supplier'] . '</td>
<td align="left">' . $row['Supplier_Price'] . '</td>
<td align="left">' . $row['Cashback'] . '</td>
<td align="left">' . $row['Cashback_Amount'] . '</td>
</tr>
';
}

echo '</table>';

mysqli_free_result ($r);

mysqli_close($dbc);

if ($pages > 1) {

echo '<br /><p>';

$current_page = ($start/$display) + 1;

if ($current_page != 1) {

echo '<a href="view_products.php"?s=' . ($start - $display) . '&p=' . $pages . ' ">Previous</a> ';

for ($i = 1; $i <= $pages; $i++) {

	if ($i != $current_page) {

		echo '<a href="view_products.php"?s=' . (($display * ($i - 1))) . '&p=' . $pages . '"> ' , $i . '</a> ';

	} else {

		echo $i . ' ';

	}

}

if ($current_page != $pages) {

	echo '<a href="view_products.php"?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';

}

echo '</p>';

}}
?>

Link to comment
Share on other sites

The problem is you're blindly trying to return rows from a MySQLi result resource, that isn't actually a result resource -- i.e. the query has an error.

 

$r = @mysqli_query ($dbc,$q);

mysqli_query() never actually throws a PHP error, so there's no use in the "@" to suppress errors. However you should handle the error here and throw one yourself:

 

$r = mysqli_query($dbc, $q) or trigger_error('MySQLi Error: ' . mysqli_error());

That will tell you why the query is failing. Obviously you'll fix any errors before pushing to a production server (where display_errors should be turned off anyway), so it should never throw an error the user will see.

Link to comment
Share on other sites

If you search for that error message, you will find that it generally means that your query failed due to an error of some kind and you need to have some error checking and error reporting logic in your code to get it to tell you why the query failed.

 

For troubleshooting purposes, you can echo mysqli_error($dbc) to find out why the query is failing.

 

And I going to guess that all the @'s you have put in front of the other mysqli_ statements in your code were to hide other similar error messages. Hiding the error messages doesn't fix your code, it just means that your code doesn't work and you aren't getting any of the errors that would let you know what isn't working. DON'T put @'s in your code to hide error messages, there's no valid reason to do so.

 

And, is there some reason this is the third thread you have started for this same code? Just because you are getting a different error with your code, doesn't mean it is a different problem. It's the same code and you should stick to one thread for it.

Link to comment
Share on other sites

ok sorry for multiple threads,  I have removed @ signs and this is the output

 

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /websites/123reg/LinuxPackage21/co/ns/ol/console-entertainment.co.uk/public_html/Test/view_products.php on line 60

 

Notice: MySQLi Error: in /websites/123reg/LinuxPackage21/co/ns/ol/console-entertainment.co.uk/public_html/Test/view_products.php on line 60

SKU_ProductID SKU_MerchSKUID Game_Title Platform Genre Weight Supplier Supplier_Price Cashback Cashback_Amount

 

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /websites/123reg/LinuxPackage21/co/ns/ol/console-entertainment.co.uk/public_html/Test/view_products.php on line 81

 

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in /websites/123reg/LinuxPackage21/co/ns/ol/console-entertainment.co.uk/public_html/Test/view_products.php on line 103

 

 

 

Warning: mysqli_error() [function.mysqli-error]: Couldn't fetch mysqli in /websites/123reg/LinuxPackage21/co/ns/ol/console-entertainment.co.uk/public_html/Test/view_products.php on line 141

 

My current coding

 

<?php # View products

$page_title = 'View Inventory';

require_once ('connect.php');

$display = 35; // Number of records per page

if (isset($_GET['p']) && is_numeric($_GET['p'])) { // How many pages there are

$pages = $_GET['p'];

} else {

// Count number of records

$q = "SELECT COUNT(Game_Title) FROM Products";

$r = mysqli_query ($dbc, $q);

$row = mysqli_fetch_array ($r, MYSQLI_NUM);

$records = $row[0];

// Calculate number of pages

if ($records > $display) {// More than one page

$pages = ceil ($records/$display);

} else {

$pages = 1;

}

} // End of p IF

if (isset($_GET['s']) && is_numeric ($_GET['s'])) {

$start = $_GET['s'];

} else {

$start = 0;
}

//make query

$q = "SELECT SKU_ProductID, SKU_MerchSKUID, Game_Title, Platform, Genre, Weight, Supplier, Supplier_Price, Cashback, Cashback_Amount AS dr, FROM Products ORDER BY Game_Title ASC LIMIT $start, $display";

$r = mysqli_query($dbc, $q) or trigger_error('MySQLi Error: ' . mysqli_error());


echo '<TABLE ALIGN="CENTER" cellspacing="0" cellpadding="5" width="75%">

<TR>
<TD align="left"><b>SKU_ProductID</b></td>
<TD align="left"><b>SKU_MerchSKUID</b></td>
<TD align="left"><b>Game_Title</b></td>
<TD align="left"><b>Platform</b></td>
<TD align="left"><b>Genre</b></td>
<TD align="left"><b>Weight</b></td>
<TD align="left"><b>Supplier</b></td>
<TD align="left"><b>Supplier_Price</b></td>
<TD align="left"><b>Cashback</b></td>
<TD align="left"><b>Cashback_Amount</b></td>
</TR>
';

$bg = '#eeeeee';

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch background color

echo ' <tr bgcolor="' . $bg . '">

<td align="left">' . $row['SKU_ProductID'] . '</td>
<td align="left">' . $row['SKU_MerchSKUID'] . '</td>
<td align="left">' . $row['Game_Title'] . '</td>
<td align="left">' . $row['Platform'] . '</td>
<td align="left">' . $row['Genre'] . '</td>
<td align="left">' . $row['Weight'] . '</td>
<td align="left">' . $row['Supplier'] . '</td>
<td align="left">' . $row['Supplier_Price'] . '</td>
<td align="left">' . $row['Cashback'] . '</td>
<td align="left">' . $row['Cashback_Amount'] . '</td>
</tr>
';
}

echo '</table>';

mysqli_free_result ($r);

mysqli_close($dbc);

if ($pages > 1) {

echo '<br /><p>';

$current_page = ($start/$display) + 1;

if ($current_page != 1) {

echo '<a href="view_products.php"?s=' . ($start - $display) . '&p=' . $pages . ' ">Previous</a> ';

for ($i = 1; $i <= $pages; $i++) {

	if ($i != $current_page) {

		echo '<a href="view_products.php"?s=' . (($display * ($i - 1))) . '&p=' . $pages . '"> ' , $i . '</a> ';

	} else {

		echo $i . ' ';

	}

}

if ($current_page != $pages) {

	echo '<a href="view_products.php"?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';

}

echo '</p>';

}}
?>

 

 

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.