Sunday, 18 August 2013

Check Data in MySQL row before Insert using php 5.3

Check Data in MySQL row before Insert using php 5.3

I want to check that URL is already available in table or not before
insert using php 5.3.
+-------------+-----------+
+ durl + surl +
+-------------+-----------+
+ abc + xyz +
+ mno + pqr +
+ efg + jkl +
+-------------+-----------+
if i am add abc it give me value stored in col 2[surl] xyz.
and if not add this in database.
<?php
$dbhost = '****';
$dbuser = '****';
$dbpass = '****';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$query="select * from new where durl = tmp";
$result = mysql_query($query);
if (mysql_num_rows($result)>0) {
// return value of col 2
}
else {
$sql = 'INSERT INTO new '.
'(durl,surl) '.
'VALUES ( "tmp", "XYZ")';
mysql_select_db('a4806808_my');
$retval = mysql_query( $sql, $conn );
mysql_close($conn);
}
?>

No comments:

Post a Comment