Jump to content

[SOLVED] Warning: mysql_fetch_assoc(): supplied argument is not a valid arguement


tobimichigan

Recommended Posts

Hi code gurus,

 

In a code, I am trying to retrieve information specific to a user, I am using the inbuilt array called

<? phpmysql_fetch_assoc?> 

 

but whenever I login, there's this message that pops up thus:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid argument

 

Here's my table:

 

CREATE TABLE `user_table` (

  `id` bigint(20) NOT NULL auto_increment,

  `pfno` varchar(255) NOT NULL,

  `ledgerno` varchar(255) NOT NULL,

  `fname` text NOT NULL,

  `oname` text NOT NULL,

  `lname` text NOT NULL,

  `soorigin` text NOT NULL,

  `lga` text NOT NULL,

  `Nationalty` text NOT NULL,

  `email` text NOT NULL,

  `residentialadd` text NOT NULL,

  `department` text NOT NULL,

  `amountd` text NOT NULL,

  `sex` text NOT NULL,

  `regdate` date NOT NULL,

  `session` int(255) NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='cUser - User Table' AUTO_INCREMENT=2 ;

 

and the code sniplet:

<?php
session_start();

if (!$_SESSION["pfno"])
{
//user not logged in, redirect to login page
header("Location:Login.php");
}
include("cn.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> MEMBERS AREA</title>
</head>

<body>
<a href="Members_Area.php">Members Area</a> | <a href="Edit_Profile.php">Edit Profile</a> | <a href="Reports.php">Reports</a> | <a href="Logout.php">Logout</a>
<?php
$pfno=$_GET["pfno"];
$user=mysql_query("Select * From user_table where pfno='$pfno'");
//$result=mysql_query($query,$link);
$user = mysql_fetch_assoc($pfno);
//Display Member Information
echo ("<p>Welcome PFNO: ".$_SESSION["pfno"]);
echo ("<p>Logged in:  " .date("m/d/Y", $_SESSION["valid_time"]));
?>
</body>
</html>

 

The error it gives is:

 

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Members.Arena\actions\Members_Area.php on line 24

 

Where line 24 is

<?php $user = mysql_fetch_assoc($pfno);?>

 

Please Nadsheem and other sharp pointed code gurus please kindly help.

 

Link to comment
Share on other sites

well try seeing what the error actually is...

 

change

$pfno=$_GET["pfno"];
$user=mysql_query("Select * From user_table where pfno='$pfno'");

 

to

 

$pfno=$_GET["pfno"];
$user=mysql_query("Select * From user_table where pfno='$pfno'") or die(mysql_error());

 

 

Tell me what it outputs

Link to comment
Share on other sites

Its still giving the error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\Members.Arena\actions\Members_Area.php on line 24..

Her's my modified code:

<?php
session_start();

if (!$_SESSION["pfno"])
{
//user not logged in, redirect to login page
header("Location:Login.php");
}
include("cn.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> MEMBERS AREA</title>
</head>

<body>
<a href="Members_Area.php">Members Area</a> | <a href="Edit_Profile.php">Edit Profile</a> | <a href="Reports.php">Reports</a> | <a href="Logout.php">Logout</a>
<?php
$pfno=$_GET["pfno"];
$user=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error());
//$result=mysql_query($query,$link);
$user = mysql_fetch_assoc($pfno);
//Display Member Information
echo ("<p>Welcome PFNO: ".$_SESSION["pfno"]);
echo ("<p>Logged in:  " .date("m/d/Y", $_SESSION["valid_time"]));
?>
</body>
</html>

 

Link to comment
Share on other sites

$user=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error());
$user = mysql_fetch_assoc($pfno);

 

the SQL is called $user not pfno.

 

change that to

 

$user = mysql_fetch_assoc($user );

 

$pfno is your $_GET, $user is your sql resource

Link to comment
Share on other sites

One more thing phpsensei, check the code now:

 

<?php
session_start();

if (!$_SESSION["pfno"])
{
//user not logged in, redirect to login page
header("Location:Login.php");
}
include("cn.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LASUSTAFFCAMS MEMBERS</title>
</head>

<body>
<a href="Members_Area.php">Members Area</a> | <a href="Edit_Profile.php">Edit Profile</a> | <a href="Reports.php">Reports</a> | <a href="Logout.php">Logout</a>
<?php
$pfno=$_GET["pfno"];
$result=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error());
//$result=mysql_query($query,$link);
$user = mysql_fetch_assoc($result);
//Display Member Information
echo ("<p>Welcome PFNO: ".$user["pfno"]);
echo ("<p>Logged in:  " .date("m/d/Y", $_SESSION["valid_time"]));
?>
</body>
</html>

 

As the 1st restricted page, I'd want to echo pfno specific to the user logged in. This I try to do by this line of code

 

<?php
echo ("<p>Welcome PFNO: ".$user["pfno"]);
?>

 

But on the line Welcome PFNO:

it returns nothing. Whereas its supposed to return the exact value (varchar) stored in the table.

So how do I get it to do this?

Link to comment
Share on other sites

Now it returns

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Members.Arena\actions\Members_Area.php on line 25

 

Here's my code as u corrected:

 

<?php
session_start();

if (!$_SESSION["pfno"])
{
//user not logged in, redirect to login page
header("Location:Login.php");
}
include("cn.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MEMBERS</title>
</head>

<body>
<a href="Members_Area.php">Members Area</a> | <a href="Edit_Profile.php">Edit Profile</a> | <a href="Reports.php">Reports</a> | <a href="Logout.php">Logout</a>
<?php
$pfno=$_GET["pfno"];
$result=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error());
//$result=mysql_query($query,$link);
$user = mysql_fetch_assoc($result);
echo mysql_num_rows($user);
//Display Member Information
echo ("<p>Welcome PFNO: ".$user["pfno"]);
echo ("<p>Logged in:  " .date("m/d/Y", $_SESSION["valid_time"]));
?>
</body>
</html>

 

Where line 25=echo mysql_num_rows($user);

 

phpsensei, what do I do next?

Link to comment
Share on other sites

Members Area | Edit Profile | Reports | Logout 0

 

Logged in: 08/03/2009

 

<?php
$pfno=$_GET["pfno"];
$result=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error());
//$result=mysql_query($query,$link;
$user = mysql_fetch_assoc($result);
echo mysql_num_rows($result);
//Display Member Information
echo ($user["pfno"]);
echo ("<p>Logged in:  " .date("m/d/Y", $_SESSION["valid_time"]));
?>

 

Numrows seems to return 0, with this code above after logout. But its still not displaying the real value in the table.

Link to comment
Share on other sites

$pfno is not passing the correct value. Returning 0 means there are no USERS by that pfno...

 

 

print $pfno

I've implemented your correction (see code)

Here's the code:

 

<a href="Members_Area.php">Members Area</a> | <a href="Edit_Profile.php">Edit Profile</a> | <a href="Reports.php">Reports</a> | <a href="Logout.php">Logout</a>
<?php
$pfno=$_GET["pfno"];
$result=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error());
//$result=mysql_query($query,$link;
$user = mysql_fetch_assoc($result);
echo mysql_num_rows($result);
//Display Member Information
print $pfno;
echo ("<p>Logged in:  " .date("m/d/Y", $_SESSION["valid_time"]));
?>

 

The value in the pfno is a varchar(255). Its apprearing in my table, (maybe u can run the sql table at the beginning of this post. I still can't figure out why its not displaying the select statement.

Link to comment
Share on other sites

print $pfno;

 

thats not a VARCHAR thats a HTTP VAR, its suposed to print something out... Its suposed to be in your URL

 

 

ex

 

http://www.domain.com/login.php?pfno=USER

 

Sensi, pls could u elaborate more clearly on this?

 

 

when you did print $pfno.. what did the browser output? if NOTHING then thats why you script is not outputting anything.

 

when you use the $_GET method you are getting the variable/value of PFNO from the URL

 

lets say your page is user.php

 

then user.php must be user.php?pfno=some value

Link to comment
Share on other sites

So Sensi,pls what do I have to do to get it in the url of Members_Area.php?

 

Ionno lol. Its your schema, its your code, what should be in the url of members_area..

 

what is pfno btw? And which page to they click to get to members_area?

 

 

Here's how it goes, members_area is accessed from the Login page

 

Login Page code

<?php

session_start();
include("cn.php");
$msg = "";
//require_once('actions/'.$action.'.php');
if (isset($_POST['Submit']))
{

$username = $_POST['pfno'];
$password = $_POST['ledgerno'];

$result = mysql_query("Select * From user_table where pfno='$pfno'",$link);

//if(mysql_num_rows($result)>0)
{
	//$row = mysql_fetch_array($result, MYSQL_BOTH);
	if($ledgerno == $row["ledgerno"])
	{

		$_SESSION['adminok'] = "ok";
		$_SESSION['pfno'] = "pfno";
		$_SESSION['ledgerno'] = "ledgerno";
		$_SESSION['valid_time']=time();
		header("Location: Members_Area.php");

	}
	else
	{
		$msg = "ledgerno incorrect";
	}
}
//else
{
	$msg = "pfno incorrect";
    }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>

<body>
<div align="center"><img src="../../images/LasustaffCoop2.jpg" alt="" name="LasuStaffCams_Logo" width="700" height="150" id="LasuStaffCams_Logo" /></div>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Members Logon </strong></td>
</tr>
<tr>
<td width="78">PF NO. </td>
<td width="6">:</td>
<td width="294"><input name="pfno" type="text" id="pfno"></td>
</tr>
<tr>
<td>LEDGER NO. </td>
<td>:</td>
<td><input name="ledgerno" type="password" id="ledgerno"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
  <p align='center'> </p>
</body>
</html>

 

After this authentication, members are directed to members_area.php. Where their $pfno is supposed to appear like Welcome PFNO: $pfno

 

Where $pfno is the value stored in the table and is also the means of authentication like username. So then Sensi, how do I get the username or $pfno to display on the members_area with the url?

Link to comment
Share on other sites

there you see, its your login page that is the problem lol

 

try

<?php
session_start();
include("cn.php");
$msg = "";
//require_once('actions/'.$action.'.php');
if (isset($_POST['Submit']))
{
   
   $username = $_POST['pfno'];
   $password = $_POST['ledgerno'];
   
   $result = mysql_query("Select * From user_table where pfno='$pfno'",$link);
   
   //if(mysql_num_rows($result)>0)
   {
      //$row = mysql_fetch_array($result, MYSQL_BOTH);
      if($ledgerno == $row["ledgerno"])
      {
         
         $_SESSION['adminok'] = "ok";
         $_SESSION['pfno'] = "pfno";
         $_SESSION['ledgerno'] = "ledgerno";
         $_SESSION['valid_time']=time();
         header("Location: Members_Area.php?pfno=".$_SESSION['pfno']);

      }
      else
      {
         $msg = "ledgerno incorrect";
      }
   }
   //else
   {
      $msg = "pfno incorrect";
    }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>

<body>
<div align="center"><img src="../../images/LasustaffCoop2.jpg" alt="" name="LasuStaffCams_Logo" width="700" height="150" id="LasuStaffCams_Logo" /></div>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Members Logon </strong></td>
</tr>
<tr>
<td width="78">PF NO. </td>
<td width="6">:</td>
<td width="294"><input name="pfno" type="text" id="pfno"></td>
</tr>
<tr>
<td>LEDGER NO. </td>
<td>:</td>
<td><input name="ledgerno" type="password" id="ledgerno"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>

Link to comment
Share on other sites

print $pfno ... just check that its not blank

 

I just did that sensi and its returning

 

0pfno

 

Here are the codes:

 

Login:

<?php
session_start();
include("cn.php");
$msg = "";
//require_once('actions/'.$action.'.php');
if (isset($_POST['Submit']))
{
   
   $username = $_POST['pfno'];
   $password = $_POST['ledgerno'];
   
   $result = mysql_query("Select * From user_table where pfno='$pfno'",$link);
   
   //if(mysql_num_rows($result)>0)
   {
      //$row = mysql_fetch_array($result, MYSQL_BOTH);
      if($ledgerno == $row["ledgerno"])
      {
         
         $_SESSION['adminok'] = "ok";
         $_SESSION['pfno'] = "pfno";
         $_SESSION['ledgerno'] = "ledgerno";
         $_SESSION['valid_time']=time();
         header("Location: Members_Area.php?pfno=".$_SESSION['pfno']);

      }
      else
      {
         $msg = "ledgerno incorrect";
      }
   }
   //else
   {
      $msg = "pfno incorrect";
    }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>

<body>
<div align="center"><img src="../../images/LasustaffCoop2.jpg" alt="" name="LasuStaffCams_Logo" width="700" height="150" id="LasuStaffCams_Logo" /></div>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Members Logon </strong></td>
</tr>
<tr>
<td width="78">PF NO. </td>
<td width="6">:</td>
<td width="294"><input name="pfno" type="text" id="pfno"></td>
</tr>
<tr>
<td>LEDGER NO. </td>
<td>:</td>
<td><input name="ledgerno" type="password" id="ledgerno"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>

 

members_area

<?php
session_start();

if (!$_SESSION["pfno"])
{
//user not logged in, redirect to login page
header("Location:Login.php");
}
include("cn.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LASUSTAFFCAMS MEMBERS</title>
</head>

<body>
<a href="Members_Area.php">Members Area</a> | <a href="Edit_Profile.php">Edit Profile</a> | <a href="Reports.php">Reports</a> | <a href="Logout.php">Logout</a>
<?php
$pfno=$_GET["pfno"];
$result=mysql_query("Select * From user_table where pfno='$pfno'") or die (mysql_error());
//$result=mysql_query($query,$link;
$user = mysql_fetch_assoc($result);
echo mysql_num_rows($result);
//Display Member Information
print $pfno
//echo ("<p>Welcome user" .$user["pfno"]);
//echo ("<p>Logged in:  " .date("m/d/Y", $_SESSION["valid_time"]));
?>
</body>
</html>

 

Any other pointers?

Link to comment
Share on other sites

alright now we go back to

 

$username = $_POST['pfno'];

 

where is the form for this? The form that submits to the login page

 

Heres the form:

 

<form name="form1" method="post" action="">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Members Logon </strong></td>
</tr>
<tr>
<td width="78">PF NO. </td>
<td width="6">:</td>
<td width="294"><input name="pfno" type="text" id="pfno"></td>
</tr>
<tr>
<td>LEDGER NO. </td>
<td>:</td>
<td><input name="ledgerno" type="password" id="ledgerno"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>

So what's next  sensi?

Link to comment
Share on other sites

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.