1

I want to display only one account from the two relational table. For example if I inquire my account number and enter it, only the details of my account will be shown not every members. So in my output the whole members and its details inside the database display. How can I manage to display only one account from two relational tables? I know there is something wrong in my sql.

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT member.*, account.*
FROM member, account
WHERE member.mem_id = account.mem_id';

mysql_select_db('databasename');
$retval = mysql_query( $sql, $conn ); 
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Account Number:{$row['Account_Number']}  <br> ".
 "First Name: {$row['fname']} <br> ".
 "Last Name: {$row['lname']} <br> ".
 "Address: {$row['address']} <br> ".
 "Contact: {$row['contact']} <br> ".
 "Share Capital: {$row['Share_Capital']} <br> ".
 "Regular Savings: {$row['Regular_Savings']} <br> ".
 "Power Savings: {$row['Power_Savings']} <br> ".
 "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

Now this is the output.

Account Number: 
First Name:
Last Name:
Address: 
Contact: 
Share Capital: 
Regular Savings:
Power Savings:
4

3 回答 3

1

如果您没有选择任何人的答案是正确的,我怀疑您手头可能有数据问题。但是假设您的记录确实是 1 对 1,那么肯定会这样做:

请注意,我在顶部添加了 $find_id 作为参数

<?php


    $dbhost     = 'localhost';
    $dbuser     = 'root';
    $dbpass     = 'password';
    $find_id    = 12;

    try
    {

        $conn = mysql_connect($dbhost, $dbuser, $dbpass);   
        if( !$conn )
            throw new Exception( "Couldn't establish a connection" );

        if( !mysql_select_db('databasename', $conn )  )
            throw new Exception( "Database couldn't be selected" );

        $sql = 'SELECT M.*, A.* FROM member M LEFT JOIN account A ON M.mem_id = A.mem_id
                WHERE M.mem_id = $find_id';

        if( !($retval = mysql_query( $sql, $conn )) )
            throw new Exception( "No matching result, or query was borked; " . mysql_error() );

        if( !($row = mysql_fetch_assoc( $retval )) )
            throw new Exception( "No rows found, move along." );

        print_r( $row );
    }
    catch( Exception $x )
    {
        echo "An exception occurred: " . $x->getMessage();
    }

所以这就是丑陋的一面,但有几点需要注意:

可能不想使用 mysql_* 函数;改用 PDO(它还没有被淘汰) - 并且,使用准备好的参数来减轻 SQL 注入攻击。我可以补充的超级书呆子花絮是,将身份验证表例程与用户表例程分开通常是一个很好的逻辑,因为用户表通常最终在“外部”使用。查询 auth 以建立 ID,然后在单独的线程中使用用户表。这样,您的身份验证查询就不会占用两个表。

祝你好运!希望这可以帮助。

于 2013-08-16T18:55:03.707 回答
0

要仅返回 1 个帐户,请尝试

'SELECT member.*, account.* FROM member inner join account on member.mem_id = account.mem_id limit 1';

但是,要检索 memberid 的成员和帐户,查询将需要更改为在 mem_id 上包含 where 谓词。例如:

    $member_id = 'some memberid'
    'SELECT member.*, account.*
    FROM member inner join account
    on member.mem_id = account.mem_id 
    where member.mem_id = $member_id';
于 2013-08-13T03:12:46.780 回答
0

您正在比较相同的 ID:“WHERE member.mem_id = account.mem_id”(我假设 member.mem_id 和 account.mem_id 引用相同的“用户”)。

你应该这样做:

$query = "SELECT member.*, account.*
FROM member, account
WHERE member.mem_id = " . $the_id;

其中 $the_id 应该是您从表单中获得的数字(需要说明的是 HTML 表单)。

希望能帮助到你 :)

于 2013-08-13T03:19:14.553 回答