在按钮徽章上显示MySQL查询的输出

时间:2016-10-25 18:06:06

标签: php mysql

我是php mysql&的新手有这个查询我想在按钮徽章上显示mysql查询的输出。我试过以下代码 请帮助。

<?php
$con=mysqli_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysqli_select_db($con,"sales");
$sth = mysqli_query($con,"select sum(sal) as Salary from `sales` ");
$result = mysqli_query($con,$sth);
$row = mysqli_fetch_assoc($con,$result);     
echo $row;
?>
<html>
<head>
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<button type="button" class="btn btn-primary">Primary <span class="badge"><?php echo $row; ?></span></button>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

<?php echo ''.$row['Salary'].''; ?>

只要您验证了设置并且查询正确,上面的行就会输出您要查找的内容。

修改

<?php
 $con=mysqli_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysqli_select_db($con,"sales");
$sth = "select sum(sal) as Salary from `sales` ";
$result = mysqli_query($con,$sth);
$row = mysqli_fetch_assoc($result);  

试试