Treehouse: Grow your CSS skills. Land your dream job.

simple I don’t know how PHP

  • # June 4, 2012 at 1:28 am

    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.


    # June 4, 2012 at 6:42 pm


    Someone should know this…

    # June 4, 2012 at 9:06 pm

    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 */

    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.

    # June 10, 2012 at 8:57 pm

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

    # June 11, 2012 at 5:55 am
    if(mysql_query($sqlinsert, $link)){
    //Query worked
    } else {
    //Query failed
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.