$query1 = "SELECT tblpatient_pass.RelationMR_no, tblpatient_pass.username, tblpatient_pass.password, tblpatient_pass.email_address, tblpatient_info.lastname, tblpatient_info.firstname, tblpatient_info.mname
FROM tblpatient_pass INNER JOIN tblpatient_info ON tblpatient_pass.RelationMR_no=tblpatient_info.MR_no
	";
		$numrows = mysql_num_rows($query1);
		if ($numrows!=0)
		{
			while ($row = mysql_fetch_array($query1))
			{
		$username = $_SESSION['username'];
		$query = mysql_query("SELECT * FROM tblpatient_pass WHERE username =".$username);	
	$result = mysql_query($query) or die(mysql_error());
	while ($patient = mysql_fetch_array($result))
	{
		echo $patient['RelationMR_no'].'<br>';
		echo $patient['username'].'<br>';
		echo $patient['password'].'<br>';
		echo $patient['email_address'].'<br>';
		echo $patient['lastname'].'<br>';
		echo $patient['firstname'].'<br>';
		echo $patient['mname'].'<br>';
		echo $patient['gender'].'<br>';
		echo $patient['age'].'<br>';
		
	}
			}
		}

guys can anybody help me,, my original problem is how I can make a profile page wherein it will display records from two different tables connected by MR_no as primary key from tblpatient_info and RelationMR_no from tblpatient_pass but I don't know how to display these records based on the username of the member. Because I quite not familiar with queries and specially inner join functions, I need to finish these for my project before March 10

Recommended Answers

All 25 Replies

Line 4 will fail, because $query1 is an sql string. Just as your other queries, you need to use mysql_query().

If you have specific questions about your queries, post them, and your table structures, in the mysql forum.

Agreed. Whenever you get a notice on either num_rows of fetch_array that parameter 1 needs to be a resource then it usually means that there was an error in a mysql statement and that the query, in your case $query1 doesn't hold an array for them to use.

$query1 = mysql_query("SELECT tblpatient_pass.RelationMR_no, tblpatient_pass.username, tblpatient_pass.password, tblpatient_pass.email_address, tblpatient_info.lastname, tblpatient_info.firstname, tblpatient_info.mname
FROM tblpatient_pass INNER JOIN tblpatient_info ON tblpatient_pass.RelationMR_no=tblpatient_info.MR_no
");

You other two mysql queries have this. So add it like it is above and it should at least fix that error. I didn't delve too deeply into your join but it will at least fix the error that you're getting. Then you can run the script to see if it is otherwise doing what you need it to.

And it might be a good idea to run an else statement on the num_rows IF statement to echo out a warning such as "There is no matching record in our system" or something similar. When you are running a check to see if there are records that match your query it's good practice to offer a "warning" in case it doesn't match for some reason.

I follow your suggestions guys, I ask it on the MySQL forum now it gives this error:
Warning: mysql_query() expects parameter 1 to be string, resource given in C:\xampp\htdocs\bernardino_ed\profile.php on line 100

this is my codes:

$username = $_SESSION['username'];
			$query = mysql_query("SELECT *
FROM tblpatient_pass p, tblpatient_info i
WHERE p.RelationMR_no = i.MR_no
AND username='$username'");	
			$result = mysql_query($query) or die(mysql_error());
		while ($patient = mysql_fetch_array($result))
		{
			echo $patient['RelationMR_no'].'<br>';
			echo $patient['username'].'<br>';
			echo $patient['password'].'<br>';
			echo $patient['email_address'].'<br>';
			echo $patient['lastname'].'<br>';
			echo $patient['firstname'].'<br>';
			echo $patient['mname'].'<br>';
			echo $patient['gender'].'<br>';
			echo $patient['age'].'<br>';
		
		}

I hope you can help me again on this guys thanks

Remove mysql_query from line 2. You should really read the manual.

$username = $_SESSION['username'];
    $query = "SELECT *                           <--------mysql_query removed...
    FROM tblpatient_pass p, tblpatient_info i  
    WHERE p.RelationMR_no = i.MR_no
    AND username='$username'";
    $result = mysql_query($query) or die(mysql_error());
    while ($patient = mysql_fetch_array($result))
    {
    echo $patient['RelationMR_no'].'<br>';
    echo $patient['username'].'<br>';
    echo $patient['password'].'<br>';
    echo $patient['email_address'].'<br>';
    echo $patient['lastname'].'<br>';
    echo $patient['firstname'].'<br>';
    echo $patient['mname'].'<br>';
    echo $patient['gender'].'<br>';
    echo $patient['age'].'<br>';
     
    }

Guys I'm still trying to modify my codes,, until I came up to this:

$username = $_SESSION['username'];
			$query = "SELECT *
FROM tblpatient_pass p, tblpatient_info i
WHERE p.RelationMR_no = i.MR_no
AND username='$username'";

		$result = mysql_query($query) or die(mysql_error());
		$patient = mysql_num_rows($result);
		if ($patient!=0)
		{
			while ($row = mysql_fetch_assoc($query))
			{
				$dusername = $row['username'];
				$dpassword = $row['password'];
				$dMR_no = $row['RelationMR_no'];
				$demail = $row['email_address'];
			}
			echo $dusername;
			echo $dpassword;
			echo $dMR_no;
			echo $demail;
			
		}
		
		else
			{
			die('No records display.');
		}

but I get this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in C:\xampp\htdocs\bernardino_ed\profile.php on line 105

the line 105 is this:

while ($row = mysql_fetch_assoc($query))

thank you and God bless

$query should be $result.

thanks pritaeas I found it already, my new problem now is why I can't display the value of my foreign key in my web page and also those values coming from the fields in my another table that I joined with my tblpatient_pass. here is my code:

$username = $_SESSION['username'];
			$query = "SELECT *
FROM tblpatient_pass p, tblpatient_info i
WHERE p.RelationMR_no = i.MR_no
AND p.username='$username'";

		$result = mysql_query($query) or die(mysql_error());
		$patient = mysql_num_rows($result);
		if ($patient!=0)
		{
			while ($row = mysql_fetch_assoc($result))
			{
				$dusername = $row['username'];
				$dpassword = $row['password'];
				$dMR_no = $row['MR_no'];
				$demail = $row['email_address'];
				$dlname = $row['lastname'];
				$dfname = $row['firstname'];
				$dgender = $row['gender'];
				$dmname = $row['mname'];
				$dage = $row['age'];
			}
			echo "MR Number:".$dMR_no.'<br>';
			echo "Name:".$dfname." ".$dmname." ".$dlname.'<br>';
			echo "Age:".$dage.'<br>';
			echo "Gender:".$dgender.'<br>';
			echo "Username:".$dusername.'<br>';
			echo "Password:".$dpassword.'<br>';
			echo "Email Address:".$demail.'<br>';

			
		}
		
		else
		{
			die(mysql_error());
		}

You'll have to be more specific. If you want to know what $row contains just use print_r($row).

When I use the print_r($row) it gives all the content of the row that I'm trying to retrieve but when I select a specific row which has an integer as its data type and those fields coming from the other table, it still didn't give any record. I tried these two codes to display them:

print_r($row['username']);
echo $row['username'];

is there a difference in displaying a VARCHAR and an INT coming from the database?

When I use the print_r($row) it successfully displayed all the records from the row that I need to use but When I use

print_r($row['age']);

to display record from a field which has integer as its data type, it seems that it can't retrieve the record as well as when I use it to display records coming from the other table, tlpatient_info
is there a difference on displaying INT records and VARCHAR in PHP?

If the value is null it may not show, otherwise it should. Never had any problems with this.

Yes, thank you again I already solve my problem,, instead of using the name of the field I used its corresponding number, and it works,, my code now is this:

<?php
$username = $_SESSION['username'];
			$query = "SELECT *
FROM tblpatient_pass p, tblpatient_info i
WHERE p.RelationMR_no = i.MR_no
AND p.username='$username'";

		$result = mysql_query($query) or die(mysql_error());
		$patient = mysql_num_rows($result);
		if ($patient!=0)
		{
			while ($row = mysql_fetch_array($result))
			{
				$dMR_no = $row[0];
				$dusername = $row[1];
				$dpassword = $row[2];
				$demail = $row[3];
				$dlname = $row[5];
				$dfname = $row[6];
				$dgender = $row[11];
				$dmname = $row[7];
				$dage = $row[9];
				$dcontact_no = $row[12];
				$dlocation = $row[8];
			//	print_r($row);
			}
		}
		
		else
		{
			die(mysql_error());
		}
?>
			<table border="0px" cellpadding="5px" cellspacing="5px">
			<tr><td class="bodytext" align="left"><b><pont>MR Number:</pont></b></td>
			<td> <?php echo '<waku>'.$dMR_no.'</waku>'; ?> </td></tr>
			<tr><td class="bodytext" align="left"><b><pont>Name:</pont></b></td>
			<td> <?php echo '<waku>'.$dfname." ".$dmname." ".$dlname.'</waku>'; ?> </td></tr>
			<tr><td class="bodytext" align="left"><b><pont>Age:</pont></b></td>
			<td> <?php echo '<waku>'.$dage.'</waku>'; ?> </td></tr>
			<tr><td class="bodytext" align="left"><b><pont>Gender:</pont></b></td>
			<td> <?php echo '<waku>'.$dgender.'</waku>'; ?> </td></tr>
			<tr><td class="bodytext" align="left"><b><pont>Contact number:</pont></b></td>
			<td> <?php echo '<waku>'.$dcontact_no.'</waku>'; ?> </td></tr>
			<tr><td class="bodytext" align="left"><b><pont>Location:</pont></b></td>
			<td> <?php echo '<waku>'.$dlocation.'</waku>'; ?> </td></tr>
			<tr><td class="bodytext" align="left"><b><pont>Email Address:</pont></b></td>
			<td> <?php echo '<waku>'.$demail.'</waku>'; ?> </td></tr>
			</table>

Sir, i am getting error
Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\html5\login.php on line 30
Query Failed

        <?php
        include ('include/dbconfig.php'); 
        if (isset($_POST['formsubmitted'])) {
            // Initialize a session:
        session_start();
            $error = array();

            if (empty($_POST['e-mail'])) {//if the email supplied is empty 
                $error[] = 'You forgot to enter  your Email ';
            } else {

                if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) {

                    $Email = $_POST['e-mail'];
                } else {
                     $error[] = 'Your EMail Address is invalid  ';
                }
            }
            if (empty($_POST['Password'])) {
                $error[] = 'Please Enter Your Password ';
            } else {
                $md5Password = md5($_POST['Password']);
            }
               if (empty($error))
            {  
                $query = "SELECT * FROM users WHERE email='$Email' AND password='$md5Password')";
                $query_check = mysql_query ($query);


                $result_check = mysql_query($link, $query_check);
                if(!$result_check){//If the QUery Failed 
                    echo 'Query Failed ';
                }

                if (@mysqli_num_rows($result_check) == 1)//if Query is successfull 
                { // A match was made.

                    $_SESSION = mysqli_fetch_array($result_check, MYSQLI_ASSOC);//Assign the result of this query to SESSION Global Variable

                    header("Location: page.php");
                }else
                {         
                    $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
                }
            }  else {

        echo '<div class="errormsgbox"> <ol>';
                foreach ($error as $key => $values) {

                    echo '  <li>'.$values.'</li>';

                }
                echo '</ol></div>';
            }

            if(isset($msg_error)){       
                echo '<div class="warning">'.$msg_error.' </div>';
            }
            /// var_dump($error);
            mysql_close($link);

        }


?>

You have posted in a 1 year old solved discussion, and are unlikley to get replies.
I quickly scanned your code and didn't see any glaring errors, but I'm assuming your '$link' connects with 'dbconfig.php'. Check that first.

Lines 27 and 30 are conflicting. Remove 30 and rename the variable in 31.

ok, Thanks for reply.
i commentd line 30 , see below code, even i enter correct email & password, it is saying
"Query Failed". i am testing in local database using WAMP Server

<?php
include ('include/dbconfig.php'); 
if (isset($_POST['formsubmitted'])) {
    // Initialize a session:
session_start();
    $error = array();

    if (empty($_POST['e-mail'])) {//if the email supplied is empty 
        $error[] = 'You forgot to enter  your Email ';
    } else {

        if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) {

            $Email = $_POST['e-mail'];
        } else {
             $error[] = 'Your EMail Address is invalid  ';
        }
    }
    if (empty($_POST['Password'])) {
        $error[] = 'Please Enter Your Password ';
    } else {
        $Password = ($_POST['Password']);
    }
       if (empty($error))
    {  
        $query = "SELECT * FROM users WHERE (email='$Email' AND password='$Password')";
        $query_check = mysql_query ($query);


       // $result_check = mysql_query($link, $query_check);
        if(!$query_check){//If the QUery Failed 
            echo 'Query Failed ';
        }

        if (@mysqli_num_rows($query_check) == 1)//if Query is successfull 
        { // A match was made.

            $_SESSION = mysqli_fetch_array($query_check, MYSQLI_ASSOC);//Assign the result of this query to SESSION Global Variable

            header("Location: page.php");
        }else
        {         
            $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
        }
    }  else {

echo '<div class="errormsgbox"> <ol>';
        foreach ($error as $key => $values) {

            echo '  <li>'.$values.'</li>';

        }
        echo '</ol></div>';
    }

    if(isset($msg_error)){       
        echo '<div class="warning">'.$msg_error.' </div>';
    }
    /// var_dump($error);
    mysql_close($link);

} // End of the main Submit conditional.

and this the code for dbconfig.php

<?php
$dbuser = 'root'; // Here enter your db username
$dbpassword = ''; // Here enter your db password
$dbname = 'database'; // Here enter your db name
$dbhost = 'localhost'; // Here enter your db host ex. localhost

$link = mysql_connect($dbhost,$dbuser,$dbpassword) or die("Couldn't make connection.");
$db = mysql_select_db($dbname, $link) or die("Couldn't select database");

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
?>

?>

Instead of:

echo 'Query Failed ';

do:

echo 'Query Failed: ' . mysql_error();

and post the error message here.

Sir, it is not saying any error, but it is unable to login even after activation

saying "Either Your Account is inactive or Email address /Password is Incorrect "

You're mixing mysql with mysqli which are two different libraries, so it cannot work, change this:

if (@mysqli_num_rows($result_check) == 1)//if Query is successfull 
{ // A match was made.
    $_SESSION = mysqli_fetch_array($result_check, MYSQLI_ASSOC);//Assign the result of this query to SESSION Global Variable
    header("Location: page.php");
}else
{         
    $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
}

To:

if (mysql_num_rows($result_check) == 1)//if Query is successfull 
{ // A match was made.
    $_SESSION = mysql_fetch_array($result_check, MYSQLI_ASSOC);//Assign the result of this query to SESSION Global Variable
    header("Location: page.php");
}else
{         
    $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
}

Or better, convert everything to mysqli. Also when developping remove @ from functions, since this suppress the errors and you cannot see where's the problem.

Didn't even notice that, nice catch. Second, assigning to the $_SESSION array is not a good idea either.

Something like $_SESSION['user'] or something, if you really need it in the session.

Sir, i am getting error
Notice: Undefined variable: email in C:\wamp\www\purchased\index.php on line 31

what may be the problem ? please help

<?php
include "functions.php";

if(check_user()){
    $user_id = $_SESSION['user_id'];
    if($user_id)
        $email = getFieldById('users', $user_id, 'email');
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">

<link rel="stylesheet" type="text/css" href="css/style.css">

<!--[if lte IE 8]>
<script type="text/javascript" src="js/html5.js"></script>
<![endif]-->
</head>
<body>
<div id = "wrap-all">
<header id="top">
    <div class="container_12 clearfix">
        <div id="logo" class="grid_6">
            <!-- replace with your website title or logo -->
            <a id="site-title" href="#">Site logo</a>
        </div>
        <div id="userinfo" class="grid_6">
        <?php if($email): ?>
            Welcome <span><?php echo $email; ?></span>
            <a href="logout.php">logout</a>
        <?php else: ?>
            <a id = "login_signup" href="login.html">login/signup</a>
        <?php endif; ?>
        </div>
    </div>
</header>

<div id = "body-wrap">

<section id="content">
<?php if($email): ?>
<p class = "body_center">logged user: <?php echo $email; ?></p>
<?php else: ?>
<p class = "body_center">Not logged user: use the top-right link to get the login page</p>
<?php endif; ?>
</section>

</div>
<footer id="bottom">
    <section class="container_12 clearfix">
        <div class="aligncenter">
            Copyright &copy; 2012 <a href="#">YourCompany.com</a>
        </div>
    </section>
</footer>
</div>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.