can't retrieve more than 1 record at the time still

Hi Again,

Following the suggestions I received from a earlier post, I've closed
the statement
while ($stmt->fetch(PDO::FETCH_BOUND))
before $stmt = NULL;
I don't get an error anymore, but still retrieve only one record.

To check that the array was being filled correctly, I inserted in several places
$ncand = count($_SESSION['numberCand']);
$ndocid = count($_SESSION['DOCIDs']);
and the count is always 0 for $ndocid and 1 for $ncand.
Now I believe that the array is not being filled, and that's why only the last record gets processed.
Can anybody see what I'm doing wrong in the for each statement? The updated code is below.
Thank you for your help.

Catherine


Earlier message:
I'm really in need of help. I'm not a PHP programmer, but I've been
given the privilege of customizing a script written by somebody else and
can't get it to work right. I have to query 2 different tables in 2
different Oracle 10G DBs, one's mine (my_schema.my_table), the other
(otherdb.other_table) belongs to another department. In my_table, I
have the doc_id and the app_id for a record. In other_table there's the
doc_id and pdf to retrieve. The goal is to make a PDF file from each
BLOB in other_table and store them in the right directory under
hash(app_id). PDO has been installed and working, and I can connect to
both DBs without a problem. If my query limits the retrieval to one
record, the script works, but if I try to run it for all records, one
pdf file is correctly created in the right directory then I get the
following error: PHP Fatal error: Call to a member function fetch() on
a non-object in /my_location/my_script.php on line 154. It the "while
($stmt->fetch(PDO::FETCH_BOUND))" line. I've pasted my script below. I
thought my problem was that maybe I was in the wrong directory after
creation of the first pdf, but several tries changing the directory
didn't make a difference. Right now, I'm running the script at the
command line. Soon I'm going to have a few hundred records to deal with
and cannot do it one by one! Any help would be very much appreciated.
Thank you!

<?php


/* First Oracle DB connection info removed */

try
{
$dbh1 = new PDO("oci:dbname=".$tns1,$db1_username,$db1_password);

}

catch(PDOException $e)
{
echo ($e->getMessage());
exit;
}


if (empty($_SESSION['docIDs']))
{
$_SESSION['DOCIDs'] = array();
$_SESSION['msgs'] = array();

$sql = "SELECT COUNT(*) all_rec FROM myschema.mytable
where academic_year = 2010";

$_SESSION['numberCand'] = 0; /* initialize ctr for stack popping */
$_SESSION['PHPulled'] = 0; /* number new personal histories pulled */

if ($res = $dbh1->query($sql))

{

/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0)
{
/* Issue the real SELECT statement and work with the results */
$sql = "select doc_id, app_id
from myschema.mytable
where academic_year = 2010";

foreach ($dbh1->query($sql) as $row)
{

$cand = array();
$cand['DOC_ID']= $row['DOC_ID'];
$cand['APP_ID'] = $row['APP_ID'];
$_SESSION['DOC_IDS'][] = $cand;

}
}
$ncand = count($_SESSION['numberCand']);
$ndocid = count($_SESSION['DOCIDs']);
print "Cand Number: " . $ncand . "\n";
print "DOCid Number: " . $ndocid . "\n";
}
}

if (!empty($_SESSION['DOC_IDS']) and $_SESSION['numberCand'] < count($_SESSION['DOC_IDS']))
{
$ncand = count($_SESSION['numberCand']);
$ndocid = count($_SESSION['DOCIDs']);
print "Cand Number: " . $ncand . "\n";
print "DOCid Number: " . $ndocid . "\n";

/* if have doc_IDs, pick next one off array */
$cand = $_SESSION['DOC_IDS'][$_SESSION['numberCand']];

if ($cand['DOC_ID'] == 0)
{ /* redirect to self to get next candidate with updated numberCand */
$_SESSION['numberCand'] = $_SESSION['numberCand'] + 1;
exit;
}

$dirname = md5($cand['APP_ID']);
$curdir = '/my_location/'.$dirname;

print "App_id/Dir: " . $row['APP_ID'] . $curdir . "\n";

if (!(is_dir($curdir)))

{
if (!mkdir($curdir,0775))
print "error: " . $curdir . "\n";
exit;
}

/* Second Oracle DB connection info removed */

try
{
$dbh2 = new PDO("oci:dbname=".$tns2,$db2_username,$db2_password);

}

catch(PDOException $e)
{
echo ($e->getMessage());
exit;
}


$stmt = $dbh2->prepare('select PERSONAL_HIST_PDF_CONTENT from otherdb.other_table
where DOC_ID = :id');

$stmt->bindParam(':id', $cand['DOC_ID'], PDO::PARAM_INT);
$stmt->bindColumn(1, $PDF, PDO::PARAM_LOB);

$stmt->execute();

if (!(chdir($curdir)))
{
$_SESSION['msgs'][] = 'Could not change to '.$curdir;
continue;
}

$cnt = 0;

while ($stmt->fetch(PDO::FETCH_BOUND))
{
$filename = 'phs-'.$cnt.'.pdf';

if (!file_exists($filename))
{ /* if file not already there, write file with BLOB contents */
$fh = fopen($filename, 'w');
fwrite($fh, stream_get_contents($PDF));
fclose($fh);

/* add to $_SESSION['PHPulled'] for each new file saved */
$_SESSION['PHPulled'] = $_SESSION['PHPulled'] + 1;
}
}

/* increment stack counter */
$_SESSION['numberCand'] = $_SESSION['numberCand'] + 1;

$stmt = NULL; /* release the connection */

/*if not done with stack, redirect to self to get next*/
if (!empty($_SESSION['DOCIDs']) and
$_SESSION['numberCand'] < count($_SESSION['DOCIDs']))
{
exit;

}

}

/* once done, go back to display search page after clearing stack*/
if(isset($_SESSION['DOCIDs']))
unset($_SESSION['DOCIDs'] );

$_SESSION['msgs'][] = 'For '.$_SESSION['numberCand'].' candidate(s), '.
$_SESSION['PHPulled'].' personal histories were loaded.';
exit;

$res = null;
$dbh1 = null;

?>

--
Catherine Madsen

Programmer Analyst
College of Chemistry
Berkeley, CA 94720-1460

TEL: 510-643-1706
FAX: 510-643-6178


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Catherine Madsen [ Mo, 14 Dezember 2009 20:21 ] [ ID #2025923 ]

Problem with LDAP functions in PHP

Hello:
I am trying to accomplish searches in a Windows active directory through
LDAP functions in PHP, but the function ldap_search and ldap_get_entries are
out of service, they give the following error messages

Warning: ldap_search() [function.ldap-search]: Search: Operations error in
C:\xampp\htdocs\asesor\ldaptest.php on line 20

Warning: ldap_get_entries(): supplied argument is not a valid ldap result
resource in C:\xampp\htdocs\asesor\ldaptest.php on line 22

Here I send you the PHP code:
$r=ldap_bind($ds, 'iespaloma\\poweruser', 'dfku34*+); //
Autentificacion anonima, habitual de los accesos de solo lectura
echo "El resultado de la autentificacion es ".$r."
";
$persona = 'Ad';
$dn = "dc=iespaloma,dc=com";
$filtro="(givenname=$persona*)";
$solonecesito = array( "ou", "sn", "givenname", "mail");
$sr=ldap_search($ds, $dn, $filtro, $solonecesito);
$info = ldap_get_entries($ds, $sr);
I believe that the error occur due a bad DN that I am passing to the
function, Could anyone help me to solve this problem?



Aeropuerto Internacional Frank País García de Holguín.





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ricardo [ Mo, 14 Dezember 2009 22:39 ] [ ID #2025924 ]

Re: can't retrieve more than 1 record at the time still

2009/12/14 Catherine Madsen <cmadsen [at] berkeley.edu>:
> Hi Again,
>
> Following the suggestions I received from a earlier post, I've closed the
> statement
> while ($stmt->fetch(PDO::FETCH_BOUND))
> before $stmt =3D NULL;
> I don't get an error anymore, but still retrieve only one record.
>
> To check that the array was being filled correctly, I inserted in several
> places $ncand =3D count($_SESSION['numberCand']);
> $ndocid =3D count($_SESSION['DOCIDs']);
> and the count is always 0 for $ndocid and 1 for $ncand.
> Now I believe that the array is not being filled, and that's why only the
> last record gets processed. Can anybody see what I'm doing wrong in the f=
or
> each statement? =C2=A0The updated code is below.
> Thank you for your help.
> Catherine
>
>
> Earlier message:
> I'm really in need of help. =C2=A0I'm not a PHP programmer, but I've been=
given
> the privilege of customizing a script written by somebody else and can't =
get
> it to work right. =C2=A0I have to query 2 different tables in 2 different=
Oracle
> 10G DBs, one's mine (my_schema.my_table), the other (otherdb.other_table)
> belongs to another department. =C2=A0In my_table, I have the doc_id and t=
he
> app_id for a record. =C2=A0In other_table there's the doc_id and pdf =C2=
=A0to
> retrieve. =C2=A0The goal is to make a PDF file from each BLOB in other_ta=
ble and
> store them in the right directory under hash(app_id). =C2=A0PDO has been
> installed and working, and I can connect to both DBs without a problem. =
=C2=A0If
> my query limits the retrieval to one record, the script works, but if I t=
ry
> to run it for all records, one pdf =C2=A0file is correctly created in the=
right
> directory then =C2=A0I get the following error: PHP Fatal error: =C2=A0Ca=
ll to a
> member function fetch() on a non-object in /my_location/my_script.php on
> line 154. =C2=A0It the "while ($stmt->fetch(PDO::FETCH_BOUND))" line. =C2=
=A0I've
> pasted my script below. =C2=A0I thought my problem was that maybe I was i=
n the
> wrong directory after creation of the first pdf, but =C2=A0several tries =
changing
> the directory didn't make a difference. =C2=A0Right now, I'm running the =
script
> at the command line. =C2=A0Soon I'm going to have a few hundred records t=
o deal
> with and cannot do it one by one! =C2=A0Any help would be very much appre=
ciated.
> =C2=A0Thank you!
>
> <?php
>
>
> /* First Oracle DB connection info removed */
>
> try
> {
> =C2=A0 $dbh1 =3D new PDO("oci:dbname=3D".$tns1,$db1_username,$db1_passwor=
d);
>
> }
>
> catch(PDOException $e)
> {
> =C2=A0 echo ($e->getMessage());
> =C2=A0 exit;
> }
>
>
> if (empty($_SESSION['docIDs']))
> {
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$_SESSION['DOCIDs'] =3D array();
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$_SESSION['msgs'] =3D array();
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$sql =3D "SELECT COUNT(*) all_rec FROM myschem=
a.mytable
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0where academic_yea=
r =3D 2010";
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$_SESSION['numberCand'] =3D 0; =C2=A0/* initia=
lize ctr for stack popping */
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$_SESSION['PHPulled'] =3D 0; /* number new per=
sonal histories pulled */
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0if ($res =3D $dbh1->query($sql))
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0{
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0/* Check the number of rows that match the SEL=
ECT statement */
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if ($res->fetchColumn() > 0)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0{
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0/* Issue the real SELECT statement and work with the
> results */
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 $sql =3D "select doc_id, app_id
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0from myschema.mytable
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0where academic_year =3D 2010";
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 foreach ($=
dbh1->query($sql) as $row)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0$cand =3D array();
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0$cand['DOC_ID']=3D $row['DOC_ID'];
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0$cand['APP_ID'] =3D $row['APP_ID'];
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0$_SESSION['DOC_IDS'][] =3D $cand;
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
> $ncand =3D count($_SESSION['numberCand']);
> $ndocid =3D count($_SESSION['DOCIDs']);
> print "Cand Number: " . =C2=A0$ncand . "\n";
> print "DOCid Number: " . =C2=A0$ndocid . "\n";
> =C2=A0 =C2=A0 =C2=A0 =C2=A0}
> }
>
> if (!empty($_SESSION['DOC_IDS']) and $_SESSION['numberCand'] <
> count($_SESSION['DOC_IDS']))
> {
> $ncand =3D count($_SESSION['numberCand']);
> $ndocid =3D count($_SESSION['DOCIDs']);
> print "Cand Number: " . =C2=A0$ncand . "\n";
> print "DOCid Number: " . =C2=A0$ndocid . "\n";
>
> =C2=A0 /* if have doc_IDs, pick next one off array =C2=A0*/
> =C2=A0 $cand =3D $_SESSION['DOC_IDS'][$_SESSION['numberCand']];
>
> =C2=A0 if ($cand['DOC_ID'] =3D=3D 0)
> =C2=A0 { =C2=A0/* =C2=A0redirect to self to get next candidate with updat=
ed numberCand */
> =C2=A0 =C2=A0 =C2=A0 $_SESSION['numberCand'] =3D $_SESSION['numberCand'] =
+ 1;
> =C2=A0 =C2=A0 =C2=A0 exit;
> =C2=A0 }
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$dirname =3D md5($cand['APP_ID']);
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$curdir =3D '/my_location/'.$dirname;
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0print "App_id/Dir: " . =C2=A0$row['APP_ID'] . =
$curdir . "\n";
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (!(is_dir($curdir)))
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0{
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (!mkdir($curdir,0775))
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print "error: " . $curdir . "\=
n";
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0exit;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0}
>
> /* Second Oracle DB connection =C2=A0info removed */
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0try
> =C2=A0 =C2=A0 =C2=A0 =C2=A0{
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$dbh2 =3D new
> PDO("oci:dbname=3D".$tns2,$db2_username,$db2_password);
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0}
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0catch(PDOException $e)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0{
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0echo ($e->getMessa=
ge());
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0exit;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0}
>
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$stmt =3D $dbh2->prepare('select PERSONAL_HIST=
_PDF_CONTENT from
> otherdb.other_table
> =C2=A0 =C2=A0 =C2=A0 =C2=A0where DOC_ID =3D :id');
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$stmt->bindParam(':id', $cand['DOC_ID'], PDO::=
PARAM_INT);
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$stmt->bindColumn(1, $PDF, PDO::PARAM_LOB);
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$stmt->execute();
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0if (!(chdir($curdir)))
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0{
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0$_SESSION['msgs'][] =3D 'Could not change to '.$curdir;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0continue;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$cnt =3D 0;
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0while ($stmt->fetc=
h(PDO::FETCH_BOUND))
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0{
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0$filename =3D 'phs-'.$cnt.'.pdf';
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (!file_=
exists($filename))
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0{ /* if file not already there, write file with BLOB
> contents */
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$fh =3D fopen($filename, 'w');
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0fwrite($fh, stream_get_contents($PDF)=
);
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0fclose($fh);
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/* add to $_SESSION['PHPulled'] for e=
ach new
> file saved */
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$_SESSION['PHPulled'] =3D $_SESSION['=
PHPulled']
> + 1;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 }
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/* increment stack=
counter */
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$_SESSION['numberC=
and'] =3D $_SESSION['numberCand'] + 1;
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$stmt =3D NULL; /*=
release the connection */
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/*if not do=
ne with stack, redirect to self to get next*/
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (!empty($_SESSI=
ON['DOCIDs']) and
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0$_SESSION['numberCand'] < count($_SESSION['DOCIDs']))
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0{
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0exit;
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0}
>
> }
>
> /* once done, go back to display search page after clearing stack*/
> if(isset($_SESSION['DOCIDs']))
> unset($_SESSION['DOCIDs'] );
>
> $_SESSION['msgs'][] =3D 'For '.$_SESSION['numberCand'].' candidate(s), '.
> $_SESSION['PHPulled'].' personal histories were loaded.';
> exit;
>
> $res =3D null;
> $dbh1 =3D null;
>
> ?>
>
> --
> Catherine Madsen
>
> Programmer Analyst
> College of Chemistry
> Berkeley, CA 94720-1460
>
> TEL: 510-643-1706
> FAX: 510-643-6178
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

The PDOStatement->fetchAll() method will allow you to return all the
rows as an array.

http://docs.php.net/manual/en/pdostatement.fetchall.php

So, no need to iterate the result set.

Something along the lines of ...


<?php
$sth =3D $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result =3D $sth->fetchAll();
print_r($result);
?>

for example.
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Richard Quadling [ Di, 15 Dezember 2009 16:22 ] [ ID #2026158 ]

Re: can't retrieve more than 1 record at the time still

--------------000301000602040506000303
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Thank you so much for sending me in the right direction. I've
re-written the first part of the script using PDO statements and it
works great.

Catherine

Richard Quadling wrote:
> 2009/12/14 Catherine Madsen <cmadsen [at] berkeley.edu>:
>
>> Hi Again,
>>
>> Following the suggestions I received from a earlier post, I've closed the
>> statement
>> while ($stmt->fetch(PDO::FETCH_BOUND))
>> before $stmt = NULL;
>> I don't get an error anymore, but still retrieve only one record.
>>
>> To check that the array was being filled correctly, I inserted in several
>> places $ncand = count($_SESSION['numberCand']);
>> $ndocid = count($_SESSION['DOCIDs']);
>> and the count is always 0 for $ndocid and 1 for $ncand.
>> Now I believe that the array is not being filled, and that's why only the
>> last record gets processed. Can anybody see what I'm doing wrong in the for
>> each statement? The updated code is below.
>> Thank you for your help.
>> Catherine
>>
>>
>> Earlier message:
>> I'm really in need of help. I'm not a PHP programmer, but I've been given
>> the privilege of customizing a script written by somebody else and can't get
>> it to work right. I have to query 2 different tables in 2 different Oracle
>> 10G DBs, one's mine (my_schema.my_table), the other (otherdb.other_table)
>> belongs to another department. In my_table, I have the doc_id and the
>> app_id for a record. In other_table there's the doc_id and pdf to
>> retrieve. The goal is to make a PDF file from each BLOB in other_table and
>> store them in the right directory under hash(app_id). PDO has been
>> installed and working, and I can connect to both DBs without a problem. If
>> my query limits the retrieval to one record, the script works, but if I try
>> to run it for all records, one pdf file is correctly created in the right
>> directory then I get the following error: PHP Fatal error: Call to a
>> member function fetch() on a non-object in /my_location/my_script.php on
>> line 154. It the "while ($stmt->fetch(PDO::FETCH_BOUND))" line. I've
>> pasted my script below. I thought my problem was that maybe I was in the
>> wrong directory after creation of the first pdf, but several tries changing
>> the directory didn't make a difference. Right now, I'm running the script
>> at the command line. Soon I'm going to have a few hundred records to deal
>> with and cannot do it one by one! Any help would be very much appreciated.
>> Thank you!
>>
>> <?php
>>
>>
>> /* First Oracle DB connection info removed */
>>
>> try
>> {
>> $dbh1 = new PDO("oci:dbname=".$tns1,$db1_username,$db1_password);
>>
>> }
>>
>> catch(PDOException $e)
>> {
>> echo ($e->getMessage());
>> exit;
>> }
>>
>>
>> if (empty($_SESSION['docIDs']))
>> {
>> $_SESSION['DOCIDs'] = array();
>> $_SESSION['msgs'] = array();
>>
>> $sql = "SELECT COUNT(*) all_rec FROM myschema.mytable
>> where academic_year = 2010";
>>
>> $_SESSION['numberCand'] = 0; /* initialize ctr for stack popping */
>> $_SESSION['PHPulled'] = 0; /* number new personal histories pulled */
>>
>> if ($res = $dbh1->query($sql))
>>
>> {
>>
>> /* Check the number of rows that match the SELECT statement */
>> if ($res->fetchColumn() > 0)
>> {
>> /* Issue the real SELECT statement and work with the
>> results */
>> $sql = "select doc_id, app_id
>> from myschema.mytable
>> where academic_year = 2010";
>>
>> foreach ($dbh1->query($sql) as $row)
>> {
>>
>> $cand = array();
>> $cand['DOC_ID']= $row['DOC_ID'];
>> $cand['APP_ID'] = $row['APP_ID'];
>> $_SESSION['DOC_IDS'][] = $cand;
>>
>> }
>> }
>> $ncand = count($_SESSION['numberCand']);
>> $ndocid = count($_SESSION['DOCIDs']);
>> print "Cand Number: " . $ncand . "\n";
>> print "DOCid Number: " . $ndocid . "\n";
>> }
>> }
>>
>> if (!empty($_SESSION['DOC_IDS']) and $_SESSION['numberCand'] <
>> count($_SESSION['DOC_IDS']))
>> {
>> $ncand = count($_SESSION['numberCand']);
>> $ndocid = count($_SESSION['DOCIDs']);
>> print "Cand Number: " . $ncand . "\n";
>> print "DOCid Number: " . $ndocid . "\n";
>>
>> /* if have doc_IDs, pick next one off array */
>> $cand = $_SESSION['DOC_IDS'][$_SESSION['numberCand']];
>>
>> if ($cand['DOC_ID'] == 0)
>> { /* redirect to self to get next candidate with updated numberCand */
>> $_SESSION['numberCand'] = $_SESSION['numberCand'] + 1;
>> exit;
>> }
>>
>> $dirname = md5($cand['APP_ID']);
>> $curdir = '/my_location/'.$dirname;
>>
>> print "App_id/Dir: " . $row['APP_ID'] . $curdir . "\n";
>>
>> if (!(is_dir($curdir)))
>>
>> {
>> if (!mkdir($curdir,0775))
>> print "error: " . $curdir . "\n";
>> exit;
>> }
>>
>> /* Second Oracle DB connection info removed */
>>
>> try
>> {
>> $dbh2 = new
>> PDO("oci:dbname=".$tns2,$db2_username,$db2_password);
>>
>> }
>>
>> catch(PDOException $e)
>> {
>> echo ($e->getMessage());
>> exit;
>> }
>>
>>
>> $stmt = $dbh2->prepare('select PERSONAL_HIST_PDF_CONTENT from
>> otherdb.other_table
>> where DOC_ID = :id');
>>
>> $stmt->bindParam(':id', $cand['DOC_ID'], PDO::PARAM_INT);
>> $stmt->bindColumn(1, $PDF, PDO::PARAM_LOB);
>>
>> $stmt->execute();
>>
>> if (!(chdir($curdir)))
>> {
>> $_SESSION['msgs'][] = 'Could not change to '.$curdir;
>> continue;
>> }
>>
>> $cnt = 0;
>>
>> while ($stmt->fetch(PDO::FETCH_BOUND))
>> {
>> $filename = 'phs-'.$cnt.'.pdf';
>>
>> if (!file_exists($filename))
>> { /* if file not already there, write file with BLOB
>> contents */
>> $fh = fopen($filename, 'w');
>> fwrite($fh, stream_get_contents($PDF));
>> fclose($fh);
>>
>> /* add to $_SESSION['PHPulled'] for each new
>> file saved */
>> $_SESSION['PHPulled'] = $_SESSION['PHPulled']
>> + 1;
>> }
>> }
>>
>> /* increment stack counter */
>> $_SESSION['numberCand'] = $_SESSION['numberCand'] + 1;
>>
>> $stmt = NULL; /* release the connection */
>>
>> /*if not done with stack, redirect to self to get next*/
>> if (!empty($_SESSION['DOCIDs']) and
>> $_SESSION['numberCand'] < count($_SESSION['DOCIDs']))
>> {
>> exit;
>>
>> }
>>
>> }
>>
>> /* once done, go back to display search page after clearing stack*/
>> if(isset($_SESSION['DOCIDs']))
>> unset($_SESSION['DOCIDs'] );
>>
>> $_SESSION['msgs'][] = 'For '.$_SESSION['numberCand'].' candidate(s), '.
>> $_SESSION['PHPulled'].' personal histories were loaded.';
>> exit;
>>
>> $res = null;
>> $dbh1 = null;
>>
>> ?>
>>
>> --
>> Catherine Madsen
>>
>> Programmer Analyst
>> College of Chemistry
>> Berkeley, CA 94720-1460
>>
>> TEL: 510-643-1706
>> FAX: 510-643-6178
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>
>
> The PDOStatement->fetchAll() method will allow you to return all the
> rows as an array.
>
> http://docs.php.net/manual/en/pdostatement.fetchall.php
>
> So, no need to iterate the result set.
>
> Something along the lines of ...
>
>
> <?php
> $sth = $dbh->prepare("SELECT name, colour FROM fruit");
> $sth->execute();
>
> /* Fetch all of the remaining rows in the result set */
> print("Fetch all of the remaining rows in the result set:\n");
> $result = $sth->fetchAll();
> print_r($result);
> ?>
>
> for example.
>

--
Catherine Madsen

Programmer Analyst
College of Chemistry
Berkeley, CA 94720-1460

TEL: 510-643-1706
FAX: 510-643-6178


--------------000301000602040506000303--
Catherine Madsen [ Di, 15 Dezember 2009 22:33 ] [ ID #2026166 ]
PHP » gmane.comp.php.general » can't retrieve more than 1 record at the time still

Vorheriges Thema: Highlighting image map on load
Nächstes Thema: strip tags but preserve title attributes