skip to Main Content

I have two table events and session which looks like this

Events table

enter image description here

Sessions tableenter image description here

Here is expected results

enter image description here

Here is my solution

  <table>
                  <tr>
                    <th>Sessions </th>
                    <th>konto</th>
                    <th>Mobile</th>
                    <th>Komputer</th>
                    <th>Date</th>
                  </tr>       
                  <?php
                      $conn = mysqli_connect("localhost", "root", "", "ideabank_julia");
                      // Check connection
                      if ($conn->connect_error) {
                      die("Connection failed: " . $conn->connect_error);
                      }
                      $sql = "SELECT sid, datetime, count(*) as num_rows, count(distinct sid) as sessions,  
                      sum( targetbuttonname = 'konto' ) as num_konto,
                      sum(devicetype ='Computer') as num_computer, 
                      sum(devicetype = 'Mobile') as num_mobile from events
                      INNER JOIN sessions ON events.sid = sessions.sid group by sid, datetime;";

                      $result = $conn->query($sql);
                      if ($result->num_rows > 0) {
                      // output data of each row
                      while($row = $result->fetch_assoc()) {
                      echo "<tr>
                      <td>". $row["num_rows"]."</td>
                      <td>". $row["num_konto"]."</td>         
                      <td>". $row["num_mobile"]. "</td>
                      <td>". $row["num_computer"]. "</td>
                      <td>". $row["datetime"]. "</td>

                      </tr>";
                      }
                      echo "</table>";
                      } else { echo "0 results"; }
                      $conn->close();
                      ?>
                </table>

Unfortunately when i run the script on phpmyadmin I am getting the following error

 # 1052 - Column: 'sid' in field list is ambiguous

And when I run on php script above I get the following error

Trying to get property of non-object

What am I doing wrong in my code?

2

Answers


  1. I think you should add alias to your selected columns to mention from which table you are taking the colums. Please Try the code below.

    $sql = "SELECT events.sid, events.datetime, count(events.*) as num_rows, count(distinct events.sid) as sessions,  
                      sum( events.targetbuttonname = 'konto' ) as num_konto,
                      sum(events.devicetype ='Computer') as num_computer, 
                      sum(events.devicetype = 'Mobile') as num_mobile from events
                      INNER JOIN sessions ON events.sid = sessions.sid group by sid, datetime";
    
    Login or Signup to reply.
  2. You should add alias name for coloumns in groupby condition.

      $sql = "SELECT even.sid, even.datetime, count(*) as num_rows, 
                   count(distinct even.sid) as sessions,  
                  sum( even.targetbuttonname = 'konto' ) as num_konto,
                  sum(devicetype ='Computer') as num_computer, 
                  sum(devicetype = 'Mobile') as num_mobile from events even
                  INNER JOIN sessions ON even.sid = sessions.sid group by even.sid, 
                  even.datetime";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search