rdoliv
June 21, 2011, 8:06pm
1
Hi everyone,
I’m building a test page to print a table with two rows in PHP using OOP.
I currently have this on my test.php file:
define("value", 1);
require_once 'admin/classes_php/Queries.class.php';
$query = new Queries();
$test = $query->myQuery(value);
echo $test;
on my Queries.class.php I have this:
require_once 'Populate.class.php';
public function myQuery($value) {
$populate = new Populate();
$query = "SELECT name
FROM users
WHERE id_type= ?";
return $populate->pullTest($query, $value);
}
on my Populate.class.php I have this:
function pullTest($query, $value) {
$result = $this->db->prepare($query);
$result->bind_param('i', $value);
$result->execute();
$result->store_result();
$result->bind_result($users);
if($result->fetch()){
return $users;
$result->free_result($users);
}
else
return 'Content not found';
}
The above works and prints just one row. However, I would like to print both rows on my sql.
I was testing like below, but I know something is wrong (for instance I don’t how to use $users and it also gives me error with fetch_array.
function pullTest($query, $value){
$result = $this->db->prepare($query);
$result->bind_param('i', $value);
$result->execute();
$result->store_result();
$result->bind_result($users);
if($result){
$rows = array();
while($row = $result->fetch_array()){
$rows[] = $row;
}
return $rows;
}
else
return false;
}
Any ideas on how to print both or more rows?
Thanks in advance.
rdoliv
June 22, 2011, 3:07pm
2
Thank you, Guido. It works now
One last question!
Let’s say that I now want to select every element in my table and not just the name.
$query = "SELECT *
FROM users
WHERE id_type= ?";
How should I do it?
$users should contain the result of the fetch. Try this:
function pullTest($query, $value) {
$result = $this->db->prepare($query);
$result->bind_param('i', $value);
$result->execute();
$result->store_result();
$result->bind_result($users);
$rows = array();
while ($result->fetch_array()) {
$rows[] = $users;
}
if (count($rows) == 0) {
return false
} else {
return $rows;
}
}
rdoliv
June 22, 2011, 2:43pm
4
Hi Guido,
Thank you for your answer.
It makes more sense but still doesn’t work. I’m getting this error:
Call to undefined method mysqli_stmt::fetch_array()
I’m trying to google it and I believe it has something to do with mysqli extension, but I really don’t find a good answer for this.
Ah yes, forgot to change that.
Use $result->fetch() instead of $result->fetch_array().
rdoliv
June 22, 2011, 3:27pm
7
I’m going to try that… Thanks!
rdoliv
June 28, 2011, 2:13pm
8
Hi,
Sorry to answer so late.
I’m having troubles using the way you’ve told me.
Now I have this:
function pullTest($query, $value) {
$result = $this->db->prepare($query);
$result->bind_param('i', $value);
$result->execute();
$result->store_result();
$result->bind_result($users, $logins);
$rows = array();
while ($result->fetch()) {
$rows[] = $users;
}
if (count($rows) == 0) {
return false
} else {
return $rows;
}
}
As you can see I have the line that states:
$rows[] = $users;
But I want two values, not one. I do I equal both values into the array and return it?
Do you want to store the two values in the $rows array? What must the $rows array look like after the while loop?
rdoliv
June 28, 2011, 2:22pm
10
I want to store anyway possible, actually I just want to be able to pass my database values to my index page to echo every element. I have something like this currently on my index page:
$test = $query->myQuery(value);
if(is_array($test)){
foreach($test as $tests){
echo $tests['users'];
echo $tests['logins];
}
}
rdoliv:
I want to store anyway possible, actually I just want to be able to pass my database values to my index page to echo every element. I have something like this currently on my index page:
$test = $query->myQuery(value);
if(is_array($test)){
foreach($test as $tests){
echo $tests['users'];
echo $tests['logins];
}
}
Ok then try
while ($result->fetch()) {
$rows[]['users'] = $users;
$rows[]['logins'] = $logins;
}
Hmm, that (see post above) might not work.
Maybe this is better
while ($result->fetch()) {
$newrow = array();
$newrow['users'] = $users;
$newrow['logins'] = $logins;
$rows[] = $newrow;
}
rdoliv
June 28, 2011, 2:49pm
13
Worked like a charm!
Thank you, Guido. For all your help.
You are the man.