HabboxWiki needs you!
Are you a Habbo buff? Or maybe a rare trader with a bunch of LTDs? Get involved with HabboxWiki to share your knowledge!
Join our team!
Whether you're raving for rares, excited for events or happy helping, there's something for you! Click here to apply
Need a helping hand?
Check out our guides for all things to help you make friends, make rooms, and make money!


Results 1 to 9 of 9

Thread: PHP Coding Help

  1. #1
    Join Date
    Apr 2008
    Posts
    41
    Tokens
    0

    Default PHP Coding Help

    Ok before i begin, im very new at php and dont flame me if i've done something stupidly wrong...


    Basically, im in charge of the website for new community radio station Bolton FM and need a php script to say the on air DJ (before i go any further, please DO NOT suggest a dj panel to me).

    I want to hook this php script up to a database with the tables "Mon", "Tue", etc with the columns "Showbegin", "Showend" and "Presenter" to pull up the current presenter.

    Here is my code:

    PHP Code:
    <?php 

    $query 
    "SELECT showbegin, showend, presenter 
    FROM date('D') 
    WHERE showbegin < curtime('G:i') 
    AND showend > curtime('G;i')"


    $result mysql_query($query); 
    if (!
    $result) { 
        
    //do error processing (log error, etc) 
        
    echo "query failure!".mysql_error(); 
        die(); 


    $showInfo mysql_fetch_assoc($result); ?>

     
    <div class="show"> 
    <p>Currently airing: <?php echo $showInfo['presenter'?></p>
    (i have my connection info at the top of the page, so no, it isnt missing)

    I get the error
    query failure!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 '() WHERE showbegin < time(G:i) AND showend > time(G:i)' at line 2


    Could anyone please suggest something?
    As i said, please be patient with me, im a bit inexperienced in php...

    Moved by ReviewDude (Forum Moderator) from "Web Design", as this is an issue related to PHP code, not its use within design.
    Last edited by ReviewDude; 25-03-2009 at 08:03 AM.

  2. #2
    Join Date
    Nov 2005
    Location
    C:\Program Files\Lethal\Jak06
    Posts
    74
    Tokens
    0

    Default

    What version of MySQL are you using ?

  3. #3

    Default

    PHP version 5.2.5
    MySQL version 5.0.67-community

  4. #4
    Join Date
    Jul 2008
    Posts
    535
    Tokens
    75

    Default

    PHP Code:
    $query "SELECT showbegin, showend, presenter 
    FROM date('D') 
    WHERE showbegin < curtime('G:i') 
    AND showend > curtime('G;i')"


    $result mysql_query($query); 
    There's no need to make two variables for a mysql_query, you can do it all in one and it gets rid of unneeded code:
    PHP Code:
    $result mysql_query("SELECT * FROM date('D') WHERE showbegin < curtime('G:i'') AND showend > curtime ('G:i')"); 
    -------

    PHP Code:
    if (!$result) { 
        
    //do error processing (log error, etc) 
        
    echo "query failure!".mysql_error(); 
        die(); 

    I don't think your viwers would want to see a mysql error, so just make a stupid phrase saying "There is a problem" or something like that. Why are you using die()? There's no need for it to be there as you're not trying to exit the script. You're also going to want to do an else{} as if you don't even if there is an error in your mysql the page is still going to try to display the currently on air text. Your new code:

    PHP Code:
    if(!$result){  
        echo 
    "onoes! There is a problem!"
    } else { 
    -------

    PHP Code:
    $showInfo mysql_fetch_assoc($result); 
    For what you're doing mysql_fetch_assoc would work, as you haven't given any specific details on what to fetch from the database, just two times.

    -------

    PHP Code:
    <div class="show"> 
    <p>Currently airing: <?php echo $showInfo['presenter'?></p>
    There's nothing wrong with that.

    -------

    So here's your complete new code:

    PHP Code:
    <?php $result mysql_query("SELECT * FROM date('D') WHERE showbegin < curtime('G:i'') AND showend > curtime ('G:i')");

    if(!
    $result){  
        echo 
    "onoes! There is a problem!"
    } else {
        
    $showInfo mysql_fetch_assoc($result);
    ?>
    <div class="show"> 
    <p>Currently airing: <?php echo $showInfo['presenter'?></p>
    <?php ?>
    I haven't tested it but that should work, let me know if you get any errors.
    Last edited by wsg14; 22-03-2009 at 03:20 PM.

  5. #5

    Default

    Thank you very much wsg14 for your detailed explanation, i understand it a bit more now.

    Here is my current code:
    PHP Code:
    <?php 
    $day 
    date('D');
    $result mysql_query("SELECT * FROM `$day` WHERE showbegin < date('G:i') AND showend > date('G:i')");

    if(!
    $result){  
        echo 
    "There has been an error in retrieving the current presenter. "
    } else {
        
    $showInfo mysql_fetch_assoc($result);
    ?>


    <div class="show"> 
    <p>Currently airing: <?php echo $showInfo['presenter'?></p>
    <?php ?>
    I had to add the date into the $day variable to get it to work, and change curtime to date(G:i)



    At the moment it does not display the presenter. All i get back is:
    Currently airing: (blank space)

    Would this have something to do with my database?

    At the moment i have 7 tables each named Mon Tue Wed Thu Fri Sat & Sun
    Each table has 6 collumns:

    ID (int,auto_increment_
    showbegin (time)
    showend (time)
    presenter (text)
    photo (text)
    profile (text)

    With photo and profile being added into the script later on...

  6. #6
    Join Date
    Jul 2008
    Posts
    535
    Tokens
    75

    Default

    Get rid of: AND showend > date('G:i') in your mysql_query.

  7. #7

    Default

    I've got it working now!

    I figured out i had to use curtime() instead of date(G:i) and it works perfectly now.

    Here is my final code:
    PHP Code:
    <?php 
    $day 
    date('D');
    $result mysql_query("SELECT * FROM `$day` WHERE showbegin < curtime() AND showend > curtime()");

    if(!
    $result){  
        echo 
    "There has been an error in retrieving the current presenter. "
    } else {
        
    $showInfo mysql_fetch_assoc($result);
    ?>


    <div class="show"> 
    <p>Current Presenter: <?php echo $showInfo['presenter'?></p>
    <?php ?>
    </div>

    Thanks for all your help!

  8. #8
    Join Date
    Jul 2008
    Posts
    535
    Tokens
    75

    Default

    lad you figured it out.

  9. #9

    Default

    Quote Originally Posted by wsg14 View Post
    lad you figured it out.
    I did

    Ive just tired adding a different entry in the database for 18:50 and it changed it perfectly.

    I think im going to have to get Bolton FM to send me on a php course -facepalm- haha


    Thanks again.

Posting Permissions

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