[英]Select Column Data from Database (Cant be converted to number?)
我需要能够比较列中的值,而不是值在列中出现的次数。 我有多个表,它们都具有相同的20列“标题”条目,但另一列“ position_order”是数字的不同值。 我有一个带有“正确”值的基表,我想遍历“标题”列中的每个名称,并计算它们的“ position_order”和我的基本案例表的“ position_order”之间的差
我相信所有工作都可以进行,但是当我查询数据库以查找由于什么原因它存储在“ position_order”列中的值时,它不会作为我可以进行计算的数字返回。
<?php
echo "
<h3 class='text-center'>Table Name</h3>
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Title</th>
<th>Score</th>
</tr>
<tbody class='row_position'>"?>
<?php
require('db_config.php');
$tablename = $_SESSION['username'] . "_laliga";
$_SESSION['tablename'] = $tablename;
$sql = "SELECT * FROM $tablename ORDER BY position_order";
$users = $mysqli->query($sql);
while($user = $users->fetch_assoc()){
$sql1 = "SELECT `position_order` FROM '".$tablename."' WHERE `title` LIKE '".$user['title']."' ";
$sql2 = "SELECT `position_order` FROM `laliga` WHERE `title` LIKE '".$user['title']."' ";
$position1 = $mysqli->query($sql1);
$position2 = $mysqli->query($sql2);
?>
<tr id="<?php echo $user['id'] ?>">
<td><?php echo $user['position_order'] ?></td>
<td><?php echo $user['title'] ?></td>
<td><?php echo abs($position1-$position2); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
这是错误日志
[2019年5月16日22:19:48 UTC] PHP注意:类mysqli_result的对象无法在第75行的/Applications/MAMP/htdocs/user.php中转换为数字
请参阅mysqli_query
的文档: https : //www.php.net/manual/zh/mysqli.query.php
返回值
对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回mysqli_result对象。 对于其他成功的查询,mysqli_query()将返回TRUE。
$position1
和$position2
是对象实例,而不是数据库中的字段。 从来没有永远使用过mysqli,但您可能想要fetch_all
吗?
这个:
$result = $mysqli->query($sql1);
if ( ! $result ) {
throw new Exception('Query 1 failed');
}
返回一个mysqli_result对象,而不是一个数字。 您必须从中提取实际结果。
if ( $result->num_rows ) {
$row = $result->fetch_assoc();
$position1 = $row['position_order'];
}
$result->close();
$result = $mysqli->query($sql2);
if ( ! $result ) {
throw new Exception('Query 2 failed');
}
if ( $result->num_rows ) {
$row = $result->fetch_assoc();
$position2 = $row['position_order'];
}
$result->close();
if ( isset($position1) && isset($position2) ) {
$abs = abs($position1-$position2);
} else {
$abs = null;
}
// output here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.