我有两个单独的应用程序,它们在MYSQL中使用相同的数据库。第一个应用程序是用Node.js编写的,并且将数据完全保存在数据库中,但是当我想从用PHP编码的第二个应用程序中读取数据时,数据看起来是隐藏的(无法选择结果)。我可以看到数据库中的数据,并且可以从与PHP中相同的查询中获得结果。当我手动添加数据时,可以从PHP应用程序访问它。
Node.js中的代码
const mysql = require('mysql')
const mysqlconnection = mysql.createConnection({
host:'172.16.X.X',
user:'XXX',
password:'XXXX',
database:'ramen'
});
var sql = "INSERT INTO challenge_usersolved (user, challenge_Id, username)
VALUES (?,?,?)";
var values = [challenge.name , challenge.difficulty, usname];
mysqlconnection.query(sql, values,(err, row)=>{
})
PHP端代码为:
<?php
$db = mysqli_connect('172.16.X.X', 'ramen', 'XXX', 'ramen', '3306');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT sum(challenge_Id)*100 as score, username FROM (SELECT
Distinct user, challenge_Id, username FROM challenge_usersolved) as T
GROUP BY username;";
$result = mysqli_query($db, $query);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " .
mysql_error();
}
$rs = mysqli_fetch_assoc($result);
echo "<html><head><title>Score Board</title></head><body>";
echo "<table align='center' border='1px' style='width:300px; line-height: 30px'>";
echo"<tr><th colspan='2'>Students Score</th></tr><t><th>Username</th> <th>Score</th></t>";
while($rows=mysqli_fetch_assoc($result) ){
echo "<tr><td>{$rows["username"]}</td><td>{$rows["score"]}</td></tr>";
}
echo "</body></html>";
?>
答案 0 :(得分:0)
您的
$rs = mysqli_fetch_assoc($result);
行将加载第一条记录,您无需对其进行任何操作。当您的代码到达while
循环时,尝试加载下一个(第二个)行,但是您只有一行,如注释部分所述。解决方法似乎是删除
$rs = mysqli_fetch_assoc($result);
行。