0

I'm experiencing mysql with object-oriented php, so i created a db with only one table called users that i want to query using php classes (the insertion function properly works)

This is my php user class:

<?php
class user{
    var $name; 
    var $surname;
    var $email;
    var $age;
    function set_user_properties($pname, $psurname, $pemail, $page){
        include "include.php";
        $sql = "INSERT INTO users (name, surname, email, age)
        VALUES ('$pname', '$psurname','$pemail', '$page')";

        if ($conn->query($sql) === TRUE){
           echo "New record created successfully";
        }else{
           echo "Error: " . $sql . "<br>" . $conn->error;
        }   
         $conn->close();
    }
    function get_user_properties($name){
        include "include.php";
        $sql3 = "SELECT name, surname, email, age
        FROM users
        WHERE name='$name'";
        $result3 = $conn->query($sql3);
            if ($result3->num_rows > 0) {
            // output data of each row
                while($row3 = $result3->fetch_assoc()) {
                    $this->name=$row3["name"];
                    $this->surname=$row3["surname"];
                    $this->email=$row3["email"];
                    $this->age=$row3["age"];
                }
            }
        $conn->close();
        return $this;
    }
}
?>

And this is the page where i want to read all the query results:

<?php

include("class_lib.php");

$pname=$_POST["firstname"];

$user1=new user();
$user1->get_user_properties($pname);
echo $user1->name;
echo $user1->surname;
echo $user1->email;
echo $user1->age;

?>

My question is: when i make a query which is supposed to have more than just one result (like this one), how can i pass all the results to the destination page?

2 Answers 2

1

The problem is that your class only stores information for one user, and that means you can't really expect it to store multiple users at once.

So the question is: "how do I get an array of users instead of just one?" And the answer to that is to create a class that performs DB queries and parses each row of the query results into a user object

referencing this part of your code:

while($row3 = $result3->fetch_assoc()) {
     $this->name=$row3["name"];
     $this->surname=$row3["surname"];
     $this->email=$row3["email"];
     $this->age=$row3["age"];
}

If you add a constructor function to your user class to instantiate a user with a name, surname, email, and age, you can end up with code in your DB class that looks more like the following

$users = array();
...
while($row3 = $result3->fetch_assoc()) {
    $users[] = new user($row3["name"],
                   $row3["surname"],
                   $row3["email"],
                   $this->age=$row3["age"]);
}
...
return $users;

I hope that makes sense.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, i'll try, but when i call the method in another page, how should i make the call? I mean, now is "$user1=new user(); $user1->get_user_properties($pname);" but then how it'll be?
+1. I typically do it a bit different though: rather than having a separate class that creates the user objects, I have a static function that returns an array of user objects inside the class itself
@user3712139 It all depends on how you write your code. If you code a static function on the user class like @James Spense mentioned, it may look like $users = user::get_users(); or if you create a db class to perform queries, it could look like $db = new db(); $users = $db->get_users();
0

Thanks everyone for helping, I solved my problem adding a constructor and using a class method. Here is the code, in case someone else will struggle on the same problem.

This is the class_lib.php where is defined the all() method that make an array with the resutus of the query:

<?php
class User{
    var $name;
    var $surname;
    var $email;
    var $age;
   
   function __construct($pname, $psurname, $pemail, $page){
        $this->name=$pname;
        $this->surname=$psurname;
        $this->email=$pemail;
        $this->age=$page;
    }

public static function all(){
        include "include.php";
        $users=array();
        $sql3 = "SELECT name, surname, email, age FROM users";

        $result3 = $conn->query($sql3);
            if ($result3->num_rows > 0) {
                while($row3 = $result3->fetch_assoc()) {
                    $users[] = new User($row3["name"],$row3["surname"], $row3["email"],$row3["age"]);
                }
            }
        $conn->close();
        return $users;
    }
?>

And this is the code from where I call the all() method. In this case the variabile $users is an array of User objects.

<?php include("class_lib.php");
$users=User::all(); //<-- this is the call to the class method.
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.