相对于表1,从表2中添加数据

时间:2013-08-27 07:57:37

标签: php mysql sql

我需要显示一个表格,显示将笑话输入数据库的用户。我有一个关于笑话名称'笑话'的表和一个名为'author'的用户的表 笑话表有id,joketext,jokedate,authorid 作者表有id,名称,电子邮件 因此'authorid'与作者表中的'id'相同。

<?php
$con=mysqli_connect("localhost","user","password","ijdb");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysql_query("SELECT joke.id, joketext, name, email FROM joke INNER JOIN author
ON authorid = author.id");

echo "<form action='delete1.php' method='post'>
<table border='1'>
<tr>
<th>Joke</th>
<th>Date</th>
<th>Name</th>
<th>Email</th>
<th>Delete</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['joketext'] . "</td>";
  echo "<td>" . $row['jokedate'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td><input type='submit' name='deleteItem' value='".$row['id']."' /></td>";
  echo "</tr>";
  }
  echo "</table>";

  echo "</form></br>";

mysqli_close($con);
?>

我收到此错误 警告:mysqli_fetch_array()期望参数1为mysqli_result,第22行的C:\ xampp \ htdocs \ kimmy \ jokes \ joke1.php中给出布尔值 和一张空表

由于

3 个答案:

答案 0 :(得分:2)

您正在使用错误     的mysql_query 代替     mysqli_query

所以代码变成了

<?php
$con=mysqli_connect("localhost","user","password","ijdb");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query("SELECT joke.id, joketext, name, email FROM joke INNER JOIN author
ON authorid = author.id");

echo "<form action='delete1.php' method='post'>
<table border='1'>
<tr>
<th>Joke</th>
<th>Date</th>
<th>Name</th>
<th>Email</th>
<th>Delete</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['joketext'] . "</td>";
  echo "<td>" . $row['jokedate'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td><input type='submit' name='deleteItem' value='".$row['id']."' /></td>";
  echo "</tr>";
  }
  echo "</table>";

  echo "</form></br>";

mysqli_close($con);
?>

答案 1 :(得分:2)

应该是:

$result = mysqli_query($con, "SELECT joke.id, joketext, name, email FROM joke INNER JOIN author
                              ON authorid = author.id");

当您使用mysql_query()扩展名时,您无法使用mysqli

答案 2 :(得分:1)

尝试更改以下内容

$result = mysql_query("SELECT joke.id, joketext, name, email FROM joke INNER JOIN author

ON authorid = author.id“);

$result = mysqli_query("SELECT joke.id, joketext, name, email FROM joke INNER JOIN author

ON authorid = author.id“);

您正在使用mysql_query而不是mysqli_query