473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

mysql_num_rows error...

luckysanj
Expand|Select|Wrap|Line Numbers
  1. $sql_result=mysql_query($query) or die("Error in Checking User".mysql_error()); 
  2.         echo $no=mysql_num_rows($sql_result);
  3.              if($no<>1)
  4.                 {
  5.                 return(false);
  6.                 }
  7.             else
  8.                 {
  9.                 return (true);
  10.                 }
  11.  
In this code i have get this type of error:
Expand|Select|Wrap|Line Numbers
  1. 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
  2.  
Can any one guide me. How is error occured?

Thank you
Jul 27 '09 #1
14 1960
bilibytes
128 100+
You are not doing the query well.

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

Expand|Select|Wrap|Line Numbers
  1. $link = mysql_connect('hostName', 'userName', 'passWord');
  2.  
  3. //then select the db
  4.  
  5. mysql_select_db('databaseName', $link);
  6.  
  7. //then you can make your queries passing the $link
  8.  
  9. $resultSet = mysql_query('SELECT * FROM Table1', $link);
  10.  
  11. //now you can check if there are results
  12. if(mysql_num_rows(0 < $resultSet)){
  13.     echo 'yeah coolm, i have a result'
  14. }
  15. else{
  16.      echo 'wtf nothing from db';
  17. }
  18.  
Jul 27 '09 #2
Markus
6,050 Expert 4TB
Your mysql_query() doesn't return a valid resource - what is the query?

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

Expand|Select|Wrap|Line Numbers
  1. return ($no !== 1);
  2.  
  3. // as opposed to this:
  4. if($no <> 1) {
  5.     return FALSE;
  6. }
  7. else {
  8.     return TRUE;
  9. }
  10.  
Jul 27 '09 #3
Markus
6,050 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. if(mysql_num_rows(0 < $resultSet)){
  2.     echo 'yeah coolm, i have a result'
  3. }
  4. else{
  5.      echo 'wtf nothing from db';
  6. }
  7.  
will not work, but I think that's just a typing error on your part ;)

Expand|Select|Wrap|Line Numbers
  1. if(mysql_num_rows($resultSet) < 0){
  2.     echo 'yeah coolm, i have a result'
  3. }
  4. else{
  5.      echo 'wtf nothing from db';
  6. }
  7.  
Jul 27 '09 #4
bilibytes
128 100+
@Markus
right, i have answered too quickly...
Jul 27 '09 #5
ok yet i have not solved my problem. so i send my three php page content.
1. index.php
Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="form1" method="post" action="check.php?id=tbl1">
  2.   <table width="200" border="1">
  3.     <tr>
  4.       <td colspan="2">Pesonal Info Table 1 </td>
  5.     </tr>
  6.     <tr>
  7.       <td width="72">Name:</td>
  8.       <td width="112"><input name="name" type="text" id="name" value="Ashok" /></td>
  9.     </tr>
  10.     <tr>
  11.       <td>Address</td>
  12.       <td><input name="address" type="text" id="address" value="Kathmandu" /></td>
  13.     </tr>
  14.     <tr>
  15.       <td><input name="tbl1" type="submit" id="tbl1" value="Submit" /></td>
  16.       <td><input type="reset" name="Submit2" value="Reset" /></td>
  17.     </tr>
  18.   </table>
  19. </form>
  20.  
  21.  
2. check.php
Expand|Select|Wrap|Line Numbers
  1.     <?php
  2.     foreach($_POST as $key=>$value)
  3.         { 
  4.         //echo "</br>";
  5.         $key.":".$$key=$value;
  6.         //echo "</br>".$key;
  7.         }
  8.         include_once("function.php");
  9.         $tblname= $_GET['id'];
  10.         $db=new Functions;
  11.         $db->dbconnection();
  12.     if(isset($tbl1))
  13.         {
  14.         $field=array('name','address');    
  15.         $value=array($name,$address);
  16.         $y=$result=$db->InsertData($tblname,$field,$value);
  17.         /*if($y==true)
  18.             {
  19.                 echo "Success";
  20.             }
  21.             else
  22.             {
  23.             echo "Failed";
  24.             }*/
  25.         }
  26.     if(isset($tbl2))
  27.         {
  28.         $field=array('phone','email');    
  29.         $value=array($phone,$email);
  30.         $result=$db->InsertData($tblname,$field,$value);
  31.         }
  32. ?>
  33.  
3. function.php
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. class Database
  3. {
  4. function dbconnection() 
  5.     {
  6.     $link=mysql_connect("localhost","root","") or die("Failed Connecting to Database");
  7.     mysql_select_db("query",$link) or die("Failed Connecting To Database");    
  8.     }
  9. }    
  10. class Functions extends Database
  11. {
  12. function InsertData($TblName,$Fields,$Values)  
  13.         {  
  14.         $query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`) 
  15.                 VALUES (".(is_array($Values)?"'".implode("','",$Values)."'":$Values).")"; 
  16.         $sql_result=mysql_query($query) or die("Error in Checking User".mysql_error()); 
  17.         echo $no=mysql_num_rows($sql_result);
  18.              if($no<>1)
  19.                 {
  20.                 return(false);
  21.                 }
  22.             else
  23.                 {
  24.                 return (true);
  25.                 }
  26.         } 
  27. }    
  28. ?>
  29.  
  30.  
But Still Error is same
Expand|Select|Wrap|Line Numbers
  1. 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
  2.  
Jul 27 '09 #6
Canabeez
126 100+
Try this as function.php
Expand|Select|Wrap|Line Numbers
  1. class Database
  2. {
  3.     private $link;
  4.     function dbconnection() 
  5.     {
  6.         $this->link=mysql_connect("localhost","root","") or die("Failed Connecting to Database");
  7.         mysql_select_db("query",$this->link) or die("Failed Connecting To Database");    
  8.     }
  9. }    
  10. class Functions extends Database
  11. {
  12.     function InsertData($TblName,$Fields,$Values)  
  13.     {  
  14.         $query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`) 
  15.         VALUES (".(is_array($Values)?"'".implode("','",$Values)."'":$Values).")"; 
  16.         $sql_result=mysql_query($query, $this->link) or die("Error in Checking User".mysql_error());
  17.  
  18.         /* This line is to debug your query */ echo "<!-- QUERY: {$query} -->";
  19.  
  20.         $no = mysql_num_rows($sql_result);
  21.         echo ($no != 1);
  22.     }
  23. }
  24.  
Jul 27 '09 #7
bilibytes
128 100+
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.
Jul 27 '09 #8
This error is occur Canabeez sir,

Expand|Select|Wrap|Line Numbers
  1. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\xampp\htdocs\my\test\query\function.php on line 18
  2. Error in Checking User
  3.  
Jul 27 '09 #9
Markus
6,050 Expert 4TB
echo() the $query variable, and post the output here. Your SQL is wrong, most likely.
Jul 27 '09 #10
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `tbl1` (`name`,`address`) VALUES ('Ashok','Kathmandu')
  2.  
The Query vairable carried right value. but the out put error is same.
Expand|Select|Wrap|Line Numbers
  1. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\xampp\htdocs\my\test\query\function.php on line 18 
  2. Error in Checking User 
  3.  
Jul 27 '09 #11
bilibytes
128 100+
put the $link as i told you... or a password
Jul 27 '09 #12
I have change little bit on Private into Public then it works but the new problem is aries:
Expand|Select|Wrap|Line Numbers
  1. <?
  2. class Database 
  3.     public $link; 
  4.     function dbconnection()  
  5.     { 
  6.         $this->link=mysql_connect("localhost","root","") or die("Failed Connecting to Database"); 
  7.         mysql_select_db("query",$this->link) or die("Failed Connecting To Database");     
  8.     } 
  9. }     
  10. class Functions extends Database 
  11.     function InsertData($TblName,$Fields,$Values)   
  12.     {   
  13.         $query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`)  
  14.         VALUES (".(is_array($Values)?"'".implode("','",$Values)."'":$Values).")";  
  15.         $sql_result=mysql_query($query, $this->link) or die("Error in Checking User".mysql_error()); 
  16.         /* This line is to debug your query */ //echo "QUERY:{$query}"; 
  17.         $no = mysql_num_rows($sql_result); 
  18.         echo ($no != 1); 
  19.     } 
  20. ?>
  21.  
  22.  
Error is :
Expand|Select|Wrap|Line Numbers
  1. 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
  2.  
  3.  
Jul 27 '09 #13
Now i need to return True or False value to
Expand|Select|Wrap|Line Numbers
  1. $y=$result=$db->InsertData($tblname,$field,$value);
which is call from check.php page.
Jul 27 '09 #14
Ok thank you i have solved this way..
Expand|Select|Wrap|Line Numbers
  1. <?
  2. class Database 
  3.     public $link; 
  4.     function dbconnection()  
  5.     { 
  6.         $this->link=mysql_connect("localhost","root","") or die("Failed Connecting to Database"); 
  7.         mysql_select_db("query",$this->link) or die("Failed Connecting To Database");     
  8.     } 
  9. }     
  10. class Functions extends Database 
  11.     function InsertData($TblName,$Fields,$Values)   
  12.     {   
  13.         $query="INSERT INTO `{$TblName}` (`".(is_array($Fields)?implode("`,`",$Fields):$Fields)."`)  
  14.         VALUES (".(is_array($Values)?"'".implode("','",$Values)."'":$Values).")";  
  15.         $d=$sql_result=mysql_query($query, $this->link) or die("Error in Checking User".mysql_error()); 
  16.         if($d==1)
  17.             {
  18.             return true;
  19.             }
  20.             else
  21.             {
  22.             return false;
  23.             }
  24.         /* This line is to debug your query */     //echo "QUERY:{$query}"; 
  25.         //$no = mysql_num_rows($sql_result); 
  26.         //echo ($no != 1); 
  27.     } 
  28. ?>
  29.  
Jul 27 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Arjan | last post by:
I've got the following error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/wbdfdart/public_html/wbdfforum/verwijder.php on line 13 verwijder.php ...
4
by: David Nikel | last post by:
I am attempting to create a login script. The following snippet of code: $query = "select * from auth where username='$username' and password=password('$password')"; $result =...
4
by: Fish44 | last post by:
Ive got the following code which works great on my localserver, but when i upload it to my service provider i get an error "Warning: mysql_num_rows(): supplied argument is not a valid MySQL...
7
by: Dejan | last post by:
Hi Sorry for my terreble english. On my local (win) comp i have apache+mysql+php 4.05 I'm counting rows using mysql_num_rows function, and everything works fine. When i upload php file on...
2
by: 00webman | last post by:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/scripts/07/view_users.php on line 15 Can anyone spot my error?!? Thanks. <?php # Script 7.6 -...
3
The1corrupted
by: The1corrupted | last post by:
Okay, this has blown my entire perception of reality way out of order. First, a page was working and not five minutes later, I get this error, Warning: mysql_num_rows(): supplied argument is...
1
by: d3vkit | last post by:
Alright I can not figure this one out, so I'm guessing it's some simple typo and some extra eyes will help me spot it. Here's the query (already connected to DB everything works in that respect):...
3
by: Sandman | last post by:
Hi, So I read the manual where it says to use mysql_affected_rows() for everything except SELECT and SHOW, and use mysql_num_rows() for those two, which actually return a result. However, I...
4
by: seigaku | last post by:
Hi everyone, I've recently begun learning PHP and it looks like I took on too much so soon. I'm attempting to fix a php script that detects whether or not a person is Eligible to join a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.