SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why working in console but not script

    why would the following work in my Console (CocoaMYSQL) and not work in a script

    query statement:
    ********************************
    SELECT * FROM ads WHERE categoriesid = 7 AND enddate >= CURRENT_TIMESTAMP ORDER BY RAND() LIMIT 5

    seems my script does not like the "CURRENT_TIMESTAMP" seeing as it will not echo it when I try

    Funnything is, the minute I switch the ">=" to "<=" then it works (with the wrong results of course)

    I am using php 4.1 i believe (if that makes a difference)

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe try =>

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that came up with a mysql error....

    Warning: mysql_query(): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=> CURRENT_TIMESTAMP ORDER BY RAND() LIMIT 5' at line 1

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't see anything wrong with your SQL (especially if it works via your console); post your code, I'll bet there's some minor error there. Also, please elaborate on "doesn't work" - do you mean that you get unexpected results? No results? PHP error? Something else entirely?
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well...the "doesn't work" refers to no records found. Though I know for a fact there are records, as I have tested it via the console, and physically checked.

    Anyway, here is the code:
    *****************************
    $selected = mysql_select_db(adnetwork, $myc);$result = mysql_query("SELECT * FROM ads WHERE categoriesid = 7 AND adnum != '' AND enddate <= CURRENT_TIMESTAMP ORDER BY RAND() LIMIT 5");

    while ($row = mysql_fetch_assoc($result)) {
    $folder = date('Ymd', mysqltotime($row['uploaddate']));

    //For debugging
    echo CURRENT_TIMESTAMP;
    echo "enddate = " .$row["enddate"];

    ?>
    <tr>
    <td><center><a href="http://mydomain.com/file.php?adname=<?php echo $row["adname"] ?>&adsid=<?php echo $row["adsid"] ?>&ad=/adnetwork/<?php echo $folder . "/" .$row["adsid"] . "/" . $row["filename"] ?>"><?php echo $row["adname"] ?><img src ="http://mydomain.com/file2.php?adsid=<?php echo $row["adsid"] ?>&adsize=thumb" BORDER=0></a></center>
    </td>
    </tr><?php
    }
    ?>

  6. #6
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    CURRENT_TIMESTAMP is a MySQL function, which is why you're not seeing PHP echo it as you would otherwise expect - PHP just simply doesn't understand it. I think I see your problem; change your code to look like this:
    PHP Code:
    $selected mysql_select_db("adnetwork"$myc) or die("Unable to select database: ".mysql_error()); //note I added quotes around the DB name
    $result mysql_query("SELECT * FROM ads WHERE categoriesid = 7 AND adnum != '' AND enddate <= CURRENT_TIMESTAMP ORDER BY RAND() LIMIT 5") or die("Query failed: ".mysql_error());

    //continue with your current code 
    mysql_select_db was most likely not selecting your database because you weren't giving PHP a string - you were trying to pass it a constant. Incidentally, turning on error reporting would have pointed right here. I also added "or die(..." statements to each of your MySQL functions to help in further debugging in case this doesn't solve the problem.
    PHP questions? RTFM
    MySQL questions? RTFM

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind....sorry everyone....just realised I was testing the script on my testing server, which does not include my correct connection doc.

    sorry everyone. thank you to kromey and retox

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heh, no problem, i do that all the time

  9. #9
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh, been there, done that, my wall has the nice head-shaped hole to prove it!
    PHP questions? RTFM
    MySQL questions? RTFM


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •