php 在php中显示mysql查询的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17388800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:48:13  来源:igfitidea点击:

Display results from mysql query in php

phpmysql

提问by user2536134

I am trying to develop an upcoming course dates for my website. I want to maintain the data in an mysql database. I have set up 3 table in msql.

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

1.courses

1.课程

2.category

2.类别

3.coursedates

3.课程日期

All are linked by course_id.

所有都通过 course_id 链接。

Basically I want to present the data from coursedates in the following way in PHP using a query.

基本上,我想在 PHP 中使用查询以下列方式显示课程日期中的数据。

Course Title          No Of Days            Course Date

课程名称 天数 课程日期

I have tried using the follwoing coding

我尝试使用以下编码

<?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);
?> 

采纳答案by Ahmed Atta

Replace your code with the following :

将您的代码替换为以下内容:

<?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);
?> 

回答by user4035

You are mixing mysql_* and mysqli:

您正在混合 mysql_* 和 mysqli:

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

But:

但:

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

Use mysqli_query instead:

使用 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);
?> 

回答by Strawberry

Note that your query might be more legible rewritten something like this...

请注意,您的查询可能会更清晰,重写如下...

"
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;
 "

Incidentally, it seems odd that you could have course dates for a course that didn't exist, so that first OUTER JOIN could probably be rewritten as an INNER JOIN!

顺便说一句,您可以为不存在的课程设置课程日期似乎很奇怪,因此第一个 OUTER JOIN 可能会被重写为 INNER JOIN!