I am trying to output records from a mysql db with simple pagination, however no information is output at all, and no errors are displayed. The error seems to be with the $data query and using mysql_num_rows, as it worked(aside from paginating) before this. Is there some basic error in my logic?
<?php
$query ='';
if (isset($_GET["query"])) {
$query = $_GET["query"];
}
if (isset($_GET["pg"])) {
$pg = $_GET["pg"];
} else $pg = 1;
$con = mysql_connect("localhost","vweb_50","uashozoo3u");
//echo "test connection\n";
if(!$con) {
die('Connection failed because of' .mysql_error());
}
mysql_query('SET NAMES utf8');
mysql_select_db("vweb_50",$con);
$table = 'AUCTIONS';
$page_rows = 10;
$max = ' limit ' .($pg - 1) * $page_rows .', ' .$page_rows;
$countQuery = "SELECT ARTICLE_NO FROM {$table} WHERE upper(ARTICLE_NAME) LIKE '%" . $query . "%'";
$data = mysql_query($countQuery) or die(mysql_error());
$rows = getRowsByArticleSearch($query, $table, $max);
$rowcount = mysql_num_rows($data);
$last = ceil($rowcount/$page_rows);
echo "<h1>{$query} Auctions</h1>" . "\n";
// printing table rows
foreach ($rows as $row) {
$pk = $row['ARTICLE_NO'];
echo '<tr>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['shortDate'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['ARTICLE_NAME'].'</a></td>' . "\n";
echo '</tr>' . "\n";
}
echo " <a href='$self?cmd=GetRecordSet&query=$query&pg=1'> <<-First</a> ";
echo " ";
$previous = $pg-1;
echo " <a href='$self?cmd=GetRecordSet&query=$query&pg=$previous'> <-Previous</a> ";
echo "---------------------------";
$next = $pg+1;
echo " <a href='$self?cmd=GetRecordSet&query=$query&pg=$next'>Next -></a> ";
echo " ";
echo " <a href='$self?cmd=GetRecordSet&query=$query&pg=$last'>Last ->></a> ";
function getRowsByArticleSearch($searchString, $table, $max) {
$searchString = mysql_real_escape_string($searchString);
$recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d.%m.%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM {$table} WHERE upper(ARTICLE_NAME) LIKE '%" . $searchString . "%'" . $max;
$result = mysql_query($recordsQuery);
//print_r($recordsQuery);
if($result === false) {
echo mysql_error();
}
$rows = array();
while($row = mysql_fetch_assoc($result)) {
//echo "<p>test while <p>";
$rows[] = $row;
}
return $rows;
mysql_free_result($result);
}