Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2010, 06:11 PM   PM User | #1
Puppet Master
New Coder

 
Join Date: Nov 2010
Location: California
Posts: 42
Thanks: 6
Thanked 2 Times in 2 Posts
Puppet Master is an unknown quantity at this point
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
Puppet Master is offline   Reply With Quote
Old 12-03-2010, 08:50 PM   PM User | #2
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
PHP Code:
if (mysql_query('Some SQL')) {
  echo(
'The record has been added');

__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 12-03-2010, 09:57 PM   PM User | #3
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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.';

__________________
JDub
http://johnnyzone.com/blog

Last edited by JohnDubya; 12-03-2010 at 10:00 PM..
JohnDubya is offline   Reply With Quote
Users who have thanked JohnDubya for this post:
Puppet Master (12-04-2010)
Old 12-03-2010, 10:04 PM   PM User | #4
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
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
Lamped is offline   Reply With Quote
Old 12-04-2010, 07:32 PM   PM User | #5
Puppet Master
New Coder

 
Join Date: Nov 2010
Location: California
Posts: 42
Thanks: 6
Thanked 2 Times in 2 Posts
Puppet Master is an unknown quantity at this point
All the codes worked like a charm. Thanks Guys.

-Puppet Master
Puppet Master is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 PM.


Advertisement
Log in to turn off these ads.