Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    California
    Posts
    42
    Thanks
    6
    Thanked 2 Times in 2 Posts

    If statement to verify MySQL Record

    The PHP script I write sends the data imputed from a form to a MySQL databse via the form post method.

    I need an 'if' statement to verify and echo text saying 'The record has been added,'

    Is it possible?

    -Puppet Master

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    if (mysql_query('Some SQL')) {
      echo(
    'The record has been added');

    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Lamped's solution will work great, but alternatively, especially if you are querying with a SELECT statement, you can assign the MySQL query to a variable, so you can use it later on in the script as well:

    PHP Code:
    //put query result into variable
    $x mysql_query('Some SQL');

    if (
    $x) {
       echo 
    'The query succeeded.';
       
    //get result data, etc.
       
    $y mysql_fetch_assoc($x);
       echo 
    $y['field_name'];
    } else {
       echo 
    'Query failed.';

    Last edited by JohnDubya; 12-03-2010 at 11:00 PM.

  • Users who have thanked JohnDubya for this post:

    Puppet Master (12-04-2010)

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    To follow the theme of adapting code, it's fairly common to put the $x = something() actually *in* the if statement, like so:

    PHP Code:
    if ($x mysql_query('Some SQL')) {
       echo 
    'The query succeeded.';
       
    //get result data, etc.
       
    $y mysql_fetch_assoc($x);
       echo 
    $y['field_name'];
    } else {
       echo 
    'Query failed.';

    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    New Coder
    Join Date
    Nov 2010
    Location
    California
    Posts
    42
    Thanks
    6
    Thanked 2 Times in 2 Posts
    All the codes worked like a charm. Thanks Guys.

    -Puppet Master


  •  

    Posting Permissions

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