0

I'm trying to make an old PHP system working on my new site. However, for it to work, everything has to be using PDO instead of regular MySQL query. I'm just trying to make it work for fun, I'm not an expert at all and just experimenting. However, I can't seem to find a solution for the problem I'm having.

I just keep getting the error: Fatal error: Uncaught Error: Call to a member function query()

I've marked the lines with // ERROR LINE //

Original code:

<?php
include("connect.php");
$day = $_GET['week'];

if($_GET['week'] == ""){
    $day = date("l");
    $_GET['week'] = date("l");
}

$slot = mysql_fetch_array(mysql_query("SELECT * FROM rp_timetable WHERE day = '$day'"));

function outit($time, $num){
    global $day, $slot;
    $info = mysql_fetch_array(mysql_query("SELECT * FROM rp_users WHERE id = '".$slot[$num]."'"));
    if($slot[$num] == ""){ 
        $book[$num] = "OPEN SLOT"; 
    }else{
        $book[$num] = "<b>DJ ".$info['djname']."</b>";
    }
    if($num%2){
        echo "<tr class='colour'><td width='150px' align='center'>$time</td><td width='150px' align='center'>".$book[$num]."<td></tr>";
    }else{
        echo "<tr><td width='150px' align='center'>$time</td><td width='150px' align='center'>".$book[$num]."<td></tr>";
    }
}

echo "<div id='timetable_pub'><center>";
$result = mysql_query("SELECT sitename FROM rp_data");
while($row = mysql_fetch_assoc($result)) {
echo "<u>".$row['sitename']." Radio Timetable</u><p>";
}
echo "<h1>-You are currently viewing ";
echo "$day";
echo "'s timetable-</h1><p>";
echo "<a href='?week=Monday'>Monday</a> | <a href='?week=Tuesday'>Tuesday</a> | <a href='?week=Wednesday'>Wednesday</a> | <a href='?week=Thursday'>Thursday</a> | 
<a href='?week=Friday'>Friday</a> | 
<a href='?week=Saturday'>Saturday</a> | <a href='?week=Sunday'>Sunday</a><br><hr><br>
<table border='0' valign='middle' align='center'><tr>
<td width='150px' align='center'><u>Time</u></td>
<td width='150px' align='center'><u>DJ Booked</u><td></tr>";

outit('12:00 - 01:00 AM', 1);
$i = 2;
while($i <= 12){
    $start_time = $i - 1;
    $end_time = $i;
    if(strlen($start_time) == 1){$start_time = "0".$start_time;}
    if(strlen($end_time) == 1){$end_time = "0".$end_time;}
    $full_time = "$start_time:00 - $end_time:00 AM";
    outit($full_time, $i);
    $i++;
}
outit('12:00 - 01:00 PM', 13);
$i = 14;
while($i <= 24){
    $start_time = $i - 13;
    $end_time = $i - 12;
    if(strlen($start_time) == 1){$start_time = "0".$start_time;}
    if(strlen($end_time) == 1){$end_time = "0".$end_time;}
    $full_time = "$start_time:00 - $end_time:00 PM";
    outit($full_time, $i);
    $i++;
}

echo "</table>
</center><p></div>";
?>

Where I'm currently at (Could be entirely wrong tho):

<?php
$day = $_GET['week'];
echo $day;

if($_GET['week'] == ""){
    $day = date("l");
    $_GET['week'] = date("l");
}

// ERROR LINE //
$slotData = $dbh->query("SELECT * FROM rp_timetable WHERE day = '$day'");
$slot = $slotData->fetch();

function outit($time, $num){
    global $day, $slot;
    $slotInfo = $dbh->query("SELECT * FROM rp_users WHERE id = '".$slot[$num]."'");
    $info = $slotInfo->fetch();
    if($slot[$num] == ""){ 
        $book[$num] = "AutoDJ"; 
    }else{
        $book[$num] = "<b>".$info['djname']."</b>";
    }
    if($num%2){
        echo "<tr class='colour'><td width='150px' align='center'>$time</td><td width='150px' align='center'>".$book[$num]."<td></tr>";
    }else{
        echo "<tr><td width='150px' align='center'>$time</td><td width='150px' align='center'>".$book[$num]."<td></tr>";
    }
}
// ERROR LINE //
echo "<div id='timetable_pub'><center>";
$result = $dbh->query("SELECT sitename FROM rp_data");
while($row = $result->fetch()) {
echo "<u>".$row['sitename']." Radio Timetable</u><p>";
}
echo "<h1>-You are currently viewing ";
echo "$day";
echo "'s timetable-</h1><p>";
echo "<a href='?week=Monday'>Monday</a> | <a href='?week=Tuesday'>Tuesday</a> | <a href='?week=Wednesday'>Wednesday</a> | <a href='?week=Thursday'>Thursday</a> | 
<a href='?week=Friday'>Friday</a> | 
<a href='?week=Saturday'>Saturday</a> | <a href='?week=Sunday'>Sunday</a><br><hr><br>
<table border='0' valign='middle' align='center'><tr>
<td width='150px' align='center'><u>Time</u></td>
<td width='150px' align='center'><u>DJ Booked</u><td></tr>";

outit('12:00 - 01:00 AM', 1);
$i = 2;
while($i <= 12){
    $start_time = $i - 1;
    $end_time = $i;
    if(strlen($start_time) == 1){$start_time = "0".$start_time;}
    if(strlen($end_time) == 1){$end_time = "0".$end_time;}
    $full_time = "$start_time:00 - $end_time:00 AM";
    outit($full_time, $i);
    $i++;
}
outit('12:00 - 01:00 PM', 13);
$i = 14;
while($i <= 24){
    $start_time = $i - 13;
    $end_time = $i - 12;
    if(strlen($start_time) == 1){$start_time = "0".$start_time;}
    if(strlen($end_time) == 1){$end_time = "0".$end_time;}
    $full_time = "$start_time:00 - $end_time:00 PM";
    outit($full_time, $i);
    $i++;
}

echo "</table>
</center><p></div>";
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Squolz
  • 1

1 Answers1

-1

I have shown a piece of code to connect to database using pdo for php in your case, the $day cannot be directly passed inside the sql query as sql doest not accept that syntax ,hope it helps to understand

try {
    $connect = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $connect->prepare("SELECT * FROM rp_timetable WHERE day =?");
    $stmt->bindParam(1,$day,PDO::PARAM_STR);
    $stmt->execute();
    $response = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($response);  
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}