treehouse : what would you like to learn today?
Web Design Web Development iOS Development

simple I don't know how PHP

  • Hi there,

    How would you do a callback function in PHP? I know this is simple but I can't seem to figure it out.

    mysql_query($sqlinsert, $link);

    /*begin pseudocode

    if(success that that query was inserted):
    (
    print "Yay, it worked!";
    )

    end pseudocode*/


    Something like that.

    Red
  • bump

    Someone should know this...
  • you can use callbacks (syntactically, similar to the way you do in javascript) in PHP 5.3+.

    In earlier versions, depending on the specific goal, you might find call_user_func() or call_user_func_array() helpful. Certain functions include a callback parameter, as well (again, depends on what you're doing).

    However, for what you're doing, there's no need.
    Your "psuedocode" is very close to actual code: it's a "conditional" code block, not a "callback."
    <?php

    $result = mysqli_query( "INSERT whatever",$link );
    if( mysqli_affected_rows( $link ) ){
    /* do something */
    }


    edit:
    Also, you may notice that I used mysqli_* functions instead of mysql_*. ext/mysql is outdated; if you have any choice in the matter, you should not be using it. ext/mysqli or PDO are the recommended APIs for mysql.


  • <?php
    mysql_query($sqlinsert, $link) or die("now i go die.");
    echo "heyyy i am still alive let's do the next thing..";
    ?>

  • if(mysql_query($sqlinsert, $link)){
    //Query worked
    } else {
    //Query failed
    }