0

I've read the other questions. The error has been posted before ofcourse. It's a common error. How/Why I am getting the errror is unique. Not a duplicate.

I know this error message has been posted on before but not with my specific set of complications. I have recently put a site together on my local server using xammp. Everything worked properly before on my local machine before i uploaded. I have encountered at-least 8 new problems after uploading to my web host but have been able to fix each one. 3 of which with the help of this site. I believe I will have it completed after this error is fixed.

The issue is with my advert rotating system. It's a very simple script/system. There are 4 database for the 4 separate places on the page i have the rotating adverts. this is the second one i am attempting to put on the live site. I've already got the first one working with no error. The error on this one is on line 5

Any ideas? As always thanks a bunch!

Full Error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/xxxxxx/public_html/includes/adverts/sidead1.php on line 5

Part of the code below:

<?php
    include '../includes/core/db/sidead1_dbinfo.php';

    $ads = mysql_query("SELECT `advert_id1`, `title1`, `desc1`, `image1` FROM `sidead1` WHERE UNIX_TIMESTAMP() < `expires1` AND `shown1`=0 ORDER BY `advert_id1` ASC LIMIT 1");
    while ($ads_row = mysql_fetch_assoc($ads)) {
        $advert_id1 = $ads_row['advert_id1'];
        $image1 = $ads_row['image1'];
        $title1 = $ads_row['title1'];
        $desc1 = $ads_row['desc1'];

        mysql_query("UPDATE `sidead1` SET `shown1`=1, `impressions1`=`impressions1`+1 WHERE `advert_id1`=$advert_id1");

        $shown1 = mysql_query("SELECT COUNT('advert_id1') FROM `sidead1` WHERE `shown1`=0");
        if (mysql_result($shown1, 0) == 0) {
            mysql_query("UPDATE `sidead1` SET `shown1`=0");
        }
    }

    ?>
user2526699
  • 177
  • 2
  • 8
  • 19
  • 1
    See [this answer](http://stackoverflow.com/a/11674313/250259) for how to troubleshoot this. – John Conde Jul 08 '13 at 02:13
  • I looked at the answer but I do not understand it, sorry. Could you be more specific on how I should implement that? I should say rather, I am really new this all of this. – user2526699 Jul 08 '13 at 02:16
  • I see this type of question all the time, you should read [common database debugging for PHP and MySQL](http://jason.pureconcepts.net/2013/04/common-debugging-php-mysql/). – Jason McCreary Jul 08 '13 at 02:19
  • I've read a ton of documentation before coming here to ask. I am not sure how/where to implement it tho. – user2526699 Jul 08 '13 at 02:22

2 Answers2

1

First off use PDO! check if you are getting something in return from the SELECT statement here is how if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; }

David Strada
  • 155
  • 11
  • If you do not mind showing me with my code, where/how would i run this with the code I have from above? – user2526699 Jul 08 '13 at 02:18
  • After you define the varialbe `$ads` before the while statement check if you are getting any result usin this `if (mysql_num_rows($ads) == 0) { echo "No rows found, nothing to print so am exiting"; exit; }` – David Strada Jul 08 '13 at 02:23
  • Go ahead and stop using this script look for PDO here http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/ – David Strada Jul 08 '13 at 02:24
  • Once i put in your ifk statement I get the following error Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/ncgotggb/public_html/includes/adverts/sidead1.php on line 5 No rows found, nothing to print so am exiting – user2526699 Jul 08 '13 at 02:25
  • also here http://php.net/manual/en/book.pdo.php Let's say for right now this is the 'best' way to doit, the way you are doing this is ancient, deprecated! Don't doit this way. – David Strada Jul 08 '13 at 02:26
  • Meaning you are not getting information from the database – David Strada Jul 08 '13 at 02:26
  • The first resource i gave you describes why you shouldn't use this script. – David Strada Jul 08 '13 at 02:29
  • lol, I am not familiar enough with PHP to REDO this entire thing. I'd like it to stay simple. Atleast till i get the site off the ground already put to much time into it. I;m sure it's something small im looking over. As i said the first one I used is working. And it's the exact same excpet that i added the value 1 to the endof each field in the database. I copied over the sidead php to sidead1 and the error went away. Theres got to be something im missing. I appreiacte that but I am going to use the script. I need to konw how to fix it. Not implement an entirely new one. I dont have the time – user2526699 Jul 08 '13 at 02:29
  • Check your select statement on phpmyadmin and see if it returns something. Thats the problem you are not fetching data from the database hence can't do the while loop – David Strada Jul 08 '13 at 02:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33058/discussion-between-user2526699-and-idavid) – user2526699 Jul 08 '13 at 02:32
0

You aren't checking the return value of your query here:

$ads = mysql_query("SELECT `advert_id1`, `title1`, `desc1`, `image1` FROM `sidead1` WHERE UNIX_TIMESTAMP() < `expires1` AND `shown1`=0 ORDER BY `advert_id1` ASC LIMIT 1");

I'd guess you have a syntax error appearing at UNIX_TIMESTAMP() <expires1``. You can find out like this:

if (($ads = mysql_query("...your query...")) == false) {
   die("SQL Error:".mysql_error);
}

Always check the return status of your query.

Oh - mysql is deprecated. Use mysqli or PDO for new projects. And old ones!

  • thanks, were would i put your code in at though? sorry, as i told them as well I am new-ish to this and am not sure where to put this in my code. – user2526699 Jul 08 '13 at 02:21
  • Replace the fourth line ($ads = ...) with the snippet I posted, after editing in your query. You also have an `UPDATE` query further down you should check the return status of. –  Jul 08 '13 at 02:24
  • I put your code in and get this error SQL Error:mysql_error – user2526699 Jul 08 '13 at 02:27
  • Have you copied this `die("SQL Error:".mysql_error);` exactly? –  Jul 08 '13 at 02:40
  • Where do i paste the above in at? – user2526699 Jul 08 '13 at 02:43
  • I assume you can interpret simple instructions and make basic edits accurately. If you can't then I can't really help further. –  Jul 08 '13 at 02:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33059/discussion-between-user2526699-and-mike-w) – user2526699 Jul 08 '13 at 03:00
  • When i put that in i get the error SQL Error:mysql_error – user2526699 Jul 08 '13 at 03:08