Jump to content

[SOLVED] mysql_numrows(): supplied argument is not a valid MySQL


Recommended Posts

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?

<?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);

?>

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?

 

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?

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;
}

 

 

<?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);
?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.