我想使用php从mysql表中获取数据。请问,有人能告诉我这段代码有什么问题吗?从mysql数据库获取数据的正确代码是什么:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$connect=mysql_connect("localhost","root","");
// connect to databsase
mysql_select_db("form1",);
enter code here
// query the database
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
// fetch the result / convert resulte in to array
while ($rows = mysql_fetch_array($query)):
$rows = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment'];
echo "$Name<br>$Address<br>$Email<br>$Subject<br>$Comment<br><br>";
endwhile;
?>
答案 0 :(得分:5)
php中的变量区分大小写。请用以下内容替换你的while循环:
while ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;
答案 1 :(得分:2)
试试这个
<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'database name';
// 2. Create a database connection
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 3. Select a database to use
$db_select = mysql_select_db($dbname,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
while ($rows = mysql_fetch_array($query)) {
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
}
?>
未经测试!! *更新!!
答案 2 :(得分:1)
将“WHILE”更改为“while”。因为php区分大小写,如c / c ++。
答案 3 :(得分:1)
<table border="1px">
<tr>
<th>Student Name</th>
<th>Email</th>
<th>password</th>
</tr>
<?php
If(mysql_num_rows($result)>0)
{
while($rows=mysql_fetch_array($result))
{
?>
<?php echo "<tr>";?>
<td><?php echo $rows['userName'];?> </td>
<td><?php echo $rows['email'];?></td>
<td><?php echo $rows['password'];?></td>
<?php echo "</tr>";?>
<?php
}
}
?>
</table>
<?php
}
?>
答案 4 :(得分:0)
尝试
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ")or die(mysql_error());
并检查是否会抛出任何错误。
然后使用while($rows = mysql_fetch_assoc($query)):
最后将其显示为
echo $name . "<br/>" . $address . "<br/>" . $email . "<br/>" . $subject . "<br/>" . $comment . "<br/><br/>" . ;
不要弃用用户mysql_*
。
答案 5 :(得分:0)
使用此代码
while ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment'];
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;
?>
答案 6 :(得分:0)
选择标识为mysql_select_db的数据库(“form1”,$ connect);
您是否收到语法错误?如果请放一个; $ comment = $ rows ['Comment']旁边。
此处变量应区分大小写
答案 7 :(得分:0)
如果这是您拥有的代码,那么您将收到错误,因为您在循环中重新分配$ row,因此您永远无法迭代结果。替换
$rows = $rows['Name'];
与
$name = $rows['Name']'
所以你的代码看起来像
WHILE ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment'];
此外,我假设表用户中的列名是姓名,地址,电子邮件等,而不是姓名,地址,电子邮件。请记住,每个变量名称/字段nameh都区分大小写。
答案 8 :(得分:0)
您的语法错误......正确的编码是:
<?php
mysql_connect("localhost","root","");
mysql_select_db("form1");
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
while($rows = mysql_fetch_array($query))
{
$rows = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo $rows.'</br>'.$address.'</br>'.$email.'</br>'.$subject.'</br>'.$comment;
}
?>
答案 9 :(得分:0)
代码:
while ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;