【问题标题】:Sql could not fetch record of a column when i join two tables当我加入两个表时,Sql 无法获取列的记录
【发布时间】:2016-12-31 14:43:20
【问题描述】:

请尝试连接两个表以获取记录,并在一个表中回显记录。

第一个是表'customer',第二个是表'beneficiary1'。

什么时候,我尝试加入表格然后只获取记录 (i.e receiver_id and receiver_name) 的 'beneficiary1' 但该表未显示另一个表为“客户”的记录(即 profile_pictures)。

这是什么原因造成的,我已经尝试了所有我能想到的代码!

<?php
mysql_connect("localhost","root");
mysql_select_db("bank_db");
$sender_id=$_SESSION["login_id"];

$res=mysql_query("SELECT c.* , b.* FROM customer c,beneficiary1 b 
                  WHERE c.id=b.sender_id 
                    AND b.sender_id='$sender_id' 
                  ORDER BY c.id ASC LIMIT 4 ");
while($row=mysql_fetch_assoc($res))
{
    if($row['profile_pictures'] == ""){
        $output6 = "<img src='default.png' class='img-circle' alt='image' width='40' height='40'/>";

    }else{
        $output6 = "<img src='src='uploads/".$row['profile_pictures']."' class='img-circle' alt='image' width='50' height='50'/>";
    } 
    ?>                          
    <tr>

        <td class="center"><?php echo $row['profile_pictures']; ?></td>
        <td><span class="text-small block text-light">0059687310 - <?php echo $row['reciever_id']; ?></span><span class="text-large"><?php echo $row['reciever_name']; ?></span><a href="#" class="btn"><i class="fa fa-pencil"></i></a></td>
        <td class="center">
        <div>

        <div class="btn-group">
            <a class="btn btn-transparent-grey dropdown-toggle btn-sm" data-toggle="dropdown" href="#">
                <i class="fa fa-cog"></i> <span class="caret"></span>
            </a>

           <ul role="menu" class="dropdown-menu dropdown-dark pull-right">
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="#">
                        <i class="fa fa-edit"></i> Edit
                    </a>
                </li>

                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="#">
                        <i class="fa fa-share"></i> Share
                    </a>
                </li>

                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="#">
                        <i class="fa fa-times"></i> Remove
                    </a>
                </li>
            </ul>
        </div>
    </div></td>
</tr>
<?php
}
?> 

为了受益人!

<php
include '_inc/dbconn.php';
$sender_id=$_SESSION["login_id"];
$sql="SELECT * FROM beneficiary WHERE sender_id='$sender_id' AND status='ACTIVE' ";
                $result=  mysql_query($sql) or die(mysql_error());
                while($rws=  mysql_fetch_array($result)){

                    .$rws[3]. //receiver_id
                    .$rws[4]. //receiver_name
                }
?>

给客户

<php
include '_inc/dbconn.php';
$sql1="SELECT * FROM customer WHERE id='reciever_id' ";
                $result1=  mysql_query($sql1) or die(mysql_error());
                while($rows=  mysql_fetch_array($result1)){

                    .$rows[14]. //profile_pictures  
                }
?>

【问题讨论】:

  • 去掉查询中$senderid周围的单引号
  • 图像没有显示静止!
  • 一些合理的代码缩进是个好主意。它帮助我们阅读代码,更重要的是,它将帮助 您调试您的代码 Take a quick look at a coding standard 为您自己谋取利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。
  • @jophab 单引号不是问题
  • 您是否尝试过使用 phpMyAdmin 或类似的查询工具运行此查询?

标签: php html mysql echo jointable


【解决方案1】:

出于加入目的,我认为您需要按以下方式重新编写查询:

$res = mysql_query("SELECT c.* , b.* FROM customer c JOIN beneficiary1 b ON c.id = b.sender_id WHERE b.sender_id = $sender_id ORDER BY c.id ASC LIMIT 4")

试试这个。

【讨论】:

  • 你不知道 PHP 双引号中的自动 $var 扩展吗?
  • 我将查询更改为您的,但也没有用。
  • 请不要推荐使用不安全的查询 - 这个很容易被 SQL 注入攻击,应该从不被使用
【解决方案2】:

为 $sender_id 附加带有双引号的连续点

$res=mysql_query("SELECT c.* , b.* FROM customer c,beneficiary1 b WHERE c.id=b.sender_id AND b.sender_id=".$sender_id." ORDER BY c.id ASC LIMIT 4 ");

【讨论】:

  • 你不知道 PHP 双引号文字中的自动 $var 扩展吗?
  • @RiggsFolly 请我不要。那是什么?
  • 您能否向我们展示这两个表的架构。使用edit 链接将此信息添加到您的问题中
  • @RiggsFolly 我如何获得架构?我在哪里可以运行 sql 表来品尝?
  • 请不要推荐使用不安全的查询 - 这个很容易被 SQL 注入攻击,应该从不被使用
猜你喜欢
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
相关资源
最近更新 更多