Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2014
    Location
    coimbatore tamilnadu
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up rows limit helpwith mysql select query

    include'db_conect.php'


    $rowsperpage=5;
    $pageNum = 1;

    if
    (isset($_GET['page']))
    {
    $pageNum = $_GET['page'];
    }
    $offset = ($pageNum - 1) * $rowsperpage;
    select querie
    $query="select product_name,p_id,product_image from product_general where pause ='off' and stock <> 'out of stock' order by decs limit $offset,$rowsperpage";
    whats the mistake pls reply
    thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Make sure you add the proper error reporting to your mysql_query/mysqli_query calls. There is a mysql syntax error in the order by; decs isn't a valid sort order. I'd presume you meant desc. Also, you'll need something to order on, or that will still fail.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts
    You can also help yourself to catch those mistakes and also help you and others read and follow your code better if you use caps like so

    PHP Code:
    $query="SELECT product_name,p_id,product_image FROM product_general WHERE pause ='off' and stock <> 'out of stock' ORDER BY fieldname DESC LIMIT $offset,$rowsperpage"

  • Users who have thanked durangod for this post:

    PRIYANKAVIVEKAN (01-24-2014)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Quote Originally Posted by PRIYANKAVIVEKAN View Post
    include'db_conect.php'


    $rowsperpage=5;
    $pageNum = 1;

    if
    (isset($_GET['page']))
    {
    $pageNum = $_GET['page'];
    }
    $offset = ($pageNum - 1) * $rowsperpage;
    select querie
    $query="select product_name,p_id,product_image from product_general where pause ='off' and stock <> 'out of stock' order by decs limit $offset,$rowsperpage";
    What is the meaning of the code in bold red there??? So far as I can see, it is just trash coding.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Jan 2014
    Location
    coimbatore tamilnadu
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up page numbering how

    when i click the cat it displays all of the products in that cat same page
    pls help in page link thanks
    priiyanka

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    PRIYANKAVIVEKAN (01-28-2014)

  • #7
    New Coder
    Join Date
    Jan 2014
    Location
    coimbatore tamilnadu
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    <?php
    include("db_connect.php");
    //this checks to see if there is page number.if not it will set it to page1
    if(!(isset($pagenum)))
    {
    $pagenum=1;
    }
    //here we count the number of results
    //edit $data to be your query
    /*$query="select * nikki.product_name,p_id,product_image from product_general where cat_id='1";*/
    $query="select count() as num from nikki.product_general";

    $rows = mysql_num_rows($query);
    //this is the number of results displayed per page
    $page_rows =4;
    //this tells us the pagenumber of the lastpage
    $last=ceil($rows/$page_rows);
    //this makes the page number isn't below one,or more than our max pages

    if ($pagenum < 1)
    {
    $pagenum = 1;
    }
    elseif ($pagenum > $last)
    {
    $pagenum = $last;
    }
    $max = 'limit' .($pagenum - 1) * $page_rows .',' .$page_rows;

    //this is the query again the same one he only difference is we add $max to it
    $query_p=mysql_query("select * from nikki.product_general $max") or die(mysql_error());


    $data=mysql_query("select * from nikki.product_general $max where cat_id=".$catid ,$con);

    ?>
    <ul>
    <?php
    while($row = mysql_fetch_array($data))
    {
    //echo $row['product_name']. " - ". $row['p_id'];
    $name = $row['product_name'];
    $product_image = $row['product_image'];
    $id=$row['p_id'];
    $href=$product_view_file.$id;
    $image_url = $host.$product_image;

    echo "<li class='product' style='float:left;list-style-type:none;width:300px;padding:5px 0;'> <div style='width:300px;'>";
    echo $name;
    echo "<a href=".$href."><img class='product_img' src=".$image_url." height='115' width='280'></a>";

    echo "</li>";
    echo " </br>";
    echo "<p>";

    echo "...page $pagenum of $last...<p>";
    //first we check if we are on page1. if we are then we donot need a previous page or first page so we do nothing . if we aren't then we generate links to the first page, and to the previous page.
    if ($pagenum == 1)
    {

    }
    else
    echo "ahref='{http://localhost/niki['products_view']}?pagenum=1'><<-first</a>";
    echo "";
    $previous=$pagenum-1;
    echo "ahref='{http://localhost/niki['products_view']}?previous'><-previous</a>";
    echo "----";
    }
    if($pagenum == $last)
    {

    }
    else{
    $next=$pagenum+1;
    echo "ahref='{http://localhost/niki['products_view']}?pagenum=$next'><<-next</a>";
    echo "";
    echo "ahref='{http://localhost/niki['products_view']}?pagenum=$last'><<-last</a>";


    }



    ?>

    </ul>
    some
    Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\niki\show.php on line 13
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-4,4' at line 1


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •