尝试将我的PHP代码用于一个简单论坛时,我一直收到错误消息

时间:2019-08-30 05:22:03

标签: php mysql

尝试此PHP代码时,我不断收到这三个错误,但我不明白我在做什么错。 *请注意,这仅用于学校项目。

mysqli_select_db()恰好需要2个参数,第15行的C:\ xampp \ htdocs \ Forum \ forum.php中给出1个参数

注意:未定义的变量:第15行的C:\ xampp \ htdocs \ Forum \ forum.php中的con

警告:mysqli_error()期望参数1为mysqli,在第15行的C:\ xampp \ htdocs \ Forum \ forum.php中给出空值

我认为也许将其更改为mysqli可以,但是没有。

<html>
<head>
<title>Forum</title>
</head>
<body>
<table border="1" cellpadding="4" width="100%">
<tr>
<td>Forum</td>
<td>Number of Topics</td>
<td>Number of Replies</td>
<td>Last Poster</td>
</tr>
<?php
mysqli_connect("localhost","root","");
mysqli_select_db('forum') or die(mysqli_error($con));
$query1 = mysqli_query('SELECT * FROM main ORDER BY id DESC'); 
while ($output1 = mysql_fetch_assoc($query1))
{
echo '<tr>';
echo '<td><a href="topics.php? 
id='.$output1['id'].'">'.$output1['name'].'</a></td>';
echo '<td>'.$output1['topics'].'</td>';
echo '<td>'.$output1['replies'].'</td>';
if (empty($output1['lastposter']))
    echo '<td>No Posts</td>';
else
    echo '<td>'.$output1['lastposter'].' @ '.date('d-m-y G:i', 
$output1['lastpostdate']).'</td>';
echo'</tr>';
}
?>
</table>
</body>
</html>

3 个答案:

答案 0 :(得分:-1)

更新的代码:

<html>
<head>
<title>Forum</title>
</head>
<body>
<table border="1" cellpadding="4" width="100%">
<tr>
<td>Forum</td>
<td>Number of Topics</td>
<td>Number of Replies</td>
<td>Last Poster</td>
</tr>
<?php
$db = mysqli_connect("localhost","root",""); // you forgot this. we need a mysql connection to be stored in variable which we can use throughout the file.
mysqli_select_db($db,'forum') or die(mysqli_error($db)); // mysqli_select_db requires two parameters first is mysqli_connection and second is db.
$query1 = mysqli_query($db,'SELECT * FROM main ORDER BY id DESC'); 
while ($output1 = mysqli_fetch_assoc($query1)) // you wrote mysql which is deprecated
{
echo '<tr>';
echo '<td><a href="topics.php? 
id='.$output1['id'].'">'.$output1['name'].'</a></td>';
echo '<td>'.$output1['topics'].'</td>';
echo '<td>'.$output1['replies'].'</td>';
if (empty($output1['lastposter']))
    echo '<td>No Posts</td>';
else
    echo '<td>'.$output1['lastposter'].' @ '.date('d-m-y G:i', 
$output1['lastpostdate']).'</td>';
echo'</tr>';
}
?>
</table>
</body>
</html>

答案 1 :(得分:-1)

<html>
<head>
<title>Forum</title>
</head>
<body>
<table border="1" cellpadding="4" width="100%">
<tr>
<td>Forum</td>
<td>Number of Topics</td>
<td>Number of Replies</td>
<td>Last Poster</td>
</tr>
<?php
 // $con Saves return object representing the connection to the MySQL server

$con=mysqli_connect("localhost","root","");

mysqli_select_db($con,'forum') or die(mysqli_error($con));

 //mysqli_query(connection,query,resultmode) ; here $con is connection

$query1 = mysqli_query($con,'SELECT * FROM main ORDER BY id DESC'); 


while ($output1 = mysql_fetch_assoc($query1))
{
echo '<tr>';
echo '<td><a href="topics.php? 
id='.$output1['id'].'">'.$output1['name'].'</a></td>';
echo '<td>'.$output1['topics'].'</td>';
  echo '<td>'.$output1['replies'].'</td>';
 if (empty($output1['lastposter']))
  echo '<td>No Posts</td>';
 else
  echo '<td>'.$output1['lastposter'].' @ '.date('d-m-y G:i', 
  $output1['lastpostdate']).'</td>';
 echo'</tr>';
 }
 ?>
 </table>
 </body>
  </html>

我相信您可以尝试一下。

答案 2 :(得分:-1)

问题是您正在使用 mysqli_select_db ,而没有将身份验证传递给SQL服务器。这就是为什么您得到此错误。

因此,首先需要与数据库凭据建立连接。

$con = mysqli_connect("localhost","root","db_password_if_any");

然后您应该选择数据库

mysqli_select_db($con,'forum') or die(mysqli_error($con));

尝试一下

$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,'forum') or die(mysqli_error($con));

您将这两行合并,可以尝试

$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

然后您将不需要此行, mysqli_select_db($ con,'forum')或die(mysqli_error($ con));