在 MySQL 中检测到同一行时如何取消操作
How to cancel action when detect same line in MySQL
我需要 PHP 代码来检测 MySQL 数据库 table 中的那一行。
谢谢
根据我对你问题的理解 - 在你的 php 代码中你可以这样做
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sqlSearch = "select everything from <table_name> where <column_name> = <value>;";
$result = mysqli_query($con,$sqlSearch );
if (mysqli_num_rows($result) > 0){
// do nothing
}
else{
$sqlInsert = "insert into <table_name> (column1,column2...) VALUES(value1,value2...);";
$result = mysqli_query($con,$sqlInsert);
}
?>
基本上,您想搜索数据库以查看该行是否存在,如果不存在,您想要执行插入查询。
希望这段代码对你有所帮助。
<?php
$variable1 = 1;
$query = "SELECT * FROM table_name WHERE column_name='$variable1'";
$result = $conn->query($query); //$conn is the connection variable
if($result->num_rows==0){
//insert into table if no duplicate tuple doesnot exist.
$insertquery = "INSERT INTO table_name (column_name) VALUES ('$variable1')";
$insertresult = $conn->query($insertquery);
}else{
//do something
}
?>
我需要 PHP 代码来检测 MySQL 数据库 table 中的那一行。 谢谢
根据我对你问题的理解 - 在你的 php 代码中你可以这样做
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sqlSearch = "select everything from <table_name> where <column_name> = <value>;";
$result = mysqli_query($con,$sqlSearch );
if (mysqli_num_rows($result) > 0){
// do nothing
}
else{
$sqlInsert = "insert into <table_name> (column1,column2...) VALUES(value1,value2...);";
$result = mysqli_query($con,$sqlInsert);
}
?>
基本上,您想搜索数据库以查看该行是否存在,如果不存在,您想要执行插入查询。
希望这段代码对你有所帮助。
<?php
$variable1 = 1;
$query = "SELECT * FROM table_name WHERE column_name='$variable1'";
$result = $conn->query($query); //$conn is the connection variable
if($result->num_rows==0){
//insert into table if no duplicate tuple doesnot exist.
$insertquery = "INSERT INTO table_name (column_name) VALUES ('$variable1')";
$insertresult = $conn->query($insertquery);
}else{
//do something
}
?>