Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP with MySQL return query 1

Status
Not open for further replies.

LuckySyringe

Technical User
Oct 11, 2005
35
0
0
US

I need to limit my rows as well as print out the data inversely, so the most recent additions come out on top and there is only six results on one page. This is what I have so far:
Code:
<?php
$start = $_GET['start'];
$offset = $_GET['offset'];
if (!$start || !$offset) {
	$start = 0;
	$offset = 6;}
$end = $start + $offset;
$dbHost = 'localhost';
$dbUser = 'tempuser';
$dbPass = 'temppass';
$dbDatabase = 'tempdatabase';
$dbTable = 'temptable';

$dbConnection = @mysql_connect($dbHost, $dbUser, $dbPass) or die("Could not connect: ".mysql_error()."<br />");
				@mysql_select_db("$dbDatabase") or die("Could not select database.<br />");
//  I want this so that the start number and the offset are in the url
//  and a link under the printed results with new start and offset numbers
$query = "SELECT * FROM ".$dbTable." LIMIT ".$start.",".$offset;
$result = @mysql_query($query) or die ('Query failed: '.mysql_error().'<br />');
$num_results = @mysql_num_rows($result);

for ($i=0; $i <$num_results; $i++) {
	$row = @mysql_fetch_assoc($result);
	echo "\t<p class=\"item\">\n".
		 "\t\t<a href=\"".$dbTable."/item.php?id=".stripslashes($row['id'])."\">".stripslashes($row['id'])."</a>\n".
		 "\t\t<span class=\"item-title\"><a href=\"".$dbTable."/item.php?id=".stripslashes($row['id'])."\">".htmlspecialchars(stripslashes($row['title']))."</a></span>".
		 "\t\t<span class=\"item-info\">".stripslashes($row['description'])."</span>".
		 "\t</p>\n";}
/*
$start = $i + 1;
echo "<p>".$i." - ".$end." of ".$num_results." "; // I realize this is redundant
//  This is where the previous / next page will be printed
switch ($i) {
	case $i < 8;
		echo "<a href=\"".$_SERVER['PHP_SELF']."?page=list&start=".$start."&offset=".$offset."\">Next Page</a>";
		break;
	case $i > 8;
		$prev = $start - $offset;
		echo "<a href=\"".$_SERVER['PHP_SELF']."?page=list&start=".$prev."&offset=".$offset."\">Previous Page</a> <a href=\"".$_SERVER['PHP_SELF']."?page=list&start=".$start."&offset=".$offset."\">Next Page</a>";
		break;
}
echo "</p>";
*/
@mysql_free_result($result);
@mysql_close($dbConnection);
?>
Any help would be greatly appreciated.
 
Thanks for clarifying - I'm kinda self-taught (as you may have already noticed), but theres still one problem...
My error log reads:
php error log said:
[21-Dec-2005 16:57:26] PHP Notice: Undefined offset: 2 in C:\ on line 16
13 times (per page load), and line 16 reads:
Code:
$array["added_".uniqid()]= $row[2];
from
Code:
$query = "SHOW TABLES FROM $dbDatabase";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_row($result)) {
    $tableName = $row[0];
    $query_2 = "SELECT * FROM $tableName ORDER BY `added` DESC LIMIT 6";
    $result_2 = mysql_query($query_2) or die (mysql_error());
    while ($row_2 = mysql_fetch_assoc($result_2)){
            $val['tableName'] = $tableName;
            $array["added_".uniqid()]= $row_2[2];
    }
}
... and I'm confused now about what you posted, I've changed line 15 to your example, but
jpadie said:
the tablename will come out blank because you are not referencing the variables by its array element
which array element do I have it reference?

-> LuckySyringe
 
that's because the name of the array is $row_2 rather than $row. my mistake.

change the line
Code:
$array["added_".uniqid()]= $row[2];// line 16

to
Code:
$array["added_".uniqid()]= $row_2;// line 16
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top