在php中显示mysql查询的结果

时间:2013-06-30 09:02:31

标签: php mysql

我正在尝试为我的网站开发即将开课的日期。我想在mysql数据库中维护数据。我在msql中设置了3个表。

1.courses

2.category

3.coursedates

所有都是由course_id链接的。

基本上我想使用查询在PHP中以下列方式呈现来自coursedates的数据。

课程名称天数课程日期

我尝试过使用以下编码

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


$query = "SELECT `coursedates`.`coursedate_id`,`courses`.`course_title`,`courses`.`no_of_days`,`category`.`category_name`,`coursedates`.`date1` FROM coursedates\n"
    . "LEFT JOIN `mentertraining`.`courses` ON `coursedates`.`course_id` = `courses`.`course_id` \n"
    . "LEFT JOIN `mentertraining`.`category` ON `courses`.`category_id` = `category`.`category_id` LIMIT 0, 30 ";

$result = mysql_query($query);
    echo "<table border='1'>
<tr>
<th>Course Title</th>
<th>Course Date</th>

</tr>";

while($row    = mysql_fetch_assoc($result))
  {
  echo "<tr>";
  echo "<td>" . $row['course_title'] . "</td>";
  echo "<td>" . $row['date1'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 

3 个答案:

答案 0 :(得分:1)

你正在混合mysql_ *和mysqli:

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

可是:

$result = mysql_query($query);//?

改为使用mysqli_query:

$query = "SELECT 
       `coursedates`.`coursedate_id`,
       `courses`.`course_title`,
       `courses`.`no_of_days`,
       `category`.`category_name`,
       `coursedates`.`date1` 
FROM
    coursedates
LEFT JOIN
      `mentertraining`.`courses` 
ON 
   `coursedates`.`course_id` = `courses`.`course_id`
LEFT JOIN
     `mentertraining`.`category` 
ON 
   `courses`.`category_id` = `category`.`category_id`
LIMIT 0, 30";

$result = mysqli_query($query);
echo "<table border='1'>
<tr>
<th>Course Title</th>
<th>Course Date</th>

</tr>";

/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>" . $row['course_title'] . "</td>";
    echo "<td>" . $row['date1'] . "</td>";
    echo "</tr>";
}

/* free result set */
mysqli_free_result($result);

echo "</table>";

mysqli_close($con);
?> 

答案 1 :(得分:1)

使用以下内容替换您的代码:

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


$query = "SELECT `coursedates`.`coursedate_id`,`courses`.`course_title`,`courses`.`no_of_days`,`category`.`category_name`,`coursedates`.`date1` FROM coursedates "
    . " LEFT JOIN `mentertraining`.`courses` ON `coursedates`.`course_id` = `courses`.`course_id` "
    . " LEFT JOIN `mentertraining`.`category` ON `courses`.`category_id` = `category`.`category_id` LIMIT 0, 30 ";

$result = mysqli_query($con,$query);
    echo "<table border='1'><tr><th>Course Title</th><th>Course Date</th></tr>";

while($row    = mysqli_fetch_assoc($result))
  {
  echo "<tr>";
  echo "<td>" . $row['course_title'] . "</td>";
  echo "<td>" . $row['date1'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 

答案 2 :(得分:0)

请注意,您的查询可能会更清晰地重写这样的内容......

"
SELECT cd.coursedate_id
     , c.course_title
     , c.no_of_days
     , t.category_name
     , cd.date1 
  FROM coursedates cd
  LEFT 
  JOIN courses c
    ON c.course_id = cd.course_id 
  LEFT 
  JOIN category t
    ON t.category_id = c.category_id 
 LIMIT 0, 30;
 "

顺便说一下,你可以为不存在的课程设定课程日期似乎很奇怪,这样第一次外部联接可能会被重写为内部联接!