Printing query result from MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Muddasir
    New Member
    • Jun 2007
    • 49

    #1

    Printing query result from MySQL

    i am having problem in printing the query result from MySQL db...

    actually i am developing a very simple search module.
    when the user select category from the given categories in drop down list....the coresponding product against that category shuld be displayed....

    table= search

    fields

    product_id, product_name, category_name, price
    [code=php]
    <?
    $cat=$_POST['category']

    $query = "SELECT product_name FROM search WHERE category_name=$ cat";

    $result=mysql_q uery($query);

    echo "the products are:'$res'";

    ?>[/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    #2
    Hi Muddasir and welcome to TSDN,

    I have taken a look at your code and whilst you have done the mysql_query, you appear to be missing the fetch.. This maybe due to you snippet missing the relevent line of code. If that the case, can you post the code with the query and result processing logic.. If its not the case a broad pointer would be to take a look at mysql_result and mysql_fetch functions in the php.net manual..

    Hope this helps

    Purple

    Comment

    • Muddasir
      New Member
      • Jun 2007
      • 49

      #3
      [code=php]<?php
      include("datast ore.php");

      $cat=$_POST["category"];


      $query = "select * from search";
      $result=mysql_q uery($query);
      $num=mysql_numr ows($result);


      $flag=0;
      $i=0;

      while($i<$num)
      {
      $cat_var=mysql_ result($result, $i,"category_na me");

      If($cat==$cat_v ar)

      $flag=1;
      $i++;
      }

      if ($flag==1)
      {

      $query = "SELECT product_name FROM search WHERE category_name=$ cat";

      $result=mysql_q uery($query);
      echo "the result is:'$res'";

      }
      else
      {

      echo "no reult found";

      }
      ?>[/code]

      i dont know why i am unable to print the result of that query.......sha ll i use mysql_fetch()

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by Muddasir
        <?php
        include("datast ore.php");

        $cat=$_POST["category"];


        $query = "select * from search";
        $result=mysql_q uery($query);
        $num=mysql_numr ows($result);


        $flag=0;
        $i=0;

        while($i<$num)
        {
        $cat_var=mysql_ result($result, $i,"category_na me");

        If($cat==$cat_v ar)

        $flag=1;
        $i++;
        }

        if ($flag==1)
        {

        $query = "SELECT product_name FROM search WHERE category_name=$ cat";

        $result=mysql_q uery($query);
        echo "the result is:'$res'";

        }
        else
        {

        echo "no reult found";

        }
        ?>

        i dont know why i am unable to print the result of that query.......sha ll i use mysql_fetch()
        [CODE=php]
        # Rest of the code
        $query = "SELECT product_name FROM search WHERE category_name=' $cat'";// Make sure to use ' ' if this $cat is not a int value.

        $result=mysql_q uery($query);
        while($result_r ow = mysql_fetch_ass oc($result))
        {
        echo "the result is :".$result_r ow['product_name']."<br>";
        }
        # Rest of the code
        [/CODE]

        Please wrap your codings with relevant [CODE] tags.

        Comment

        • Purple
          Recognized Expert Contributor
          • May 2007
          • 404

          #5
          Thanks Ajaxrand - took me a little longer to work out
          Last edited by Purple; Jun 13 '07, 08:28 AM. Reason: Beaten to it by Ajaxrand

          Comment

          • Muddasir
            New Member
            • Jun 2007
            • 49

            #6
            many thanks Purple and Ajaxrand
            its working now..
            u guys are great
            thanks once again

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Originally posted by Muddasir
              many thanks Purple and Ajaxrand
              its working now..
              u guys are great
              thanks once again
              You are welcome Muddasir and keep in touch with TSDN !

              Purple, I appreciate your Post
              Thanks,
              Ajaxrand

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                If you find yourself doing more work with PHP and MySQL, you may want to take a look at the article I wrote on building a MySQL class to make performing these common functions much more accessible.

                Comment

                Working...