Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve/Dispaly data 1

Status
Not open for further replies.

LWolf

Programmer
Feb 3, 2008
77
US
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>';

}
 
The PDO execute() method, does not return rows of data. It returns true on success and false of failure. That's it. Nothing more.

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


Code:
$[b]emp[/b]->execute();
    while ($row = $[b]emp[/b]->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
 
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'];
}
 
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::[b]FETCH_ASSOC[/b], 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
 
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
 
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
 
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!!!!!!!
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top