Reputation: 9221
I insert some data from a php foreach. And when the data insert, I make sure if the data has already existed in 2 tables? if not, insert, else jump.
But in this process, how can I know each item data has been inserted or no?
{
//some foreach process get $title, $name, $date
mysql_query("INSERT INTO table1 (title,name,date) SELECT '".$title."','".$name."','".$date."' FROM dual WHERE not exists (SELECT title FROM table1 WHERE table1.title = '".$title."') AND NOT EXISTS (SELECT title FROM table2 WHERE table2.title = '".$title."')");
//I want to add some judge like this:
//if this item insert {
//do some stuff;
//} else {
//do some stuff;
//}
}
Upvotes: 1
Views: 3367
Reputation: 188004
From http://php.net/function.mysql-query:
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
Upvotes: 6
Reputation: 6052
This might help
If (mysql_query($sql)){
//Success
}else{
//Failure
echo mysql_error(); //Reason
}
Upvotes: 1
Reputation: 31
mysql_query always returns a value. It returns a TRUE when INSERT is successful. See code below. Hope this helps. :)
$result=mysql_query($sql);
// SUCCESSFUL
if($result){
echo "Successful";
}
// NOT SUCCESSFUL
else {
echo "Not inserted";
}
Upvotes: 3