Object PHP and MYSQL queries
Get the solution ↓↓↓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?
Answer
Solution:
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.
Answer
Solution:
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;
}
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: an exception occurred in the driver: could not find driver
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
MySQL
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.