...

View Full Version : If statement to verify MySQL Record



Puppet Master
12-03-2010, 07:11 PM
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

Lamped
12-03-2010, 09:50 PM
if (mysql_query('Some SQL')) {
echo('The record has been added');
}

JohnDubya
12-03-2010, 10:57 PM
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:



//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.';
}

Lamped
12-03-2010, 11:04 PM
To follow the theme of adapting code, it's fairly common to put the $x = something() actually *in* the if statement, like so:


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.';
}

Puppet Master
12-04-2010, 08:32 PM
All the codes worked like a charm. Thanks Guys.

-Puppet Master



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum