I want to check that URL is already available in table or not before insert using php 5.3.
我想在使用php 5.3插入之前检查表中是否已有URL。
+-------------+-----------+ + 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.
如果我添加abc,它会给我存储在col 2 [surl] xyz中的值。如果不在数据库中添加它。
<?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);
}
?>
3 个解决方案
#1
0
check your both db field type id text or varchar i think tmp is a string valus so you need to match in quote then try:-
检查你的db字段类型id文本或varchar我认为tmp是一个字符串valus所以你需要在引用中匹配然后尝试: -
$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);
#2
0
Select your database before continuing with your query and as @Rakesh said if the db type is text or varchar wrap your values with quotation marks.
在继续查询之前选择数据库,并且@Rakesh说如果db类型是text或varchar用引号包装你的值。
EG:
$dbhost = '****';
$dbuser = '****';
$dbpass = '****';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db('a4806808_my') or die("couldn't select the database"); //select your database before continuing
$query="select * from new where durl = 'tmp'"; //use quotation marks around 'tmp' as rakesh said.
$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")';
$retval = mysql_query( $sql, $conn );
}
mysql_close($conn); //close your connection after everything is done
#3
0
In Mysql,you should quote a string with '' or "" like this:
在Mysql中,你应该用''或“”引用一个字符串,如下所示:
$query="select * from new where durl = 'tmp'";
if tmp is a php variable, you code should like that:
如果tmp是一个php变量,你的代码应该是这样的:
$query="select * from new where durl = '$tmp'";
#1
0
check your both db field type id text or varchar i think tmp is a string valus so you need to match in quote then try:-
检查你的db字段类型id文本或varchar我认为tmp是一个字符串valus所以你需要在引用中匹配然后尝试: -
$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);
#2
0
Select your database before continuing with your query and as @Rakesh said if the db type is text or varchar wrap your values with quotation marks.
在继续查询之前选择数据库,并且@Rakesh说如果db类型是text或varchar用引号包装你的值。
EG:
$dbhost = '****';
$dbuser = '****';
$dbpass = '****';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db('a4806808_my') or die("couldn't select the database"); //select your database before continuing
$query="select * from new where durl = 'tmp'"; //use quotation marks around 'tmp' as rakesh said.
$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")';
$retval = mysql_query( $sql, $conn );
}
mysql_close($conn); //close your connection after everything is done
#3
0
In Mysql,you should quote a string with '' or "" like this:
在Mysql中,你应该用''或“”引用一个字符串,如下所示:
$query="select * from new where durl = 'tmp'";
if tmp is a php variable, you code should like that:
如果tmp是一个php变量,你的代码应该是这样的:
$query="select * from new where durl = '$tmp'";