Jump to content

Recommended Posts

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

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?

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.

 

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

I'm getting these warnings ->

Quote

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 by TechnoDiver

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);
        

 

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?

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.