我正在尝试建立一个论坛,但是为此,我需要一个数据库白色数据。
这是我的脚本:
(连接和打印数据脚本)
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<div id ="frm">
<?php $con = connect(); ?>
<div>
<?php renderContent($con); ?>
</div>
</div>
</body>
</html>
<?php
function connect() {
$con=mysqli_connect("localhost", "root", "", "forum");
var_dump($con);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
return $con;
}
}
function renderContent($con) {
$sql = "SELECT * FROM textforum";
$result = $con->query($sql);
// output data of each row
echo $row["text"]. "<br>";
}
并且输出错误是:
object(mysqli)#1(19){[“ affected_rows”] => int(0)[“ client_info”] => string(79)“ mysqlnd 5.0.11-dev-20120503-$ Id:76b08b24596e12d4553bd41fc93cccd5bac2fe7a $” [“ client_version“] => int(50011)[” connect_errno“] => int(0)[” connect_error“] => NULL [” errno“] => int(0)[” error“] =>字符串(0) “” [“ error_list”] => array(0){} [“ field_count”] => int(0)[“ host_info”] =>字符串(20)“通过TCP / IP的本地主机” [“ info”] = > NULL [“ insert_id”] => int(0)[“ server_info”] =>字符串(21)“ 5.5.5-10.1.21-MariaDB” [“ server_version”] => int(50505)[“ stat” ] => string(134)“正常运行时间:7020线程:1问题:1674慢查询:0打开:38刷新表:1打开表:30查询每秒平均:0.238” [“ sqlstate”] => string(5) “ 00000” [“ protocol_version”] => int(10)[“ thread_id”] => int(177)[“ warning_count”] => int(0)}
注意:未定义变量:第42行的C:\ xampp \ htdocs \ LoginTest \ Login \ process.php中的行
ankit:
这是我的输出:
object(mysqli)#1(19){[“ affected_rows”] => int(0)[“ client_info”] => string(79)“ mysqlnd 5.0.11-dev-20120503-$ Id:76b08b24596e12d4553bd41fc93cccd5bac2fe7a $” [“ client_version“] => int(50011)[” connect_errno“] => int(0)[” connect_error“] => NULL [” errno“] => int(0)[” error“] =>字符串(0) “” [“ error_list”] => array(0){} [“ field_count”] => int(0)[“ host_info”] =>字符串(20)“通过TCP / IP的本地主机” [“ info”] = > NULL [“ insert_id”] => int(0)[“ server_info”] =>字符串(21)“ 5.5.5-10.1.21-MariaDB” [“ server_version”] => int(50505)[“ stat” ] => string(134)“正常运行时间:8825线程:1问题:1683慢查询:0打开:38刷新表:1打开表:30查询每秒平均:0.190” [“ sqlstate”] => string(5) “ 00000” [“ protocol_version”] => int(10)[“ thread_id”] => int(182)[“ warning_count”] => int(0)}
文本blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblbText blalblalblb
blalblalblbText是正确的输出,但其余的不是我如何摆脱它。
最佳答案
mysqli_connect()
接受第四个参数作为databasename
主机名
用户名
密码
数据库名称
您错过了在连接中传递数据库的机会
尝试这个:
$con=mysqli_connect("localhost", "root", "","database_name_here");
更新您的函数
renderContent
:function renderContent($con) {
$sql = "SELECT * FROM textforum";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["text"]. "<br>";
}
}else{
echo "0 results";
}
}