tobimichigan Posted August 9, 2009 Share Posted August 9, 2009 Code gurus, Hi again, Please I need a pointer to correct this specific error: <?php $pfno=$_GET["pfno"]; $query=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error()); $result=mysql_query($query); $num=mysql_numrows($result); echo "<p> PFNO : ".$_SESSION["pfno"]. "<p>Logged in: " .date("m/d/Y", $_SESSION["valid_time"]); $i=0; while ($i < $num) { $pfno = mysql_fetch_assoc($result); $f1=mysql_result($result,"lname"); $f2=mysql_result($result,"fname"); $f3=mysql_result($result,"oname"); $f4=mysql_result($result,"department"); $i++; } echo("<p> Welcome".$f1); echo ("<p>Department:".$f3); ?> I want to echo out a record specific to the current user but this error: mysql_numrows(): supplied argument is not a valid MySQL result resource on line 25. Where line 25= "$num=mysql_numrows($result);" Please could you guys give a possible insight as to what could be wrong? Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/ Share on other sites More sharing options...
Daniel0 Posted August 9, 2009 Share Posted August 9, 2009 Take an extra look at this: $query=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error()); $result=mysql_query($query); And now tell me what is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894054 Share on other sites More sharing options...
bundyxc Posted August 9, 2009 Share Posted August 9, 2009 <?php $pfno=$_GET["pfno"]; $query="Select * From user_table where pfno='$pfno'"; //Declare the query to be run. $result=mysql_query($query); //Run the query. $num=mysql_num_rows($result); //Fixed typo, function is mysql_num_rows() echo "<p> PFNO : ".$_SESSION["pfno"]. "<p>Logged in: " .date("m/d/Y", $_SESSION["valid_time"]); $i=0; while ($i < $num) { $pfno = mysql_fetch_assoc($result); $f1=mysql_result($result,"lname"); $f2=mysql_result($result,"fname"); $f3=mysql_result($result,"oname"); $f4=mysql_result($result,"department"); $i++; } echo("<p> Welcome".$f1); echo ("<p>Department:".$f3); ?> Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894055 Share on other sites More sharing options...
tobimichigan Posted August 9, 2009 Author Share Posted August 9, 2009 Take an extra look at this: $query=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error()); $result=mysql_query($query); And now tell me what is wrong. Well Dan, I removed the "or die (mysql_error()); which seems to reveal a an error in the sql function. It says: Warning: Wrong parameter count for mysql_numrows() in on line 25 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 'Resource id #5' at line 1 Here's the corrected code: <?php $pfno=$_GET["pfno"]; $query=mysql_query("Select * From user_table where pfno='$pfno'"); $result=mysql_query($query); $num=mysql_num_rows('$result',$link) or die(mysql_error());; echo "<p> PFNO : ".$_SESSION["pfno"]. "<p>Logged in: " .date("m/d/Y", $_SESSION["valid_time"]); $i=0; while ($i < $num) { $pfno = mysql_fetch_assoc($result); $f1=mysql_result($result,"lname"); $f2=mysql_result($result,"fname"); $f3=mysql_result($result,"oname"); $f4=mysql_result($result,"department"); $i++; } echo("<p> Welcome".$f1); echo ("<p>Department:".$f3); ?> So then what could be the matter? <?php $pfno=$_GET["pfno"]; $query="Select * From user_table where pfno='$pfno'"; //Declare the query to be run. $result=mysql_query($query); //Run the query. $num=mysql_num_rows($result); //Fixed typo, function is mysql_num_rows() echo "<p> PFNO : ".$_SESSION["pfno"]. "<p>Logged in: " .date("m/d/Y", $_SESSION["valid_time"]); $i=0; while ($i < $num) { $pfno = mysql_fetch_assoc($result); $f1=mysql_result($result,"lname"); $f2=mysql_result($result,"fname"); $f3=mysql_result($result,"oname"); $f4=mysql_result($result,"department"); $i++; } echo("<p> Welcome".$f1); echo ("<p>Department:".$f3); ?> Hey bundy, thanks, I implemented the your correction but just as I told Dan, its giving "Warning: Wrong parameter count for mysql_numrows() in on line 25 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 'Resource id #5' at line 1" What could be wrong? Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894077 Share on other sites More sharing options...
tobimichigan Posted August 9, 2009 Author Share Posted August 9, 2009 Oh Dan, I corrected your pointer too, I almost forgot: here's the corrected: <?php $pfno=$_GET["pfno"]; $query="Select * From user_table where pfno='$pfno'"; $result=mysql_query($query); $num=mysql_num_rows('$result',$link) or die(mysql_error()); echo "<p> PFNO : ".$_SESSION["pfno"]. "<p>Logged in: " .date("m/d/Y", $_SESSION["valid_time"]); $i=0; while ($i < $num) { $pfno = mysql_fetch_assoc($result); $f1=mysql_result($result,"lname"); $f2=mysql_result($result,"fname"); $f3=mysql_result($result,"oname"); $f4=mysql_result($result,"department"); $i++; } echo("<p> Welcome".$f1); echo ("<p>Department:".$f3); ?> You were right Take an extra look at this: $query=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error()); $result=mysql_query($query); And now tell me what is wrong. Corrected as $query="Select * From user_table where pfno='$pfno'"; $result=mysql_query($query); But Dan, its now giving: Warning: Wrong parameter count for mysql_num_rows() in on line 25, what's wrong now? Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894080 Share on other sites More sharing options...
Daniel0 Posted August 9, 2009 Share Posted August 9, 2009 mysql_num_rows doesn't take the $link parameter. Only a resource as first parameter. Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894082 Share on other sites More sharing options...
tobimichigan Posted August 9, 2009 Author Share Posted August 9, 2009 mysql_num_rows doesn't take the $link parameter. Only a resource as first parameter. What does this mean Dan? and how can I implement the correction? Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894296 Share on other sites More sharing options...
PFMaBiSmAd Posted August 9, 2009 Share Posted August 9, 2009 The light-blue text in his post is a link to the php.net documentation that shows how it should be used. Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894298 Share on other sites More sharing options...
tjhilder Posted August 9, 2009 Share Posted August 9, 2009 if you're looking to get a user's data based on their profile number, you could use this code: $pfno = $_GET['pfno']; $query = "SELECT lname, fname, oname, department, pfno FROM user_table WHERE pfno='$pfno'"; if($result = mysql_array($query) { echo "<p> PFNO : ".$_SESSION["pfno"]. "<p>Logged in: " .date("m/d/Y", $_SESSION["valid_time"]); while(list($lname, $fname, $oname, $department, $pfno) = mysql_fetch_array($result)) { echo $lname . "<br />"; echo $fname . "<br />"; echo $oname . "<br />"; echo $department . "<br />"; } } else { echo "Error: " . mysql_error() . ". Query: " . $query; } Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894317 Share on other sites More sharing options...
bundyxc Posted August 9, 2009 Share Posted August 9, 2009 <?php //I spent a bit of time with your code, making it clean, as well as fixing a few things. //I commented on the things that I fixed, but didn't comment on cleanliness changes. $pfno = $_GET["pfno"]; $query = "SELECT * FROM `user_table` WHERE `pfno` = '" . $pfno . "'"; $result = mysql_query($query); $num = mysql_num_rows($result) OR die(mysql_error()); //mysql_num_rows() does not allow/need $link parameter echo "<p> PFNO : " . $_SESSION["pfno"] . "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]); $i = 0; while ($i < $num) { $pfno = mysql_fetch_assoc($result); $f1=mysql_result($pfno, $i, "lname"); //Incorrect parameters for mysql_result() in $f1, $f2, $f3, $f4. $f2=mysql_result($pfno, $i, "fname"); //Correct parameters: mysql_result (resource $result, int $row, $field) $f3=mysql_result($pfno, $i, "oname"); //I fixed your parameters, and used $i for row number. $f4=mysql_result($pfno, $i, "department"); $i++; } echo("<p> Welcome" . $f1); echo ("<p>Department:" . $f3); ?> Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894358 Share on other sites More sharing options...
tobimichigan Posted August 10, 2009 Author Share Posted August 10, 2009 U guz r all great coders, I wonder what the world of coding would be without you all..Thanks so much it now works Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894429 Share on other sites More sharing options...
bundyxc Posted August 10, 2009 Share Posted August 10, 2009 Well, thanks. I don't know about these other guys, but quite honestly.. I see myself as a PHP noob. Honestly, just keep coding, and rtfm as much as you can. It'll all make sense once you get the hang of it. Also, just a tip: code clean. The nicer looking it is, the easier it is for you to maintain, and for us to help with. You should be able to hand it over to a programmer, and they should know what it does without you saying a word. In addition, comment the hell out of your code. I guarantee that if you don't, you will mess up, forget things, etc. Each part of your application should have a few comments explaining it. Some people get lazy when it comes to complex things... don't. Complex parts of the program are the hardest to figure out, so commenting them up is the most valuable. /rantfornoobfromnoob Quote Link to comment https://forums.phpfreaks.com/topic/169457-solved-mysql_numrows-supplied-argument-is-not-a-valid-mysql/#findComment-894618 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.