user2536134
user2536134

Reputation: 25

Display results from mysql query in php

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.

1.courses

2.category

3.coursedates

All are linked by course_id.

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

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

Upvotes: 1

Views: 28909

Answers (3)

user4035
user4035

Reputation: 23759

You are mixing mysql_* and mysqli:

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

But:

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

Use mysqli_query instead:

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

Upvotes: 1

Strawberry
Strawberry

Reputation: 33935

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!

Upvotes: 0

Ahmed Atta
Ahmed Atta

Reputation: 373

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

Upvotes: 2

Related Questions