Jump to content

[SOLVED] Whats wrong with this code.


menios

Recommended Posts

I m trying to retrieve data from my tbl_product where the category id =1

In the database my category table has 6 ids and i have around a 1000 different items in table product.

The only display im getting is a blank box on my browser.

Here is the code if someone could have a look.

Thanks

 

 

[hr]

<html>


<body>
<?php
@ $rpp;		//Records Per Page
@ $cps;		//Current Page Starting row number
@ $lps;		//Last Page Starting row number

/////////////////////////////////////////////////////////////////////////////////
//Database connection
/////////////////////////////////////////////////////////////////////////////////
$user="root";
$password="*****";
$database="vivlia";
$tablename="tbl_product";
@mysql_connect("localhost",$user,$password);

@mysql_select_db($database) or die( "Unable to select database");

if (isset($_GET['1']))
{
    $cat = $_GET['1'];       
                              
    $sql = "SELECT * FROM tbl_product WHERE cat_id = '$cat'";
    echo $sql;


/////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
//Load for the first time First result printed user will click next to change
/////////////////////////////////////////////////////////////////////////////////	
if(empty($_GET["cps"])) 
{
	$cps = "0";
} 
else
{
	$cps = $_GET["cps"];
}
/////////////////////////////////////////////////////////////////////////////////

$a = $cps+1; 

$rpp = "15";

$lps = $cps - $rpp; //Calculating the starting row number for previous page

/////////////////////////////////////////////////////////////////////////////////
//Check for previus link If the user is viewing the first set of data then the link will
//be disabled, if on the next set then it will carry the $lps in its link and enable it
if ($cps <> 0)
{
	$prv =  "<a href='cookery.php?cps=$lps'>Previous</a>";
}
else	
{
	$prv =  "<font color='cccccc'>Previous</font>";
}
/////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
$q="Select SQL_CALC_FOUND_ROWS * from tbl_product limit $cps, $rpp";
$rs=mysql_query($q) or die(mysql_error());
$nr = mysql_num_rows($rs); //Number of rows found with LIMIT in action

$q0="Select FOUND_ROWS()";
$rs0=mysql_query($q0) or die(mysql_error());
$row0=mysql_fetch_array($rs0);
$nr0 = $row0["FOUND_ROWS()"]; //Number of rows found without LIMIT in action

/////////////////////////////////////////////////////////////////////////////////
//Check  the user has reached thelast page of the items	
if (($nr0 < 10) || ($nr < 10))
{
   		$b = $nr0;
}
else
{
	$b = ($cps) + $rpp;
}
}
/////////////////////////////////////////////////////////////////////////////////

?>
<br>
<table border="1" cellpadding="4" cellspacing="1" width="100%" align="center">
  
  <tr><td bgcolor="#000080" align='Center'><b><font face="verdana" color="#FFFFFF">Title</font></b></td>
  <td bgcolor="#000080" align='Center'><b><font face="verdana" color="#FFFFFF">test</font></b></td>
</tr>
<?php
while ($row=mysql_fetch_array($rs))
{
	/////////////////////////////////////////////////////////////////////////////////
	//loop after fullfilling the above SQL criteria.
	/////////////////////////////////////////////////////////////////////////////////
	$cps = $cps +1; 

	$val=$row["pd_name"];
	$val2=$row ["pd_price"];
	echo "<tr><td width='10%' align='center'><font face=verdana><img</font></td><td align='center'><font fave=verdana>$val $val2</font></td></tr>";  

}

echo "<tr><td align='right' colspan=2>$prv";


//Check whether there is a Next link if not disable

if ($cps == $nr0)
{ 	
	echo "  |  <font color='CCCCCC'>Next</font>";
}
else
{
	if ($nr0 > 5)
	{ 
		echo "  |  <a href='cookery.php?cps=$cps&lps=$lps'>Next  </a>"; 
	}
}


?>
</td>
</tr>
</table>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/79686-solved-whats-wrong-with-this-code/
Share on other sites

the most obvious mistake you have made is :

mysql_select_db function should have 2 arguments,  you've put only one :

 

 

$conn = mysql_connect("localhost",$user,$password)

mysql_select_db($database,$conn);

 

and so do mysql_query !!!

mysql_query($sql,$conn);

 

when you are testing a page, remove all "@" sign to see what is the error , then if you want put them back !

 

i didn't read too much carefully your code, just because you have this obvious mistakes, if not solved, come back and tell us.

I ve changed the 2 arguments to this

But still empty box and no error..

 

$tablename="tbl_product";
$conn=mysql_connect("localhost",$user,$password);

mysql_select_db($database,$conn) or die( "Unable to select database");
mysql_query($sql,$conn);
if (isset($_GET['1']))
{
    $cat = $_GET['1'];      //  for simplicity only. 
                              
    $sql = "SELECT * FROM tbl_product WHERE cat_id = '$cat'";
    echo $sql;

I ve changed the code and now i m getting results but sadly its all the database entries.  and i ve been trying to limit the results by category id

[hr]

<html>


<body>
<?php
@ $rpp;		//Records Per Page
@ $cps;		//Current Page Starting row number
@ $lps;		//Last Page Starting row number

/////////////////////////////////////////////////////////////////////////////////
//Database connection
/////////////////////////////////////////////////////////////////////////////////
$user="root";
$password="asere";
$database="smalltownbooks";
$tablename="tbl_product";
$conn=mysql_connect("localhost",$user,$password);

mysql_select_db($database,$conn) or die( "Unable to select database");
mysql_query($sql,$conn);
if (isset($_GET['1']))
{
    $cat = $_GET['1'];      //  for simplicity only. 
                              
    $sql = "SELECT * FROM tbl_product WHERE cat_id = '$cat'";
    echo $sql;

}
/////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
//Load for the first time First result printed user will click next to change
/////////////////////////////////////////////////////////////////////////////////	
if(empty($_GET["cps"])) 
{
	$cps = "0";
} 
else
{
	$cps = $_GET["cps"];
}
/////////////////////////////////////////////////////////////////////////////////

$a = $cps+1; 

$rpp = "15";

$lps = $cps - $rpp; //Calculating the starting row number for previous page

/////////////////////////////////////////////////////////////////////////////////
//Check for previus link If the user is viewing the first set of data then the link will
//be disabled, if on the next set then it will carry the $lps in its link and enable it
if ($cps <> 0)
{
	$prv =  "<a href='cookery.php?cps=$lps'>Previous</a>";
}
else	
{
	$prv =  "<font color='cccccc'>Previous</font>";
}
/////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
$q="Select SQL_CALC_FOUND_ROWS * from tbl_product limit $cps, $rpp";
$rs=mysql_query($q) or die(mysql_error());
$nr = mysql_num_rows($rs); //Number of rows found with LIMIT in action

$q0="Select FOUND_ROWS()";
$rs0=mysql_query($q0) or die(mysql_error());
$row0=mysql_fetch_array($rs0);
$nr0 = $row0["FOUND_ROWS()"]; //Number of rows found without LIMIT in action

/////////////////////////////////////////////////////////////////////////////////
//Check  the user has reached thelast page of the items	
if (($nr0 < 10) || ($nr < 10))
{
   		$b = $nr0;
}
else
{
	$b = ($cps) + $rpp;
}

/////////////////////////////////////////////////////////////////////////////////

?>
<br>
<table border="1" cellpadding="4" cellspacing="1" width="100%" align="center">
  
  <tr><td bgcolor="#000080" align='Center'><b><font face="verdana" color="#FFFFFF">Title</font></b></td>
  <td bgcolor="#000080" align='Center'><b><font face="verdana" color="#FFFFFF">test</font></b></td>
</tr>
<?php
while ($row=mysql_fetch_array($rs))
{
	/////////////////////////////////////////////////////////////////////////////////
	//loop after fullfilling the above SQL criteria.
	/////////////////////////////////////////////////////////////////////////////////
	$cps = $cps +1; 

	$val=$row["pd_name"];
	$val2=$row ["pd_price"];
	echo "<tr><td width='10%' align='center'><font face=verdana><img</font></td><td align='center'><font fave=verdana>$val $val2</font></td></tr>";  

}

echo "<tr><td align='right' colspan=2>$prv";


//Check whether there is a Next link if not disable

if ($cps == $nr0)
{ 	
	echo "  |  <font color='CCCCCC'>Next</font>";
}
else
{
	if ($nr0 > 5)
	{ 
		echo "  |  <a href='cookery.php?cps=$cps&lps=$lps'>Next  </a>"; 
	}
}


?>
</td>
</tr>
</table>
</body>
</html>

The point is to limit the display results but it just displays everything

and i think i m limiting it by

 

 

if (isset($_GET['1']))
{
    $cat = $_GET['1'];      //  for simplicity only. 
                              
    $sql = "SELECT * FROM tbl_product WHERE cat_id = '$cat'";
    echo $sql;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.