1

Please am learning php OOP and am running into this errors How can i fix this? Here is my code below:

<?php
class songs_data {
private $conn;
public int $limit;

public function __construct()
{
 global $conn;
 $this->conn = $conn;
}
public function get_songs($limit){
  $sql = "SELECT * FROM songs WHERE published= true ORDER BY id DESC LIMIT ?"; // 
  SQL with parameters
  $stmt = $this->conn->prepare($sql);
  $stmt->bind_param("i", $limit);
  $stmt->execute();
  $songs = $stmt->get_songs($limit); // get the mysqli result
  // fetch data
  return $songs->fetch_assoc();
 }
}

And Here is my index.php code below:

 <?php 
     $song = new songs_data();
        $songs = $song->get_songs(5);
        foreach ($songs as $row) {
            echo $row["song_name"];
        }

  ?>
  • What is the data type of table column `published` ? – Paul T. Jul 19 '20 at 00:55
  • The data type is "Tinyint" – GILBERT MONDAY Jul 19 '20 at 01:01
  • Ok am coming let me try it now – GILBERT MONDAY Jul 19 '20 at 01:05
  • It is still showing the error: – GILBERT MONDAY Jul 19 '20 at 01:06
  • Fatal error: Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\toneupdatespro\includes\songs_data.php:14 Stack trace: #0 C:\xampp\htdocs\toneupdatespro\index.php(19): songs_data->get_songs(5) #1 {main} thrown in C:\xampp\htdocs\toneupdatespro\includes\songs_data.php on line 14 – GILBERT MONDAY Jul 19 '20 at 01:06
  • Where is `global $conn` defined? ... do a `var_dump($conn);` after the line with global to see what you get. – Paul T. Jul 19 '20 at 01:08
  • In my config.php file – GILBERT MONDAY Jul 19 '20 at 01:10
  • am coming let me try what you said – GILBERT MONDAY Jul 19 '20 at 01:11
  • Look at what i get – GILBERT MONDAY Jul 19 '20 at 01:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218122/discussion-between-paul-t-and-gilbert-monday). – Paul T. Jul 19 '20 at 01:13
  • object(mysqli)#1 (18) { ["affected_rows"]=> int(0) ["client_info"]=> string(13) "mysqlnd 7.4.8" ["client_version"]=> int(70408) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=> string(20) "localhost via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(21) "5.5.5-10.4.13-MariaDB" ["server_version"]=> int(100413) ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(216) ["warning_count"]=> int(0) } – GILBERT MONDAY Jul 19 '20 at 01:15

0 Answers0