mysql_num_rows error...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luckysanj
    New Member
    • May 2009
    • 68

    mysql_num_rows error...

    [code=php]
    $sql_result=mys ql_query($query ) or die("Error in Checking User".mysql_err or());
    echo $no=mysql_num_r ows($sql_result );
    if($no<>1)
    {
    return(false);
    }
    else
    {
    return (true);
    }
    [/code]
    In this code i have get this type of error:
    [code=html]
    Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource in D:\xampp\htdocs \my\test\query\ function.php on line 2
    [/code]

    Can any one guide me. How is error occured?

    Thank you
  • bilibytes
    New Member
    • Jun 2008
    • 128

    #2
    You are not doing the query well.

    you should pass a mysql connection to the query so that it knows where to query.

    Code:
    $link = mysql_connect('hostName', 'userName', 'passWord');
    
    //then select the db
    
    mysql_select_db('databaseName', $link);
    
    //then you can make your queries passing the $link
    
    $resultSet = mysql_query('SELECT * FROM Table1', $link);
    
    //now you can check if there are results
    if(mysql_num_rows(0 < $resultSet)){
        echo 'yeah coolm, i have a result'
    }
    else{
         echo 'wtf nothing from db';
    }

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Your mysql_query() doesn't return a valid resource - what is the query?

      Also, you if/else block can be shortened like so:

      Code:
      return ($no !== 1);
      
      // as opposed to this:
      if($no <> 1) {
          return FALSE;
      }
      else {
          return TRUE;
      }

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Billibytes beat me to it.

        Anyway, couple of things: You do not have to pass a link resource to the mysql_* functions - if you do not, mysql will use the last opened connection.

        Also:
        Code:
        if(mysql_num_rows(0 < $resultSet)){
            echo 'yeah coolm, i have a result'
        }
        else{
             echo 'wtf nothing from db';
        }
        will not work, but I think that's just a typing error on your part ;)

        Code:
        if(mysql_num_rows($resultSet) < 0){
            echo 'yeah coolm, i have a result'
        }
        else{
             echo 'wtf nothing from db';
        }

        Comment

        • bilibytes
          New Member
          • Jun 2008
          • 128

          #5
          Originally posted by Markus
          Billibytes beat me to it.

          Anyway, couple of things: You do not have to pass a link resource to the mysql_* functions - if you do not, mysql will use the last opened connection.

          Also:
          Code:
          if(mysql_num_rows(0 < $resultSet)){
              echo 'yeah coolm, i have a result'
          }
          else{
               echo 'wtf nothing from db';
          }
          will not work, but I think that's just a typing error on your part ;)

          Code:
          if(mysql_num_rows($resultSet) < 0){
              echo 'yeah coolm, i have a result'
          }
          else{
               echo 'wtf nothing from db';
          }
          right, i have answered too quickly...

          Comment

          • luckysanj
            New Member
            • May 2009
            • 68

            #6
            ok yet i have not solved my problem. so i send my three php page content.
            1. index.php
            [code=html]
            <form id="form1" name="form1" method="post" action="check.p hp?id=tbl1">
            <table width="200" border="1">
            <tr>
            <td colspan="2">Pes onal Info Table 1 </td>
            </tr>
            <tr>
            <td width="72">Name :</td>
            <td width="112"><in put name="name" type="text" id="name" value="Ashok" /></td>
            </tr>
            <tr>
            <td>Address</td>
            <td><input name="address" type="text" id="address" value="Kathmand u" /></td>
            </tr>
            <tr>
            <td><input name="tbl1" type="submit" id="tbl1" value="Submit" /></td>
            <td><input type="reset" name="Submit2" value="Reset" /></td>
            </tr>
            </table>
            </form>

            [/code]

            2. check.php
            [code=php]
            <?php
            foreach($_POST as $key=>$value)
            {
            //echo "</br>";
            $key.":".$$key= $value;
            //echo "</br>".$key;
            }
            include_once("f unction.php");
            $tblname= $_GET['id'];
            $db=new Functions;
            $db->dbconnection() ;
            if(isset($tbl1) )
            {
            $field=array('n ame','address') ;
            $value=array($n ame,$address);
            $y=$result=$db->InsertData($tb lname,$field,$v alue);
            /*if($y==true)
            {
            echo "Success";
            }
            else
            {
            echo "Failed";
            }*/
            }
            if(isset($tbl2) )
            {
            $field=array('p hone','email');
            $value=array($p hone,$email);
            $result=$db->InsertData($tb lname,$field,$v alue);
            }
            ?>
            [/code]

            3. function.php
            [code=php]
            <?php
            class Database
            {
            function dbconnection()
            {
            $link=mysql_con nect("localhost ","root","" ) or die("Failed Connecting to Database");
            mysql_select_db ("query",$li nk) or die("Failed Connecting To Database");
            }
            }
            class Functions extends Database
            {
            function InsertData($Tbl Name,$Fields,$V alues)
            {
            $query="INSERT INTO `{$TblName}` (`".(is_array($ Fields)?implode ("`,`",$Fields) :$Fields)."`)
            VALUES (".(is_array($V alues)?"'".impl ode("','",$Valu es)."'":$Values ).")";
            $sql_result=mys ql_query($query ) or die("Error in Checking User".mysql_err or());
            echo $no=mysql_num_r ows($sql_result );
            if($no<>1)
            {
            return(false);
            }
            else
            {
            return (true);
            }
            }
            }
            ?>

            [/code]

            But Still Error is same
            [code=html]
            Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource in D:\xampp\htdocs \my\test\query\ function.php on line 17
            [/code]

            Comment

            • Canabeez
              New Member
              • Jul 2009
              • 126

              #7
              Try this as function.php
              [code=php]
              class Database
              {
              private $link;
              function dbconnection()
              {
              $this->link=mysql_con nect("localhost ","root","" ) or die("Failed Connecting to Database");
              mysql_select_db ("query",$th is->link) or die("Failed Connecting To Database");
              }
              }
              class Functions extends Database
              {
              function InsertData($Tbl Name,$Fields,$V alues)
              {
              $query="INSERT INTO `{$TblName}` (`".(is_array($ Fields)?implode ("`,`",$Fields) :$Fields)."`)
              VALUES (".(is_array($V alues)?"'".impl ode("','",$Valu es)."'":$Values ).")";
              $sql_result=mys ql_query($query , $this->link) or die("Error in Checking User".mysql_err or());

              /* This line is to debug your query */ echo "<!-- QUERY: {$query} -->";

              $no = mysql_num_rows( $sql_result);
              echo ($no != 1);
              }
              }
              [/code]

              Comment

              • bilibytes
                New Member
                • Jun 2008
                • 128

                #8
                I suggest that you go step by step.

                first check that you get the data from the brower, and try to echo it.

                then try to connect to the db and make a query with data you write in your file, not from $_POST

                and try to output it.

                and so on, you have to narrow down the search until you know where the problem comes from.

                once you know where your code fails, come back and we will guide you.

                Comment

                • luckysanj
                  New Member
                  • May 2009
                  • 68

                  #9
                  This error is occur Canabeez sir,

                  [code=html]
                  Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\xampp\htdocs \my\test\query\ function.php on line 18
                  Error in Checking User
                  [/code]

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    echo() the $query variable, and post the output here. Your SQL is wrong, most likely.

                    Comment

                    • luckysanj
                      New Member
                      • May 2009
                      • 68

                      #11
                      [code=html]
                      INSERT INTO `tbl1` (`name`,`addres s`) VALUES ('Ashok','Kathm andu')
                      [/code]

                      The Query vairable carried right value. but the out put error is same.
                      [code=html]
                      Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\xampp\htdocs \my\test\query\ function.php on line 18
                      Error in Checking User
                      [/code]

                      Comment

                      • bilibytes
                        New Member
                        • Jun 2008
                        • 128

                        #12
                        put the $link as i told you... or a password

                        Comment

                        • luckysanj
                          New Member
                          • May 2009
                          • 68

                          #13
                          I have change little bit on Private into Public then it works but the new problem is aries:
                          [code=php]
                          <?
                          class Database
                          {
                          public $link;
                          function dbconnection()
                          {
                          $this->link=mysql_con nect("localhost ","root","" ) or die("Failed Connecting to Database");
                          mysql_select_db ("query",$th is->link) or die("Failed Connecting To Database");
                          }
                          }
                          class Functions extends Database
                          {
                          function InsertData($Tbl Name,$Fields,$V alues)
                          {
                          $query="INSERT INTO `{$TblName}` (`".(is_array($ Fields)?implode ("`,`",$Fields) :$Fields)."`)
                          VALUES (".(is_array($V alues)?"'".impl ode("','",$Valu es)."'":$Values ).")";
                          $sql_result=mys ql_query($query , $this->link) or die("Error in Checking User".mysql_err or());
                          /* This line is to debug your query */ //echo "QUERY:{$query} ";
                          $no = mysql_num_rows( $sql_result);
                          echo ($no != 1);
                          }
                          }
                          ?>

                          [/code]

                          Error is :
                          [code=html]
                          Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource in D:\xampp\htdocs \my\test\query\ function.php on line 19

                          [/code]

                          Comment

                          • luckysanj
                            New Member
                            • May 2009
                            • 68

                            #14
                            Now i need to return True or False value to
                            [code=php]$y=$result=$db->InsertData($tb lname,$field,$v alue);[/code]
                            which is call from check.php page.

                            Comment

                            • luckysanj
                              New Member
                              • May 2009
                              • 68

                              #15
                              Ok thank you i have solved this way..
                              [code=php]
                              <?
                              class Database
                              {
                              public $link;
                              function dbconnection()
                              {
                              $this->link=mysql_con nect("localhost ","root","" ) or die("Failed Connecting to Database");
                              mysql_select_db ("query",$th is->link) or die("Failed Connecting To Database");
                              }
                              }
                              class Functions extends Database
                              {
                              function InsertData($Tbl Name,$Fields,$V alues)
                              {
                              $query="INSERT INTO `{$TblName}` (`".(is_array($ Fields)?implode ("`,`",$Fields) :$Fields)."`)
                              VALUES (".(is_array($V alues)?"'".impl ode("','",$Valu es)."'":$Values ).")";
                              $d=$sql_result= mysql_query($qu ery, $this->link) or die("Error in Checking User".mysql_err or());
                              if($d==1)
                              {
                              return true;
                              }
                              else
                              {
                              return false;
                              }
                              /* This line is to debug your query */ //echo "QUERY:{$query} ";
                              //$no = mysql_num_rows( $sql_result);
                              //echo ($no != 1);
                              }
                              }
                              ?>
                              [/code]

                              Comment

                              Working...