PHP连接到MySQL数据库,但不会回显数据

时间:2020-06-09 21:54:55

标签: php html mysql database web

我能够连接我的mysql数据库,但它不会回显任何数据。 它输出连接成功,但仍未回显任何数据。我已经三遍检查拼写错误,我无法弄清楚。

我已经查看了用户的特权,我只是不了解它是如何连接的,但是不会回显数据。

index.php output

代码

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbName = "printshop";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbName, $dbport);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
/* Left Nav
-----------------------------------------------------------------------------*/
body {
	margin: 0px;
	padding: 0px;
	margin-left: 0px;
	height: 100vh;
}

.maincontent {
	margin-left: 200px;
	padding: 0px 0px;
	height: 1000px;
	background-color: rgb(238, 238, 238);
}

.sidebar {
	margin: 0;
	padding: 0;
	width: 200px;
	background-color: #28343a;
	position: fixed;
	height: 100%;
	overflow: auto;
  }
  
  .sidebar a {
	display: block;
	color: rgb(255, 255, 255);
	padding: 16px;
	text-decoration: none;
  }
   
  .sidebar a.active {
	background-color: #327491;
	color: white;
  }
  
  .sidebar a:hover:not(.active) {
	background-color: #1f282c;
	color: white;
  }
  
  div.content {
	margin-left: 200px;
	padding: 1px 16px;
	height: 1000px;
  }
  
  @media screen and (max-width: 700px) {
	.sidebar {
	  width: 100%;
	  height: auto;
	  position: relative;
	}
	.sidebar a {float: left;}
	div.content {margin-left: 0;}
  }
  
  @media screen and (max-width: 400px) {
	.sidebar a {
	  text-align: center;
	  float: none;
	}
  }

  /*Cards*/
  .cardcontainer {
	  padding-top: 20px;
	  margin-left: 20px;
	  margin-right: 20px;
	  display: flex;
  }

  div.card {
	width: 250px;
	box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
	text-align: center;
	margin: 20px;
  }
  
  div.header {
	background-image: linear-gradient(to bottom right, rgb(51, 96, 126), rgb(56, 104, 150));
	color: white;
	padding: 10px;
	font-size: 40px;
  }

  div.headergreen {
	background-image: linear-gradient(to bottom right, rgb(78, 168, 75), rgb(55, 124, 37));
	color: white;
	padding: 10px;
	font-size: 40px;
  }
  
  div.container {
	padding: 10px;
	background-color: white;
	font-weight: bold;
  }
<?php
	include_once 'includes/dbh.inc.php'
?>

<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src='https://kit.fontawesome.com/a076d05399.js'></script>
</head>

<body>
    <div class="sidebar">
        <a class="active" href="#home">Home</a>
        <a href="#news">Jobs</a>
        <a href="#contact">Contacts</a>
        <a href="#about">Invoices</a>
    </div>

    <div class="maincontent">
        <div class="cardcontainer">
            <div class="card">
                <div class="header">
                    <h1>8</h1>
                </div>
                <div class="container">
                    <p>Jobs In Production</p>
                </div>
            </div>

            <div class="card">
                <div class="headergreen">
                    <h1>3</h1>
                </div>
                <div class="container">
                    <p>Jobs Completed</p>
                </div>
            </div>
        </div>
    </div>

    <?php
			$sql = "SELECT firstname FROM customers;";
			$results = mysqli_query($conn, $sql);
			$resultCheck = mysqli_num_rows($result);

			if ($resultCheck > 0) {
				while ($row = mysqli_fetch_assoc($result)) {
					echo $row['firstname'];
				}
			}
		?>

</body>

</html>

2 个答案:

答案 0 :(得分:-1)

不应有';'在脚本的这一行

$sql = "SELECT firstname FROM customers;";

应该是:

$sql = "SELECT firstname FROM customers";

答案 1 :(得分:-1)

首先尝试删除逗号
来自这个$sql = "SELECT firstname FROM customers;";
为此$sql = "SELECT firstname FROM customers";

如果仍然无法正常工作,请尝试类似的操作

include('yourConfig.php'); // Have you included this connection file??
$conn->set_charset('utf8');
$sql = "SELECT firstname FROM customers";
$result = $conn->query($sql);
$resultCheck = mysqli_num_rows($result);

// output data of each row
if ($resultCheck > 0) {
    while($row = $result->fetch_assoc()) 
    {
        echo $row["commonname"];
    } else {
        echo "please try again";
    }
}

mysqli_close($conn);