-1

I have recently started using PHP to implement a basic ranking system. Here is my code:

review.php

<?php 
require 'connect.inc.php';
mysql_select_db("conference");
$query = mysql_query("SELECT * FROM event");
$event = [];

while($row = mysql_fetch_array($query)){
    $event [] = $row;
}
?>
<?php foreach($event as $events): ?>
        <div class="event">

        <h3><a href="event.php?id=<?php echo $events['eventID'];?>"><?php echo $events['eventName'];?></a><h3>
        <div class="event-rating"> Rating: x/5</div>
        </div>
    <?php endforeach;?>

event.php

<?php 
require 'connect.inc.php';
$event= null;

if(isset($_GET['eventID'])){
    $id=(int)$_GET['eventID'];
    $event = mysql_query("SELECT * FROM event WHERE eventID = {$id}")->fetch_object();
}   
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
    <?php if($event):?>
        <div class="event">
            This is Event "<?php echo $event['eventName'];?>"
            <div class="event-rating"> Rating: x/5</div>
            <div class="event-rate"> Rating: x/5</div> Rate this Event:
            <?php foreach(range(1,5)as $rating):?>
                <a href=""><?php echo $rating; ?></a>
            <?php endforeach ?>
        </div>
            <?php endif;?>
    </body>
</html>

The problem I have is that nothing is outputting onto the webpage, whereas it should display a rating page from 1 to 5. I believe the problem lies within the ->fetch_object(); line because this had been causing me problems before so I used alternatives such as mysql_fetch_array.

I also tried starting again using PDO and mysqli connections but still not having any luck.

If anyone could provide any advice it would be greatly appreciated.

Also, if anyone can explain ->fetch_object(); that would be very useful as there isn't much explanation online.

kdav4
  • 39
  • 2
  • 2
  • 5
  • `is that nothing is outputting onto the webpage` in which file? Have you read logs? Check your database tables? Var_dumped vars? Have you done anything except posting codes? – u_mulder Jan 10 '15 at 19:00

1 Answers1

1

OK, this one is going to be a tricky one to explain, since it appears as though you don't have a basic understanding of how PHP's mysql_* functions work.

In PHP, the -> operator is used to access properties and functions of an Object type. mysql_query() returns a Resource Identifier, which cannot be used as an object within PHP. Instead, you need to use the procedural mysql_fetch_object( $result ); function. However, later in your code, you try to use the returned object as an array, for example:

This is Event "<?php echo $event['eventName'];?>"

The next issue you face is that the mysql_* family of functions is now deprecated, and will soon be disappearing from new versions of PHP. As a result, you're much better off going with one of the newer libraries, such as PDO.

Your code can be rewritten in PDO, and I've taken the time to do that for you:

<?php
$db = new PDO('mysql:dbname=conference;host=127.0.0.1', 'username', 'password');
$result = $db->query( 'SELECT * FROM event' );

while( $event = $result->fetchObject() ):
?>
    <div class="event">
        <h3><a href="event.php?id=<?php echo $event->eventID ?>"><?php echo $event->eventName ?></a><h3>
        <div class="event-rating"> Rating: x/5</div>
    </div>
<?php endwhile ?>

Similarly, inside of event.php, you should be using Prepared Statements to prevent SQL injection attacks on your site. For example:

<?php 
$db = new PDO('mysql:dbname=conference;host=127.0.0.1', 'username', 'password');

if( isset($_REQUESR['eventID']) )
{
    $sql = $db->prepare( 'SELECT * FROM event WHERE eventID = :eid' );
    $sql->execute( array(':eid' => $_REQUEST['eventID']) );
    $event = $sql->fetchObject();
}   
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
    <?php if($event):?>
        <div class="event">
            This is Event "<?php echo $event->eventName ?>"
            <div class="event-rating"> Rating: x/5</div>
            <div class="event-rate"> Rating: x/5</div> Rate this Event:
            <?php foreach(range(1,5)as $rating):?>
                <a href=""><?php echo $rating; ?></a>
            <?php endforeach ?>
        </div>
            <?php endif;?>
    </body>
</html>

Hopefully the above friendly advice will help you with your learning and development with PHP.

Community
  • 1
  • 1
BenM
  • 52,573
  • 26
  • 113
  • 168
  • Thank you very much for the advice. I did not realise this was the case, but im glad that you have been kind enough to explain it. I have spent some time looking though the resources you provided and code and started again :) (for the better) – kdav4 Jan 11 '15 at 15:54
  • I'm sorry I can't vote twice. Nice and enhanced answer. – Stephan Vierkant Jan 11 '15 at 17:10