Why do these two sets of php code affect each other?

Hey everyone,

I am trying to work out why the two sets of code below affect each other…

Take a look at this page:

http://www.glofamily.com/inner/index.php

You will see a “Latest News” and “Next Events” column. The Next Events shows an error, the Latest News is fine. But the weird thing is that if i take out the Latest News code (Taken from a WP Blog) the Next Events code works and shows that i want it to show…

Latest News


					<?php
				        require('blog/wp-blog-header.php');
				
				        $iNumberOfPosts = 0;
				        while ($iNumberOfPosts < 1 && have_posts()) : the_post('showposts=1'); ?>
				
				        <h4 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h4>
					    <p><em><?php the_time('j F Y') ?></em></p>
				        <p style="margin-top:5px"><?php content('60'); ?></p>
				        <?php $iNumberOfPosts++; ?>
				     	<?php endwhile; ?>

Next Events


			        <?
			        $event = Event::selectNextOnStage();
			        $i = 0;
			        while($row = mysql_fetch_array($event)){
			            $date = $row['date'];
			            $date = strtotime($date);
			            $date = date("F jS Y",$date);
			                ?>
							<div class="stories" style="margin-top:0px">        
							<p style="float:right"><em><?=$date?></em></p> 
							<h4><?=$row['title']?></h4>  
							<img src="http://<?=$_SERVER['SERVER_NAME'].$sitename?>inner/eventimages/<?echo $row['image']?>" alt="" style="width:165px; height:100px; border: 1px solid #c84ae6"/>
							<p><?=substr($row['body'], 0, 80);?>...</p>     
			                <a href="http://<?=$_SERVER['SERVER_NAME'].$sitename?>inner/events/?ID=<?echo $row['ID']?>"><img src="images/read-more.png" border="0" style="margin-top:-10px"/></a>
			                </div>
			                <div class="clear"></div>
			                <?  
			        }
			        ?>	

Why do i get the error as you will notice on the page i have provided:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /domains/glofamily.com/http/inner/index.php on line 75

Refers to this line:


while($row = mysql_fetch_array($event)){

I know the code actually works because if i take out the Latest News code everything is fine…

Any ideas why this is?

Thanks

We can’t know without seeing the contents of the Event::selectNextOnStage() function.

Sorry :confused:

The function is shown below:



    public function selectNextOnStage(){

        $sql = "SELECT *
                FROM c2_innerglo.tbl_events e
                WHERE DAY(date) >= DAY(CURRENT_DATE)
                AND archive = 0 AND deleted = 0
                ORDER BY date ASC
                LIMIT 2";
        $result = mysql_query($sql) or mysql_error();
        return $result;
    }


Have you tried printing out $event when you get the error message. If there is a SQL error in selectNextOnStage(), $event will be the error message, not a valid MySQL resource. Seems like that is what is happening, though I don’t know how it’s related to the Latest News section.

Edit: actually… if there were an error, wouldn’t the or operator make $event TRUE instead of the error message? Because FALSE or “error message” is TRUE.

well i just tried doing that and this is the SQL that is printed…

SELECT * FROM c2_innerglo.tbl_events e WHERE DAY(date) >= DAY(CURRENT_DATE) AND archive = 0 AND deleted = 0 ORDER BY date ASC LIMIT 2

Am i missing something?

Yeah, I meant print the error message if there is one, not the SQL.

    public function selectNextOnStage(){
        $sql = "SELECT *
                FROM c2_innerglo.tbl_events e
                WHERE DAY(date) >= DAY(CURRENT_DATE)
                AND archive = 0 AND deleted = 0
                ORDER BY date ASC
                LIMIT 2";
        $result = mysql_query($sql);
        if(!$result)
                print mysql_error();  // for the sake of testing
        return $result;
    }

lol ah sorry,

Well this is the error message:

SELECT command denied to user ‘c2_inner’@‘www.sonassi.com’ for table ‘tbl_events’

What does this mean? I don’t understand, when i dont include the WP blog code i don’t get any errors, it’s really weird :rolleyes:

Any ideas what the error means?

It’s a problem with permissions. The error message means the user c2_inner doesn’t have access to tbl_events in the MySQL database. You’ll have to look at your database and fix the permissions.

Ok you will not believe what the problem was!

I managed to get it working…

Basically the blog code as shown in the first post had this line of code:


require('blog/wp-blog-header.php');

This included the connection string for the blog on the website which is on a separate database. Now the Events code comes after this, so it was overriding the connection for the Events database, and as the blog database does not have an events table it was showing an error!

So basically a school boy error :smiley:

Thanks for helping me out, your suggestions led to the answer in the end :slight_smile:

Thanks again

And this, my friends, is why global variables are bad.

Glad you got it working, Billy.