Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从两个MYSql表中获取数据并显示为HTML表_Php_Mysql - Fatal编程技术网

Php 从两个MYSql表中获取数据并显示为HTML表

Php 从两个MYSql表中获取数据并显示为HTML表,php,mysql,Php,Mysql,我有两个表,第一个是“users”表,其中有一列名为“store”,还有一个表名为“stores”,其中列为“store number”“store location” 用户表中的“存储”列是“存储编号” 我想做的是创建一个类似 样本数据: 店号:34 店址:伦敦 用户:34 门店号|门店位置|此门店的用户数| 因此,它将类似于从存储中选择*并为每一行创建新行 对于用户的数量,可以是用户的sum*,其中“store”等于stores表中的“store number” 我希望这是有道理的 杰克 更

我有两个表,第一个是“users”表,其中有一列名为“store”,还有一个表名为“stores”,其中列为“store number”“store location”

用户表中的“存储”列是“存储编号”

我想做的是创建一个类似

样本数据:

店号:34 店址:伦敦 用户:34

门店号|门店位置|此门店的用户数|

因此,它将类似于从存储中选择*并为每一行创建新行

对于用户的数量,可以是用户的sum*,其中“store”等于stores表中的“store number”

我希望这是有道理的

杰克

更新:

这是正确的:

$result = mysql_query("SELECT * FROM stores", $con) or die(mysql_error());

echo "<table border='1'>";
echo "<tr> <th>Store Number</th> <th>Store Location</th> <th>Number of Users</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['storenumber'];
    echo "</td><td>"; 
    echo $row['location'];
    echo "</td><td>"; 
    echo 'Amount of users here';
    echo "</td></tr>"; 
} 

echo "</table>";
如果不存在,则创建表
用户
(

尝试以下SQL:

SELECT storenumber, location, COUNT(users.store) as nbr_users FROM stores
LEFT JOIN users ON stores.storenumber = users.store
GROUP BY store.id
然后呢

echo $row['nbr_users'];
要打印用户数,请尝试以下操作:

$result = mysql_query("SELECT * FROM stores", $con) or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Store Number</th> <th>Store Location</th> <th>Number of Users</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // gets the total amount of users
    $query = mysql_query("SELECT COUNT(*) AS total FROM users WHERE `store`='".$row['storenumber']."'") or die( mysql_error() );
    $r = mysql_fetch_array( $query );
    $total = $r['total'];
    unset($query, $r);

    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['storenumber'];
    echo "</td><td>"; 
    echo $row['location'];
    echo "</td><td>"; 
    echo $total;
    echo "</td></tr>"; 
} 

echo "</table>";
$result=mysql\u query(“从存储区选择*”,$con)或die(mysql\u error());
回声“;
回显“门店号门店位置用户号”;
//一直排到下一排,直到没有更多的东西了
while($row=mysql\u fetch\u数组($result)){
//获取用户总数
$query=mysql\u query(“选择COUNT(*)作为用户总数,其中'store`='”、$row['storenumber'].“'))或die(mysql\u error());
$r=mysql\u fetch\u数组($query);
$total=$r['total'];
未设置($r);
//将每行的内容打印到表格中
回声“;
echo$row['storenumber'];
回声“;
echo$row['location'];
回声“;
echo$总计;
回声“;
} 
回声“;
您可以试试这个-

$result = mysql_query("Select count(user_id) as CNT,storenumber,location from store Left join user ON user.store_number = store.store_number group by store.store_number", $con) or die(mysql_error());
$row = mysql_fetch_assoc($result );

echo "<table border='1'>";
echo "<tr> <th>Store Number</th> <th>Store Location</th> <th>Number of Users</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['storenumber'];
    echo "</td><td>"; 
    echo $row['location'];
    echo "</td><td>"; 
    echo $row['CNT'];
    echo "</td></tr>"; 
} 

echo "</table>";
$result=mysql\u query(“选择count(user\u id)作为CNT、storenumber、location from store Left join user ON user.store\u number=store.store\u number按store.store\u number分组),$con)或die(mysql\u error());
$row=mysql\u fetch\u assoc($result);
回声“;
回显“门店号门店位置用户号”;
//一直排到下一排,直到没有更多的东西了
while($row=mysql\u fetch\u数组($result)){
//将每行的内容打印到表格中
回声“;
echo$row['storenumber'];
回声“;
echo$row['location'];
回声“;
echo$row['CNT'];
回声“;
} 
回声“;

如果您能提供精确的表结构,我可以更准确地构建它。

一秒钟后我将更新…请使用表结构和示例数据更新。我已更新了我的解决方案。请参阅此处的SQLFIDLE:请使用两个表的
CREATE table
语句更新问题。首先,您使用'140'编写
2
然后
'140'没有显示
?我再次更新了SQL,请如上所述使用
计数(users.store)
。使用创建表更新了我的帖子,您的计数更新没有改变任何内容:(
users
table缺少表中的
store
字段。此外,我将
groupby
操作符更改为unique
id
字段,可能有助于解决缺少的140个问题。
SUM()
将给出用户id的总和,而不是用户id的数量。在本例中,用户id 1和2将给出3。
$result = mysql_query("SELECT * FROM stores", $con) or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Store Number</th> <th>Store Location</th> <th>Number of Users</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // gets the total amount of users
    $query = mysql_query("SELECT COUNT(*) AS total FROM users WHERE `store`='".$row['storenumber']."'") or die( mysql_error() );
    $r = mysql_fetch_array( $query );
    $total = $r['total'];
    unset($query, $r);

    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['storenumber'];
    echo "</td><td>"; 
    echo $row['location'];
    echo "</td><td>"; 
    echo $total;
    echo "</td></tr>"; 
} 

echo "</table>";
$result = mysql_query("Select count(user_id) as CNT,storenumber,location from store Left join user ON user.store_number = store.store_number group by store.store_number", $con) or die(mysql_error());
$row = mysql_fetch_assoc($result );

echo "<table border='1'>";
echo "<tr> <th>Store Number</th> <th>Store Location</th> <th>Number of Users</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['storenumber'];
    echo "</td><td>"; 
    echo $row['location'];
    echo "</td><td>"; 
    echo $row['CNT'];
    echo "</td></tr>"; 
} 

echo "</table>";