为什么我的PHP mysql调用不起作用?执行时不显示任何内容

时间:2018-01-23 04:48:37

标签: php mysql

为什么我的PHP mysql调用不起作用?执行时没有显示

简化为:

$votesSql = "SELECT SUM(col1) FROM database.table WHERE col2 = value";
$votes = mysqli_query($con, $votesSql);

<div class="col-md-1"><b id="<?=$data['discord_id']?>_vote_count"><?= $votes ?></b> Votes</div>

2 个答案:

答案 0 :(得分:1)

您错过fetch the resultsquery所在的行,如下所示:

<?php
  $votesSql = "SELECT SUM(col1), discord_id FROM database.table WHERE col2 = value";
  $votes = mysqli_query($con, $votesSql);

  $row = mysqli_fetch_array($votes);    // you were missing this line

  echo '<div class="col-md-1"><b id="'.$row['discord_id'].'_vote_count">'.$row[0].'</b> Votes</div>';
?>

虽然正如@tadman上面已经指出的那样,您应该考虑使用prepared statements切换到mysqli

您的查询将如下所示:

$con = new mysqli($servername, $username, $password, $dbname);

$votesSql = $con->prepare("SELECT SUM(col1), discord_id FROM database.table WHERE col2 = ?");
$votesSql->bind_param("s", $value);

$value = "yeah";
$votesSql->execute();

while ($row = $votesSql->fetch_array())
{
   echo '<div class="col-md-1"><b id="'.$row['discord_id'].'_vote_count">'.$row[0].'</b> Votes</div>';
}

更多关于prepared statementsphp.net/prepared-statements

答案 1 :(得分:0)

<?php
$votesSql = "SELECT SUM(col1) FROM database.table WHERE col2 = value";
$votes = mysqli_query($con, $votesSql);

while($data= $votes->fetch_assoc()) { // any fetch method use ?> 

<div class="col-md-1"><b id="<?=$data['discord_id']?>_vote_count"><?= $votes ?></b> Votes</div>

<?php } ?>