Page 1 of 1

Results not being echoed

Posted: Mon Apr 11, 2005 7:05 am
by mohson
can anyone see why my results are not being echoed from line 129 :

Code: Select all

<?php


// config-------------------------------------
$host = "xxxxxxxxxxx"; //your database host
$user = "xxx"; // your database user name
$pass = "xxxxxxxxxxx"; // your database password
$db = "contact_management_system"; // your database name

$filename = "index.php"; // name of this file
$option = array (5, 10, 20, 50, 100, 200);
$default = 100; // default number of records per page
$action = $_SERVER['PHP_SELF']; // if this doesn't work, enter the filename


// database query. Enter your query here

 $query = 	"SELECT 
		 stu_id, name,organisation,website 
		 FROM studentwebsites ORDER BY organisation "; 


// end config---------------------------------

	
$opt_cnt = count ($option);

$go = $_GET['go'];
// paranoid
if ($go == "") {
$go = $default;
}
elseif (!in_array ($go, $option)) {
$go = $default;
}
elseif (!is_numeric ($go)) {
$go = $default;
}
$nol = $go;
$limit = "0, $nol";
$count = 1; 

echo "<form name=\"form1\" id=\"form1\" method=\"get\" action=\"$action\">\r\n";
echo "<select name=\"go\" id=\"go\">\r\n";

for ($i = 0; $i <= $opt_cnt; $i ++) {
if ($option[$i] == $go) {
echo "<option value=\"".$option[$i]."\" selected=\"selected\">".$option[$i]."</option>\r\n";
} else {
echo "<option value=\"".$option[$i]."\">".$option[$i]."</option>\r\n";
}
}

echo "</select>\r\n";
echo "<input type=\"submit\" name=\"Submit\" id=\"Submit\" value=\"Go\" />\r\n";
echo "</form>\r\n";

$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect");
mysql_select_db ($db) or die ("Unable to select database $db");



// control query------------------------------
/* this query checks how many records you have in your table.
I created this query so we could be able to check if user is
trying to append number larger than the number of records
to the query string.*/
$off_sql = mysql_query ("$query") or die ("Error in query: $off_sql".mysql_error());
$off_pag = ceil (mysql_num_rows($off_sql) / $nol);
//-------------------------------------------- 


$off = $_GET['offset'];
//paranoid
if (get_magic_quotes_gpc() == 0) {
$off = addslashes ($off);
}
if (!is_numeric ($off)) {
$off = 1;
}
// this checks if user is trying to put something stupid in query string
if ($off > $off_pag) {
$off = 1;
}

if ($off == "1") {
$limit = "0, $nol";
}
elseif ($off <> "") {
for ($i = 0; $i <= ($off - 1) * $nol; $i ++) {
$limit = "$i, $nol";
$count = $i + 1;
}
} 




// Query to extract records from database.
$sql = mysql_query ("$query LIMIT $limit") or die ("Error in query: $sql".mysql_error()); 



// Define your colors for the alternating rows

$color1 = "#ADD8E6";$color2 = "#E0FFFF";
$color = $color2;


echo "<table width=\"50%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">
	<tr>
	<td 			<b>Id</b>					</td>
    <td 			<b>Name</b>					</td>
	<td>			<b>Organisation</b>			</td>
	<td>			<b>Website</small></b>		</td></tr>"; 


while ($row = mysql_fetch_object($sql)) 






{($color==$color2)? $color = $color1 : $color = $color2;



echo "<tr bgcolor=\"$color\"><td>".$count . '</td>
<td>'.$row->org_id.'</td>
<td>'.$row->name.'</a></td>
<td>'. $row->organisation .'</td>
<td>'. $row->website .'</tr>';


$count += 1;
}

echo "</table>"; 

echo "<br /><br />\r\n";
if ($off <> 1) {
$prev = $off - 1;
echo "[ < <a href=\"$filename?offset=$prev&go=$go\">prev</a> ] \r\n";
}
for ($i = 1; $i <= $off_pag; $i ++) {
if ($i == $off) {
echo "[<b> $i </b>] \r\n";
} else {
echo "[ <a href=\"$filename?offset=$i&go=$go\">$i</a> ] \r\n";
}
}
if ($off < $off_pag) {
$next = $off + 1;
echo "[ <a href=\"$filename?offset=$next&go=$go\">next</a> > ] \r\n";
}

echo "<br /><br />\r\n";
echo "Page $off of $off_pag<br />\r\n";



?>

Posted: Mon Apr 11, 2005 9:03 am
by CoderGoblin

Code: Select all

$sql = mysql_query (&quote;$query LIMIT $limit&quote;) or die (&quote;Error in query: $sql&quote;.mysql_error());
As $sql is not what I would recommend for a variable name I would change it to this code

Code: Select all

$result = mysql_query (&quote;$query LIMIT $limit&quote;) or die (&quote;Error in query: &#1111;$query] &quote;.mysql_error());
echo(&quote;Number rows= &quote;.mysql_num_rows($result).&quote;&lt;br /&gt;&quote;);
The echo is just to check you actually get something from your DB. You can remove it later. Remember 0 rows does not mean a failure. It could be the SQL conditions are wrong. I have also changed the die statement to reflect the query, not $sql.

If you get more than 0 returning let me know and I'll take another look.

Posted: Mon Apr 11, 2005 9:29 am
by mohson
thanks Goblin... the echo returns Numrows = 0

do you think the problem is in my sql then as ive used this exact code before and its worked fine on other applications ive built - it must be the sql mustn it??

Posted: Mon Apr 11, 2005 9:32 am
by mohson
SOMEONE NEEDS TO HIT ME ROUND THE HEAD WITH A BASEBALL BAT,, Ive been soooo stupid!!!! Ofcourse nothing is going to display - theres no DATA TO displayed how could I be soooo stupid.....ahhhhh..aghhhhh... Nevermind

Posted: Mon Apr 11, 2005 9:48 am
by CoderGoblin
You know I saw an animated smilie once of a face hitting itself with a hammer (can't remember where). I bet it could be used frequently on these boards.

Further to the echo I would recommend the following structure whenever processing data from SQL...

Code: Select all

// Query to extract records from database.
$result = mysql_query ("$query LIMIT $limit") or die ("Error in query: $query".mysql_error()); 
 
if (@mysql_num_rows($result) { 
  // Do things
} else {
  echo("No results found");
}
Please also note you do not close the final <td> on each row.

Posted: Tue Apr 12, 2005 8:15 am
by mohson
Coder Goblin could you have a look at this again please because I now do have records but they are still not bwing displayed any reason as to why.
the num rows clearly shows two records but nothing is being displayed on screen - cheers

code below:

Code: Select all

<?php


// config-------------------------------------
$host = "vega.soi.city.ac.uk"; //your database host
$user = "xxxx"; // your database user name
$pass = "xxxx"; // your database password
$db = "xxxxx"; // your database name

$filename = "index.html"; // name of this file
$option = array (5, 10, 20, 50, 100, 200);
$default = 100; // default number of records per page
$action = $_SERVER['PHP_SELF']; // if this doesn't work, enter the filename


// database query. Enter your query here

 $query = 	"SELECT 
		 stu_id, name,organisation,website 
		 FROM studentwebsites ORDER BY organisation "; 


// end config---------------------------------

	
$opt_cnt = count ($option);

$go = $_GET['go'];
// paranoid
if ($go == "") {
$go = $default;
}
elseif (!in_array ($go, $option)) {
$go = $default;
}
elseif (!is_numeric ($go)) {
$go = $default;
}
$nol = $go;
$limit = "0, $nol";
$count = 1; 

echo "<form name=\"form1\" id=\"form1\" method=\"get\" action=\"$action\">\r\n";
echo "<select name=\"go\" id=\"go\">\r\n";

for ($i = 0; $i <= $opt_cnt; $i ++) {
if ($option[$i] == $go) {
echo "<option value=\"".$option[$i]."\" selected=\"selected\">".$option[$i]."</option>\r\n";
} else {
echo "<option value=\"".$option[$i]."\">".$option[$i]."</option>\r\n";
}
}

echo "</select>\r\n";
echo "<input type=\"submit\" name=\"Submit\" id=\"Submit\" value=\"Go\" />\r\n";
echo "</form>\r\n";

$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect");
mysql_select_db ($db) or die ("Unable to select database $db");



// control query------------------------------
/* this query checks how many records you have in your table.
I created this query so we could be able to check if user is
trying to append number larger than the number of records
to the query string.*/
$off_sql = mysql_query ("$query") or die ("Error in query: $off_sql".mysql_error());
$off_pag = ceil (mysql_num_rows($off_sql) / $nol);
//-------------------------------------------- 


$off = $_GET['offset'];
//paranoid
if (get_magic_quotes_gpc() == 0) {
$off = addslashes ($off);
}
if (!is_numeric ($off)) {
$off = 1;
}
// this checks if user is trying to put something stupid in query string
if ($off > $off_pag) {
$off = 1;
}

if ($off == "1") {
$limit = "0, $nol";
}
elseif ($off <> "") {
for ($i = 0; $i <= ($off - 1) * $nol; $i ++) {
$limit = "$i, $nol";
$count = $i + 1;
}
} 




// Query to extract records from database.
$result = mysql_query ("$query LIMIT $limit") or die ("Error in query: [$query] ".mysql_error());echo("<b>Number of Websites</b> = ".mysql_num_rows($result)."<br />");



// Define your colors for the alternating rows

$color1 = "0099FF";$color2 = "0000FF";
$color = $color2;


echo "<table width=\"50%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">
	<tr>
	<td> 			<b>Id</b>					</td>
    <td 			<b>Name</b>					</td>
	<td>			<b>Organisation</b>			</td>
	<td>			<b>Website</small></b>		</td></tr>"; 


while ($row = mysql_fetch_object($sql)) 






{($color==$color2)? $color = $color1 : $color = $color2;




echo "<tr bgcolor=\"$color\"><td>".$count . '</td>
<td>'.$row->stu_id.'</td>
<td>'.$row->name.'</td>
<td>'. $row->organisation .'</td>
<td>'. $row->website .'</tr>';



$count += 1;
}

echo "</table>"; 

echo "<br /><br />\r\n";
if ($off <> 1) {
$prev = $off - 1;
echo "[ < <a href=\"$filename?offset=$prev&go=$go\">prev</a> ] \r\n";
}
for ($i = 1; $i <= $off_pag; $i ++) {
if ($i == $off) {
echo "[<b> $i </b>] \r\n";
} else {
echo "[ <a href=\"$filename?offset=$i&go=$go\">$i</a> ] \r\n";
}
}
if ($off < $off_pag) {
$next = $off + 1;
echo "[ <a href=\"$filename?offset=$next&go=$go\">next</a> > ] \r\n";
}

echo "<br /><br />\r\n";
echo "Page $off of $off_pag<br />\r\n";



?>

Posted: Tue Apr 12, 2005 8:17 am
by CoderGoblin
Can you post your current code...

Posted: Tue Apr 12, 2005 8:20 am
by mohson
sure, its there above - got rid of that extra <a/> and replaced org_id with stu_id as i was reffering to the incorrect field, still no joy

Posted: Tue Apr 12, 2005 8:55 am
by CoderGoblin
I asked you to change the $sql to $result as it is more meaningful. Unfortunately you still use $sql for the mysql_fetch...