[英]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;
}
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> </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> </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: 从这里我可以看到,这将产生一个空白表,但是由于多种原因返回了许多行:
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> </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> </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: 几点:
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.