在从MySQL到PHP页面选择数据时遇到问题

时间:2016-09-28 17:07:05

标签: php mysql

我一直在关注从我的数据库中选择数据的非常好的YouTube视频,但我似乎无法做到正确。我可以插入没问题,但选择会让我感到很痛苦。

任何人都可以看到此脚本有任何问题吗?

我的问题是,如果我尝试加载它,页面就会变成空白。

这不是连接问题,因为我已经检查过了。

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<h1>Sam Boyle</h1>

<?php

$mysqli = NEW MySQLi (‘localhost’, ‘xxxx’, ‘xxxx’, ‘cbd_players’);

//Connect to the database
$resultSet = $mysqli->query("SELECT * FROM squad”);

//Count the returned rows
if($resultSet->num_rows > 0) {
//Turn results into arrays
     while($rows = $resultSet->fetch_assoc())
     {

$id = [‘id’];
$firstname = ['firstname'];
$lastname =  ['lastname'];
$squadnumber = ['squadnumber'];
$division = ['division'];
$position = ['position'];
$nationality = ['nationality'];
$games  = ['games'];
$goals = ['goals'];
$assists = ['assists'];
$yellows = ['yellows'];
$reds = ['reds'];

echo “<p>Name: $firstname $lastname</br> Number: $squadnumber </br>Division: $division </br> Position: $position </br> Nationality: $nationality </br>Games played: $games </br>Goals: $goals </br>Assists: $assists </br> Yellow cards: $yellows </br> Red cards: $reds</p>”;

}else {

echo “No information recorded.”
}


?>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

只需改变一下:

while($rows = $resultSet->fetch_assoc())
 {
   $id = [‘id’];
   $firstname = ['firstname'];
   $lastname =  ['lastname'];

这个

 while($rows = $resultSet->fetch_assoc())
 {
   $id = $rows["id"];
   $firstname = $rows["firstname"];
   $lastname =  $rows["lastname"];

答案 1 :(得分:0)

我已对您的代码进行了必要的更改。 请试试。 还要显示display_error以获取确切的错误。

<?php
$mysqli = NEW MySQLi ('localhost', 'xxxx', 'xxxx', 'cbd_players');

//Connect to the database
$resultSet = $mysqli->query("SELECT * FROM squad");

 //Count the returned rows
if($resultSet->num_rows > 0) {
  //Turn results into arrays
 while($rows = $resultSet->fetch_assoc())
 {

    $id = $rows['id'];
    $firstname = $rows['firstname'];
    $lastname =  $rows['lastname'];
    $squadnumber = $rows['squadnumber'];
    $division = $rows['division'];
    $position = $rows['position'];
    $nationality = $rows['nationality'];
    $games  = $rows['games'];
    $goals = $rows['goals'];
    $assists = $rows['assists'];
    $yellows = $rows['yellows'];
    $reds = $rows['reds'];

    echo "<p>Name: $firstname $lastname</br> Number: $squadnumber </br>Division: $division </br> Position: $position </br> Nationality: $nationality </br>Games played: $games </br>Goals: $goals </br>Assists: $assists </br> Yellow cards: $yellows </br> Red cards: $reds</p>";
 }
}else {

  echo "No information recorded.";
}


?>