views:

74

answers:

2

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);
}
A: 

What I normally do when I have a problem like that is:

  • Echo the complete SQL statement to the screen
  • Enter the shell on the server
  • Enter the MySQL command prompt ( $mysql -uUSER -pPASSWORD )
  • Copy and paste the SQL statement and start toying with it to see what´s wrong

You'll find out soon enough why you have an empty record set.

Works for me.

jeroen
The problem is the query is perfect, and returns exactly what it should in phpmyadmin.
Joshxtothe4
No errors in the log-file either?
jeroen
A: 

The problematic line is

$data = mysql_query() or die(mysql_error());

This executes a blank query that fails and exits further processing. Therefore, all further top-level echos are not called.

phihag
Actually on my side I was using $countQuery as the query, which has been reflected now in my Post, and the problem still remains. Thankyou for your answer however.
Joshxtothe4
I still have no idea what is causing this error.
Joshxtothe4