我想从MySql表中检索数据。我正在使用Xampp、phpMyAdmin等……我一步一步地遵循了本教程:https://www.youtube.com/watch?v=IHdd02IK2Jg
但我得到了这个错误:
注意:未定义的变量:第29行C:''XAMPP''htdocs''display_prsc.php中的记录
还有一个警告:
警告:mysql_fetch_assoc()要求参数1为resource,在第29行C:''XAMPP''htdocs''display_prsc.php中给定null
当我运行它时,它只显示列名。
<? php
//make connection
mysql_connect('localhost','root','');
//select_db
mysql_select_db('mynewdb');
$sql="SELECT * FROM new";
$records=mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
<tr>
<?php
while ($new=mysql_fetch_assoc($records)){
echo"<tr>";
echo"<td>".$new['py']."</td>";
echo"<td>".$new['pcrit']."</td>";
echo"<td>".$new['psco']."</td>";
echo"</tr>";
}
?>
</table>
</body>
</head>
</html>
您的代码中有一些错误:
1.php打开标签错误
您必须删除php打开标签中的空格,例如
<?php
2.奇怪的html
几乎你的整个html都错了!我建议你读一本书或看一个基本的html教程
所以你的整个代码应该是这样的:
<?php
//make connection
mysql_connect('localhost', 'root', '');
//select_db
mysql_select_db('mynewdb');
$sql = "SELECT * FROM new";
$records = mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
</head> <!-- head tag closed here -->
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
</tr> <!-- forgot / to close the tag -->
<?php
while ($new = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" . $new['py'] . "</td>";
echo "<td>" . $new['pcrit'] . "</td>";
echo "<td>" . $new['psco'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
旁注:
1.可以改进什么?
- 我会做一些基本的错误检查,例如,如果连接失败等等
- 将
mysqli_*
与已准备好的语句结合使用,或者将PDO
与已准备的语句结合,它们会安全得多!(我们生活在2015年) - (此外,如果你教程中的男生使用
mysql_*
,请更改教程!)
因此,您不仅可以使用旧代码,还可以使用改进后的代码:
<?php
//Database stuff
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "mynewdb";
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT * FROM new");
$stmt->execute();
//Close Connection
$dbh = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
<html>
<head>
<title>Program Scores Table</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
</tr>
<?php
while ($new = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>";
echo "<td>" . $new['py'] . "</td>";
echo "<td>" . $new['pcrit'] . "</td>";
echo "<td>" . $new['psco'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
2.编码旁注
将错误报告添加到文件顶部,这将有助于查找错误。
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
错误报告只能在暂存阶段进行,而不能在生产阶段进行。
注意第一个php标记中的空格。你有<? php
,应该是<?php
既然你似乎已经开始学习了,就要避免学习那些不受欢迎的东西。不要使用MySQL,使用PDO或Mysqli。
尝试
<?php
//make connection
$con= mysql_connect('localhost','root','');
//select_db
$select= mysql_select_db('mynewdb',$con);
$sql="SELECT * FROM new";
$records=mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
<tr>
<?php
while ($new=mysql_fetch_array($records)){
echo"<tr>";
echo"<td>".$new['py']."</td>";
echo"<td>".$new['pcrit']."</td>";
echo"<td>".$new['psco']."</td>";
echo"</tr>";
}
?>
</table>
</body>
</head>
</html>
我建议您使用针对MySQL的Prepared语句,而不是干净的MySQL,它根本不安全,以后会给您带来问题。
这里有个轻快的向导
http://www.w3schools.com/pHp/php_mysql_prepared_statements.asp
您的代码中存在多个错误。但对于错误,这是由于mysql_query()
在第行返回FALSE:
$records=mysql_query($sql);
那么,线while ($new=mysql_fetch_assoc($records)){
不能得到$records
的值,从而产生误差。
其他错误包括:
HTML
- 缺少DOCTYPE
- 缺少元字符集
- 缺少
</tr>
- 放错地方的
</head>
- 要现代化,请使用CSS为HTML设置样式,而不是使用HTML标记属性
PHP
- 在使用函数之前没有检查函数的返回值
- 使用不推荐使用的
mysql_*
函数;请改用PDO/MySQLi <? php
的额外空间