Jump to content

Get all results, pdo


Pain

Recommended Posts

Hello, i need some help with pdo. I'm struggling while trying to echo out all results.

class Test {

public $testName;
public $testEmail;
public $testLastname;

public function testGetAll() {
 
$query = $this->db->prepare('SELECT * FROM customers');
$query->execute();
 
$this->testEmail        = $data['email'];
$this->testName       = $data['name'];
$this->testLastname = $data['lastname'];
 
}
}
Edited by Pain
Link to comment
Share on other sites

Thanks for the answer. 

 

When i retrieve those values from the db like this:

$i = 0;
foreach($profile->testGetAll() as $value[$i]) {
$value[$i];
$i++;
} 
 
 

 
i get a big bunch of rows. How can i assign a variable to each row? $name would be name, $lastname would be lastname etc
Edited by Pain
Link to comment
Share on other sites

Just because you are now using PDO doesn't mean the basic principles don't still apply.

$qstmt = $conn->prepare('SELECT * FROM customers');
$qstmt->execute();
while ($row = $qstmt->fetch(PDO::FETCH_ASSOC))
{ 
   $email = $row['email'];
   $testname = $row['name'];
   $testlastname = $row['lastname'];
   echo "$testname has a last name of $testlastname and an email of $email<br>";
}

Note that you don't need to use prepare here since you don't have any arguments to your query. 

Link to comment
Share on other sites

Thanks for the reply.

 

It works, thank you. However this code should be inside a class and i don't want my class to echo. I want to use these variables on another page. So if I have something like this in my class:

$i = 0;
while ($row = $qstmt->fetch(PDO::FETCH_ASSOC))
{
$email[$i] = $row['email'];
$testname[$i] = $row['name'];
$testlastname[$i] = $row['lastname'];
$i++;
}

How would i echo out $lastname[5] or any other random variable. 

 

So far i've got this :/

require_once('connect.php');
require_once('class/profile.php');
 
$profile = new Profile($db);
echo $profile->testGetAll();
Edited by Pain
Link to comment
Share on other sites

you're making this harder than it needs to be.

 

First, you're using a loop with fetch to put columns into 3 separate arrays. This is terrible for 2 reasons. First is that now you have to pass around 3 separate arrays instead of just one multi-dim array. Second, now you will almost certainly never need to make use of just the names or just the lastnames or just the emails. Instead, you will almost certainly be using a loop to have the data grouped by it's original row. IOW you've flipped the rows and columns for no practical reason.

 

You should put it to an array that looks something like this:

 

array(
  0 => array('email'=>'email address','name'=>'some name','lastname'=>'last name here'),
  1 => array('email'=>'email address','name'=>'some name','lastname'=>'last name here'),
  2 => array('email'=>'email address','name'=>'some name','lastname'=>'last name here'),
  // etc..
)
That way you can easily reference a single set. Examples:

 

echo first row:

echo $array[0]['email'];
echo all emails:

foreach ($array as $row) {
  echo $row['email'];
}
Which brings me back to your original use of fetch() and the while loop. All you need to do is use

 

$data = $qstmt->fetchAll(PDO::FETCH_ASSOC);
And then $data will be structured exactly as above. No loop or assigning it to other vars or nothing. That is the exact point of fetchAll: when you want to grab all the returned results and pass it somewhere else.
Link to comment
Share on other sites

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.