TechnoDiver Posted September 1, 2021 Author Share Posted September 1, 2021 12 hours ago, kicken said: Your User class code is expecting to receive an instance of the PDO class. That exists as the conn property on your Database class, referenced by $conn->conn. Thanks for all your responses and patience. My issue wasn't understanding the technical side that you were explaining (sometimes it is) in this specific instance I was having trouble with syntax, operators etc. I'm still trying to get a grasp of '->', '::' etc and how to move different types of data in the manner that needs to be used for that specific data type. Thanks for your help, I believe I've made it past that particular hiccup and will concentrate on learning and refactoring with PDO.. I'm sure we'll speak again. Have an awesome week Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589554 Share on other sites More sharing options...
TechnoDiver Posted September 1, 2021 Author Share Posted September 1, 2021 13 hours ago, kicken said: Your database class doesn't have a prepare method, that's why you get an error. You're right, it doesn't. This is my database class: class Database { private $dbhost = "localhost"; private $dbuser = "root"; private $dbpassword = ""; private $dbname = "qcic"; public $conn; public function __construct() { $dsn = "mysql:host=" . $this->dbhost . ";dbname=" . $this->dbname; $this->conn = new PDO($dsn, $this->dbuser, $this->dbpassword); } } I'm not sure what the prepare method would even consist of or how to implement it in this instance. I was just trying to create the connection. What would a prepare method in this class even do? Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589555 Share on other sites More sharing options...
kicken Posted September 1, 2021 Share Posted September 1, 2021 14 minutes ago, TechnoDiver said: What would a prepare method in this class even do? Based on how you're trying to use it, you would just pass along the data to PDO's prepare method. public function prepare($sql){ return $this->conn->prepare($sql); } There's not much benefit to that over just dealing with PDO directly. This is why I suggested earlier that you might be better off for now just dealing with PDO and drop the whole idea of having a Database class. All your database class is managing to do so far is get in your way and confuse you. Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589556 Share on other sites More sharing options...
TechnoDiver Posted September 1, 2021 Author Share Posted September 1, 2021 (edited) 26 minutes ago, kicken said: This is why I suggested earlier that you might be better off for now just dealing with PDO and drop the whole idea of having a Database class. All your database class is managing to do so far is get in your way and confuse you. I've done that and am making a bit of progress. my config.php file is now light, slim and simple -> <?php ob_start(); session_start(); $timezone = date_default_timezone_set("America/Cancun"); $whitelist = array('username', 'email', 'email2', 'password', 'password2'); $db_host = "localhost"; $db_user = "root"; $db_password = ""; $db_name = "qcic"; $conn = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_password); ob_end_flush(); I still have a lot of mysqli to change to PDO. But I've just run into another something. This is the start of my User class -> <?php class User { private $conn; private $username; public function __construct($conn, $username) { $this->conn = $conn; $query = "SELECT * FROM users WHERE username='$username'"; $statement = $this->conn->prepare($query); $statement->execute(); $this->user_data = $statement->fetchAll(PDO::FETCH_ASSOC); } public function getUsername() { print_r($this->user_data); $username = $this->user_data['username']; return $username; } public function getRole() { $role = $this->user_data['role']; return $role; } You can see where I printed the array in getUsername for test purposes. This is the array -> Quote Array ( [0] => Array ( [id] => 1 [firstname] => Some [lastname] => One [username] => TechnoDiver => technodiver@somemail.com [password] => xxxxxxx [signup_date] => 2021-08-08 [profile_pic] => the_original.png [num_posts] => 0 [num_likes] => 0 [user_closed] => [friend_array] => [role] => admin [subs] => main ) ) I'm getting these warnings -> Quote Warning: Undefined array key "username" in /opt/lampp/htdocs/qcic/assets/class/User.php on line 18Warning: Undefined array key "role" in /opt/lampp/htdocs/qcic/assets/class/User.php on line 28 The keys I specified are exactly how they are as the DB columns, and we can both see that they're defined. So what is causing these error messages? Edited September 1, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589557 Share on other sites More sharing options...
Barand Posted September 1, 2021 Share Posted September 1, 2021 fetchAll() returns an array of records. You want just the single record. Use fetch(); The reason for using prepared statements is to separate the date (like $username) from the query. Doing it like you are is as much use as a chocolate teapot. You should use placeholders for the data values. $query = "SELECT * FROM users WHERE username = ?"; $statement = $this->conn->prepare($query); $statement->execute( [$username] ); $this->user_data = $statement->fetch(PDO::FETCH_ASSOC); Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589558 Share on other sites More sharing options...
TechnoDiver Posted September 1, 2021 Author Share Posted September 1, 2021 1 hour ago, Barand said: fetchAll() returns an array of records. You want just the single record. Use fetch(); Alright, so to understand you better - Does this mean that a fetchAll() on this query: $query = "SELECT * FROM users WHERE username = ?"; would return all values in the table under the username column? So, all usernames in the table? Whereas fetch() returns the values in a specific row that the specific username is in? Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589559 Share on other sites More sharing options...
ginerjm Posted September 1, 2021 Share Posted September 1, 2021 A Fetch returns a Row of the results, not all of the rows like a fetchall would do. Each row then has the individual fields that were requested by the query. Most of the time you will execute your query, check if it succeeded and then begin a while loop that fetches a row and processes the data of that row. Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589561 Share on other sites More sharing options...
Barand Posted September 1, 2021 Share Posted September 1, 2021 51 minutes ago, TechnoDiver said: Alright, so to understand you better - Does this mean that a fetchAll() on this query: $query = "SELECT * FROM users WHERE username = ?"; would return all values in the table under the username column? So, all usernames in the table? No. If you run a query that returns several rows (eg all users older than 25), fetchAll() gives an array of all the returned rows. Fetch() will return a single row 1 ) When you use print_r() it is better to use it between <pre>..</pre> tags. It makes it much easier to see the array structure. EG echo '<pre>' . print_r($array, true) . '</pre>'; 2 ) Use that code to see the contents of $this->user_data returned with fetchAll() and then try it after using fetch() and see the difference. You should see that with fetchAll() you have an extra array level, so using $this->user_data['username'] fails. You need $this->user_data[0]['username'] to get the username from the first row in the array. Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589566 Share on other sites More sharing options...
TechnoDiver Posted September 1, 2021 Author Share Posted September 1, 2021 Ok, thank you for your responses. They were very helpful Quote Link to comment https://forums.phpfreaks.com/topic/313639-undefined-type-on-database-class/page/2/#findComment-1589571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.