Turn on thread page Beta
    • Thread Starter
    Offline

    0
    ReputationRep:
    I've written a script that inserts a record into a MYSQL database using PHP and an SQL script.

    It works fine but I don't understand this part of the code:

    Code:
    <?php 
    if (mysql_query($query, $dbc)){
    echo "done";
    }
    else {
    echo "fail";
    }
    ?>
    I don't get how the IF statement can test if the operation suceeded or not with just this:

    if (mysql_query($query, $dbc))

    I would have thought it should at read

    if (mysql_query($query, $dbc))==true {
    do stuff
    }
    Offline

    1
    ReputationRep:
    PHP has it's own syntax, and that's just the way it works.

    You want to buy a PHP book off me? PHP4 Bible it's called, and I'll sell it for a good price.
    Offline

    0
    ReputationRep:
    Wish i could help....dont work with that code, sorry
    Offline

    13
    ReputationRep:
    you dont have to have if(something==true), if the something is either true or false, e.g. it would be pointless doing if(true==true), it is the same as if(true)
    Offline

    1
    ReputationRep:
    (Original post by James_W)
    PHP has it's own syntax, and that's just the way it works.

    You want to buy a PHP book off me? PHP4 Bible it's called, and I'll sell it for a good price.
    http://www.amazon.co.uk/exec/obidos/...409541-3250253

    Tell me if you want it.. I'll sell it cheaper than what they want on amazon.co.uk 'used'.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by James_W)
    http://www.amazon.co.uk/exec/obidos/...409541-3250253

    Tell me if you want it.. I'll sell it cheaper than what they want on amazon.co.uk 'used'.
    Hiya, thanks, I think I will leave it because we are switching to PHP5 soon anyway which is object oriented and totaly different syntax.
    Offline

    1
    ReputationRep:
    (Original post by amazingtrade)
    I've written a script that inserts a record into a MYSQL database using PHP and an SQL script.

    It works fine but I don't understand this part of the code:

    Code:
    <?php 
    if (mysql_query($query, $dbc)){
    echo "done";
    }
    else {
    echo "fail";
    }
    ?>
    I don't get how the IF statement can test if the operation suceeded or not with just this:

    if (mysql_query($query, $dbc))

    I would have thought it should at read

    if (mysql_query($query, $dbc))==true {
    do stuff
    }
    PHP isn't a strongly typed language, so the call to mysql_resource returns a resource type on success of some querys, true on some others and false on a failure. In PHP a type such as resource will evaluate to true on a test of it unless it is not set.
    It's a basic property of a if statement that if the condition is true you go down the first branch and if it's false you go down the second. Testing its equality to true just returns another true and is unneeded duplication.

    Alaric.
    Offline

    1
    ReputationRep:
    (Original post by amazingtrade)
    Hiya, thanks, I think I will leave it because we are switching to PHP5 soon anyway which is object oriented and totaly different syntax.
    PHP is already Object Oriented, it's just getting a new Object model (a much better one ). The syntax is going to be almost identical in every other respect though.

    Alaric.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Alaric)
    PHP is already Object Oriented, it's just getting a new Object model (a much better one ). The syntax is going to be almost identical in every other respect though.

    Alaric.
    With PHP5 they are making it more hardcore arn't they? I read they are going to make you have to declare variables and stuff, which can only be a good thing in my opinion.
    Offline

    10
    ReputationRep:
    (Original post by amazingtrade)
    I've written a script that inserts a record into a MYSQL database using PHP and an SQL script.

    It works fine but I don't understand this part of the code:

    Code:
    <?php 
    if (mysql_query($query, $dbc)){
    echo "done";
    }
    else {
    echo "fail";
    }
    ?>
    I don't get how the IF statement can test if the operation suceeded or not with just this:

    if (mysql_query($query, $dbc))

    I would have thought it should at read

    if (mysql_query($query, $dbc))==true {
    do stuff
    }
    No, mysql_query($query, $dbc) in the if statement alone is fine, cos mysql_query returns true if the query was executed successfully. If you wanted to check that the query had not been executed successfully, you'd use if(!mysql_query($query, $dbc) (possibly with an extra set of brackets, can't remember perfectly. The PHP manual is very useful btw (especially the function reference).
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by piginapoke)
    A much neater version of your sig:

    cout << (amazingtrade=="alive")?"Must be on UKL":"Must be dead";
    Thanks I didn't know you could but conditional statements within cout.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Nylex)
    No, mysql_query($query, $dbc) in the if statement alone is fine, cos mysql_query returns true if the query was executed successfully. If you wanted to check that the query had not been executed successfully, you'd use if(!mysql_query($query, $dbc) (possibly with an extra set of brackets, can't remember perfectly. The PHP manual is very useful btw (especially the function reference).
    Thanks for the link, it should be handy.
 
 
 
Poll
Do you think parents should charge rent?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.