×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Retrieve/Dispaly data

Retrieve/Dispaly data

Retrieve/Dispaly data

(OP)
I give up. I have been reading tons of webpages and cannot figure out why this is not returning any results. I know the data is there, punctuation is correct for the query, the connection works, etc. There are no errors, just no data. Pls help....

CODE

$pdo = new PDO("mysql:host=$hostname; dbname=$database;" , $username, $password);

$dept = "Jeans";
$emp = $pdo->prepare("Select * From Employees Where department = " . $dept);
$emps = $emp->execute();


foreach ($emps as $row) {
 echo $row['fname'] . ' ' . $row['lname'] . '<br>';

} 

RE: Retrieve/Dispaly data

The PDO execute() method, does not return rows of data. It returns true on success and false of failure. That's it. Nothing more.
http://php.net/manual/en/pdostatement.execute.php

To get the data out for display you need to use the fetch or fetchAll methods after a successful execute().

http://php.net/manual/en/pdostatement.fetch.php

CODE

$emp->execute();
    while ($row = $emp->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
      $data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n";
      print $data;
    } 

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Retrieve/Dispaly data

(OP)
So I modified the code and still get no results.

CODE

$emp = $pdo->prepare("Select * From Employees Where department = " . $dept);
$emp->execute();

while ($row = $emp->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
	echo $row['fname'];
} 

RE: Retrieve/Dispaly data

The FETCH_NUM constant indicates fetch() will return an array with numbered indexes, not names. If you want names use FETCH_ASSOC.

CODE

while ($row = $emp->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
	echo $row['fname'];
} 

I strongly suggest you take a minute and read the entries in the PHP manual I linked to above. Its explained there.

also, it sounds like your PHP installation does not have errors turned on, for development, you should always have errors turned on.

CODE

error_reporting(E_ALL); 
ini_set("display_errors", 1); 

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Retrieve/Dispaly data

(OP)
I did look at the links, I am trying to learn PDO, I have been a classic ASP programmer for years. I truly find it unnecessarily difficult to simply return a recordset using this.
I have errors turned on. Using the while loop that you provided it returned no results, even after removing the 'where' clause to further make it easier to find them. What is wrong here?!

Table: 'Employees'
Field: 'fname', 'lname'
Contains 5 records.

CODE

$emp = $pdo->prepare("Select * From Employees");
$emp->execute();

while ($row = $emp->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
	echo $row['fname'];
} 
Code returns no results and no errors.

I currently feel like I am spinning my wheels getting nowhere. Please help

RE: Retrieve/Dispaly data

O.k, lets dial it back, and try a simple direct query.

CODE

$rows= $pdo->query("Select * From Employees");

foreach($rows as $row)
echo $row['fname'];
} 

If the connection is working, that should absolutely show results.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Retrieve/Dispaly data

(OP)
It returned this..." Invalid argument supplied for foreach() in /var/www..."

RE: Retrieve/Dispaly data

(OP)
Ok, this is seems like a simple problem and it was. I use an include file which is for my db connection so I only use 1 line. The program was set to use the configuration for my host, not locally! Thank you for all the help!!!!!!!

RE: Retrieve/Dispaly data

Glad you worked out. Sorry I did not answer earlier. Had yo go away for a few days.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close