简体   繁体   English

尝试使用PHP和MySQL(内部联接)返回记录集

[英]Trying to return a record set using PHP & MySQL (inner join)

I am new to PHP/MySQL to please bear with me. 我是PHP / MySQL的新手,请多多包涵。 I am trying to have PHP write a table which returns a list of records from a join table. 我试图让PHP编写一个表,该表从连接表返回记录列表。 The SQL statement works perfectly when I run the query but I do not know how to write the function properly. 当我运行查询时,SQL语句可以完美地工作,但是我不知道如何正确地编写函数。

SQL statement which works: 有效的SQL语句:


SELECT members.nick_name, assets.asset_desc, shares.asset_cost, shares.percent_owner
          FROM 
            (shares INNER JOIN assets
            ON shares.asset_ID = assets.asset_ID)
            INNER JOIN members
            ON shares.member_ID = members.member_ID
            WHERE shares.member_ID = $member_ID"

My functions: 我的职能:


function get_shares_by_member($member_ID) {
global $db;
$query = "SELECT members.nick_name, assets.asset_desc, shares.asset_cost, shares.percent_owner
          FROM 
            (shares INNER JOIN assets
            ON shares.asset_ID = assets.asset_ID)
            INNER JOIN members
            ON shares.member_ID = members.member_ID
            WHERE shares.member_ID = $member_ID";
$share_result = $db->query($query);
$share_result = $share_result->fetch();
return $share_result;
}

function get_shares() {
global $db;
$query = "SELECT * FROM shares";
$share = $db->query($query);
$shares_table = $share->fetch();
return $share;
}

My action: 我的动作:

if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_shares';
}

if ($action == 'list_shares') {
if (!isset($member_ID)) {
    $member_ID = 0;
}
$shares = get_shares_by_member($member_ID);
$share = get_shares();
}

Here is my table: 这是我的桌子:


<table>
<tr>
    <th>Nick Name</th>
    <th>Asset Description</th>
    <th>Asset Cost</th>
    <th class="right">% Ownership<th>
    <th>&nbsp;</th>
</tr>
<?php foreach ($shares_table as $share) : ?>
<tr>
    <td><?php echo $share['nick_name']; ?></td>
    <td><?php echo $share['asset_desc']; ?></td>
    <td><?php echo $share['asset_cost']; ?></td>
    <td class="right"><?php echo $share['percent_owner']; ?></td>
    <td>&nbsp;</td>
</tr>
<?php endforeach; ?>
</table>

I know this is a lot to ask but I've been struggling with this for the past 3 days. 我知道有很多要问的问题,但是过去三天我一直在为此苦苦挣扎。 Any help will be much appreciated! 任何帮助都感激不尽! If anyone needs help with AD/Exchange, I'd be happy to share my knowledge in that area! 如果有人在AD / Exchange方面需要帮助,我很乐意分享我在该领域的知识!

Thanks!! 谢谢!!

From what I can see on here, this will produce a blank table with however many rows are returned for a number of reasons: 从这里我可以看到,这将产生一个空白表,但是由于多种原因返回了许多行:

  • None of the columns returned by the get_shares_by_member function are share_ID or asset_ID, so those columns won't be filled in. get_shares_by_member函数返回的列都不是share_ID或asset_ID,因此不会填充这些列。
  • You are referencing percent_owner from $shares which, assuming is an array as you are using it in a 'foreach' loop, will need an index to reference it, or it should otherwise be $share['percent_owner'] 您正在从$ shares引用percent_owner,假设您在'foreach'循环中使用它时是一个数组,则需要一个索引来引用它,否则应为$ share ['percent_owner']
  • The percent_owner field will be put in the 'asset cost' column at present as there is no blank cell produced to move it to the '% ownership' column where it would seem logical to have it. 由于没有空白单元格可将其移到“拥有百分比”列,因此似乎会合乎逻辑,因此,目前将“%_owner”字段放在“资产成本”列中。

Based on what you have posted so far, the following should suit your needs: 根据您到目前为止发布的内容,以下内容将满足您的需求:

<table>
    <tr>
        <th>Nick Name</th>
        <th>Asset Description</th>
        <th>Asset Cost</th>
        <th class="right">% Ownership<th>
        <th>&nbsp;</th>
    </tr>
    <?php foreach ($shares as $share) : ?>
    <tr>
        <td><?php echo $share['nick_name']; ?></td>
        <td><?php echo $share['asset_desc']; ?></td>
        <td><?php echo $share['asset_cost']; ?></td>
        <td class="right"><?php echo $shares['percent_owner']; ?></td>
        <td>&nbsp;</td>
    </tr>
    <?php endforeach; ?>
</table>

I would recommend changing either the $share variable in the foreach, or the $share which is being set by get_shares(). 我建议更改在foreach中的$ share变量,或由get_shares()设置的$ share。 Personally speaking, I would change the latter from 就我个人而言,我将后者从

$share = get_shares();

to something like: 像这样:

$shares_table = get_shares();

as it is essentially containing all of the information from the shares table, assuming the database abstraction layer function fetch() returns all results. 因为它实质上包含了shares表中的所有信息,所以假设数据库抽象层函数fetch()返回所有结果。

There could also be an issue when you are doing: 执行操作时可能还会出现问题:

$share = $db->query($query);
$share = $share->fetch();

Going from different database abstraction layers I have seen, I would expect fetch() to be done as (using your variables) 从我所见过的不同数据库抽象层来看,我希望fetch()可以完成(使用您的变量)

$share = $db->fetch();

If the fetch() is requiring a result to be passed into it, then I would expect the code to look similar to: 如果fetch()要求将结果传递给它,那么我希望代码看起来类似于:

$share_result = $db->query($query);
$share = $db->fetch($share_result);

a few points: 几点:

  1. are you sure your query does not return any errors? 您确定查询不返回任何错误吗? If there are errors, that might cause fetch() to fail 如果有错误,则可能导致fetch()失败
  2. what DB class are you using? 您正在使用哪种数据库类? I would suggest that you please check that there is a fetch() function and what parameters does it accept? 我建议您检查是否有fetch()函数以及它接受哪些参数? For example, the fetch() may be invoked like $share_result->fetch() or $db->fetch() or $db->fetch($share_result) etc. 例如,可以像$ share_result-> fetch()或$ db-> fetch()或$ db-> fetch($ share_result)等那样调用fetch()。
  3. I might be wrong but it seems that the fetch() might be always returning the first row from the resultset. 我可能是错的,但似乎fetch()可能总是从结果集中返回第一行。 Perhaps you might need to do fetch() in a loop for reading all results 也许您可能需要在循环中执行fetch()才能读取所有结果

You may as well try using the default PHP functions. 您也可以尝试使用默认的PHP函数。 Here is a code snippet that explains how you may rewrite your functions using PHP's default mysql() library: 以下代码段说明了如何使用PHP的默认mysql()库重写函数:

mysql_connect('your host', 'your user', 'your password'); function get_shares_by_member($member_ID) {
    $output = Array();
    $query = "SELECT members.nick_name, assets.asset_desc, shares.asset_cost, shares.percent_owner
              FROM 
                (shares INNER JOIN assets ON shares.asset_ID = assets.asset_ID)
                INNER JOIN members ON shares.member_ID = members.member_ID
                WHERE shares.member_ID = $member_ID";
    $share_result = mysql_query($query);
    while ($row = mysql_fetch_assoc($share_result)) {
        $output[] = $row;
    }
    return $output;
}

function get_shares() {
    $output = Array();
    $query = "SELECT * FROM shares";
    $share = mysql_query($query);
    while ($row = mysql_fetch_assoc($share)) {
        $output[] = $row;
    }
    return $output; 
}

Hope the above helps. 希望以上内容对您有所帮助。 Please feel free to let me know if there is anything that is not clear. 如果有任何不清楚的地方,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM