[PHPwestoz] problem with includes

Keith Moss khan at swcombine.com
Sat Feb 27 00:37:23 EST 2010


I think you may need to change

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

to

$result = $conn->query($sql)->fetchAll();
foreach($result as $row) {

On 23 February 2010 16:05, Gnitnop G <gnitnop at ymail.com> wrote:

> Oops! I should have said I'm using mysql PDOs.
> anyways after a read thru the manual heres what I found.
>
> "$conn" returns a PDO object
> "$conn->query($sql)" returns a PDOStatement Object
> "$query($sql)" returns an array
>
> since the last line returns an array the $conn object should be iterable.
>
> Moreover, I have also tested it without a database service for A.php. I
> removed all db handling code from A.php and am able to login. I have no
> problem. thats because the dbconnect object is already created by login2.php
> and its just once. So I am able to login because A.php doesnt include
> dbconnect.php(just for the test). The problem kicks in when A.php locally
> includes dbconnect.php. Thats when there is a confiict of object creation.
> first login2.php creates the object (obviously by doing a   $conn =
> dbConnect('query'); new PDO blah blah..) and then the local script A.php has
> its own include to dbconnect.php.
>
> So I can say after testing that the code is fine when the includes are
> switched off.
>
> I also tried persistent connections by adding another argument in the new
> PDO constructor
> array(PDO::ATTR_PERSISTENT => true); and then making all $conn=null on
> completion of each local script, but that didnt seem to work
> either....probably I was missing something else, but I cut the research
> since I thought I was going away in a tangent.
>
> have anyone struck gold yet?
>
> thanks
> greymatter
>
>
> --- On *Mon, 22/2/10, Kim Richardson <krichardson at viewdale.com.au>* wrote:
>
>
> From: Kim Richardson <krichardson at viewdale.com.au>
>
> Subject: Re: [PHPwestoz] problem with includes
> To: phpwestoz at lists.linux.org.au
> Date: Monday, 22 February, 2010, 15:27
>
>
> The output of $conn->query($sql) would be a result set pointer, not an
> actual array.
>
> Try
>
> $result=$conn->query($sql);
> if($result->num_rows>0)
> {
>    while($row=$result->fetch_assoc())
>    {
>            ... do your magic here
>    }
> }
>
> On 22/02/2010 9:58 PM, Gnitnop G wrote:
>
>   30     <div id="artdescription">
>  <?php
>  foreach ($conn->query($sql) as $row) {
>  ?>
>         <h2><?php echo $row['title']; ?></h2>
>           <p><?php $extract = getFirst($row['article']);
>             echo $extract[0];
>             if ($extract[1]) {
>
>               echo '<a
> href="abstractexplanation.php?article_id='.$row['article_id'].'"> More</a>';
>               } ?></p>
>       <?php } ?>
> 41    </div>
>
>
> ps: according to my sketched-out example, that last error should look like
> this:
>
> [Mon Feb 22 12:26:49 2010] [error] [client 127.0.0.1] PHP Warning:  Invalid
> argument supplied for foreach() in C:\\Program Files\\Apache
> Group\\Apache2\\htdocs\\xxxxxxxx\\B.php on line 31, referer:
> http://localhost/xxxxxxxx/B.php
>
>
>
> --- On *Mon, 22/2/10, Keith Moss <khan at swcombine.com><http://uk.mc246.mail.yahoo.com/mc/compose?to=khan@swcombine.com>
> * wrote:
>
>
> From: Keith Moss <khan at swcombine.com><http://uk.mc246.mail.yahoo.com/mc/compose?to=khan@swcombine.com>
> Subject: Re: [PHPwestoz] problem with includes
> To: "PHP users in Western Australia (or nearby)"
> <phpwestoz at lists.linux.org.au><http://uk.mc246.mail.yahoo.com/mc/compose?to=phpwestoz@lists.linux.org.au>
> Date: Monday, 22 February, 2010, 11:32
>
> Ah I see it's time once again for the annual post to the mailing list :)
>
> include_once should indeed work the situation you describe.
>
> What was the code around line 31 of journal_pdo.php that was throwing the
> foreach error?
>
> On 22 February 2010 13:30, Gnitnop G <gnitnop at ymail.com<http://uk.mc246.mail.yahoo.com/mc/compose?to=gnitnop@ymail.com>
> > wrote:
>
>>
>> Hi All!
>>
>> I seem to be having a problem with php includes. sketching out my problem
>> with an example will look something like this:
>>
>> I have 3 main php pages A.php, B.php and C.php.
>> I have 3 include files; (1) dbconnect.php, (2) login1.php (3) login2.php
>>
>> All main pages; A.php, B.php and C.php includes (1) & (2) in their own
>> page
>> login1.php includes (3)
>> login2.php AGAIN includes (1) - (for its local database service)
>>
>> Basically, in a nutshell, what I'm trying to achieve here is for a user to
>> be able to login from any main page; i.e. A,B or C; and login1.php has the
>> form that gets included in these pages, apart from these pages already
>> including dbconnect.php.
>> login1.php then includes login2.php which also includes dbconnect.php for
>> its own use.
>>
>> So the problem I'm having here is I get the following error when I try to
>> login from any of the 3 main pages:
>>
>> [Mon Feb 22 12:06:40 2010] [error] [client 127.0.0.1] PHP Fatal error:
>> Cannot redeclare dbconnect() (previously declared in C:\\Program
>> Files\\Apache Group\\Apache2\\htdocs\\xxxxxxx\\includes\\dbconnect.php:2) in
>> C:\\Program Files\\Apache
>> Group\\Apache2\\htdocs\\xxxxxxx\\includes\\dbconnect.php on line 22,
>> referer: http://localhost/xxxxxxx/A.php
>>
>> I get the same error when clicking from AB or C. Only the referer page in
>> the trace changes and the rest are all the same each time.
>>
>> Then, I tried include_once for all .php's but then I run into empty
>> for-each'es as one below:
>>
>> [Mon Feb 22 12:26:49 2010] [error] [client 127.0.0.1] PHP Warning:
>> Invalid argument supplied for foreach() in C:\\Program Files\\Apache
>> Group\\Apache2\\htdocs\\kookaburraartgallery\\journal_pdo.php on line 31,
>> referer: http://localhost/xxxxxxxx/B.php
>>
>> Can someone help me with this maze, plz.
>>
>> thanks
>> greymatter
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> PHPwestoz mailing list
>> PHPwestoz at lists.linux.org.au<http://uk.mc246.mail.yahoo.com/mc/compose?to=PHPwestoz@lists.linux.org.au>
>> http://lists.linux.org.au/listinfo/phpwestoz
>>
>>
>
>
> --
> Cheers,
> Keith
> Australia is merely an island of Antarctica, and of no further
> significance.
>
> -----Inline Attachment Follows-----
>
> _______________________________________________
> PHPwestoz mailing list
> PHPwestoz at lists.linux.org.au<http://uk.mc246.mail.yahoo.com/mc/compose?to=PHPwestoz@lists.linux.org.au>
> http://lists.linux.org.au/listinfo/phpwestoz
>
>
>
> _______________________________________________
> PHPwestoz mailing listPHPwestoz at lists.linux.org.au <http://uk.mc246.mail.yahoo.com/mc/compose?to=PHPwestoz@lists.linux.org.au>http://lists.linux.org.au/listinfo/phpwestoz
>
>
> --
> ---------------
> Kim Richardson
> Web Applications Developer
> Viewdale IT
> Tel: 1300 799 516 - Fax: (07) 3319 8932http://www.ausnix.com.au
>
>
> -----Inline Attachment Follows-----
>
> _______________________________________________
> PHPwestoz mailing list
> PHPwestoz at lists.linux.org.au<http://uk.mc246.mail.yahoo.com/mc/compose?to=PHPwestoz@lists.linux.org.au>
> http://lists.linux.org.au/listinfo/phpwestoz
>
>
>
> _______________________________________________
> PHPwestoz mailing list
> PHPwestoz at lists.linux.org.au
> http://lists.linux.org.au/listinfo/phpwestoz
>
>


-- 
Cheers,
Keith
Australia is merely an island of Antarctica, and of no further significance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux.org.au/pipermail/phpwestoz/attachments/20100226/53c9127b/attachment.htm 


More information about the PHPwestoz mailing list