This question already has answers here:
Possible return values for mysql_affected_rows()
(2 answers)
Closed 9 years ago.
Can anyone please tell me what are all the possibilities of return values for #mysql_affected_rows. Because i am checking if(#mysql_affected_rows()). In most case it returns 1(sucess) or 0(not sucess). So it worked correctly. But in some cases it returns some value other than 0 if not success on updation.
I dont know what it returns. But it come into the loop. Inside loop i am trying to insert the data. But it shows duplication error on SQL for that. For your reference check the below code please,
$sqlU = sprintf("UPDATE %s SET count = count + 1
WHERE id = %d", 'table', 123);
mysql_query($sqlU);
if(!#mysql_affected_rows()) {
$sqlI = sprintf("INSERT INTO %s (id) VALUES (%d)",
'table', 123);
mysql_query($sqlI);
}
First, the mysql_* functions are deprecated. mysql_affected_rows() seems to return an integer in all cases. It returns -1 if the query failed.
What you really need instead of mysql_affected_rows() is a proper query that is not open to race conditions.
INSERT INTO table (id, count) VALUES (123, 0) ON DUPLICATE KEY UPDATE count = count + 1
Related
This question already has answers here:
How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?
(7 answers)
Postgres on conflict do update on composite primary keys
(2 answers)
Closed 6 months ago.
I have a problem with my SQL statement written in PHP.
I am using store() function to both INSERT or UPDATE object in the database following it's rule that it can be done:
The INSERT OR UPDATE command is an extension of the INSERT command, with these differences:
If the row being inserted does not exist, INSERT OR UPDATE performs an
INSERT operation. If the row being inserted already exists, INSERT OR
UPDATE performs an UPDATE operation, updating the row with the
specified column values.
I wrote type declaration for the Content ID like : public ?$contentId = null; so I can use the same methods for both actions.
public function store(Content $content, int $teamId)
{
$rsm = new ResultSetMapping($this->entityServiceRepository);
$stmt = $this->entityServiceRepository->getConnection()->prepare(
'UPDATE content
SET
name = :name,
url = :url,
WHERE id = :contentId
AND team_id = :teamId
',
$rsm
);
$stmt->bindValue("contentId", $content->getId(), PDO::PARAM_INT);
$stmt->bindValue("teamId", $content, PDO::PARAM_INT);
$stmt->bindValue("name", $content->getName(), PDO::PARAM_STR);
$stmt->bindValue("url", $content->getUrl(), PDO::PARAM_STR);
$stmt->executeQuery();
However from some reason data does not exist in the database. All bindValue() return true ant other sql queries seems to work well. How can I debug this query? Or can someone tell is there a problem with sql query I wrote.
Thanks.
This question already has answers here:
PDO Parameterized Query - Reuse named placeholders?
(5 answers)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 3 years ago.
I'm trying to write a query to insert/update a table and it's my first time using prepared statements, I'm receiving the error SQLSTATE[HY093]: Invalid parameter number but from what I can see I'm passing all the columns/values required.
(I'm trying to do this without using bindParam as in example #2 here)
This is just a test for now, I plan on making it dynamic once I've actually got a query working.
$data_test = [
':event_id' => 3354,
':event' => 'TESTESTEST',
':staff_booking_id' => 27255,
':is_read' => 'yes',
':priority' => 'medium'
];
$q = "INSERT INTO events(event_id, event, staff_booking_id, is_read, priority)
VALUES(:event_id, :event, :staff_booking_id, :is_read, :priority)
ON DUPLICATE KEY UPDATE event_id = LAST_INSERT_ID(:event_id), event = :event, staff_booking_id = :staff_booking_id, is_read = :is_read, priority = :priority;";
$result = $this->db->prepare($q);
$result = $result->execute($data_test);
As commentented by FunkFortyNiner and tadman, it is possible that the issue comes from the fact that you are reusing the same placeholder.
Actually the MySQL syntax does not require you to reuse the named parameter: you can use the VALUES() to refer to the values initially passed for INSERT.
Also, your attempt to update event_id using LAST_INSERT_ID() does not seem right; I am unsure that this is valid syntax - and anyway, if this is the primary key of table, then you don't want to update it.
Finally, as pinpointed by FunkFortyNiner, event is a reserved word in MySQL, so it needs to be quoted.
$q =
"INSERT INTO events(
event_id,
`event`,
staff_booking_id,
is_read,
priority
)
VALUES(
:event_id,
:event,
:staff_booking_id,
:is_read,
:priority
)
ON DUPLICATE KEY UPDATE
`event` = VALUES(`event`),
staff_booking_id = VALUES(staff_booking_id),
is_read = VALUES(is_read),
priority = VALUES(priority)";
This question already has an answer here:
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given [duplicate]
(1 answer)
Closed 1 year ago.
I am trying to insert a new game and an image into my database and I have the following code:
$insert_query = "INSERT INTO Games (name, release_date, description, developer, publisher)
VALUES ('$name', '$release_date', '$description', '$developer', '$publisher')";
$insert = mysqli_query($mysql,$insert_query);
if (!$insert)
{
echo("Error description: " . mysqli_error($mysql));
}
$row = mysqli_fetch_assoc($insert);
$id = $row['id'];
echo $id;
$newImageName = "games".$id.".".$imageExt2;
$imageDestination = 'GamePictures/'.$newImageName;
move_uploaded_file($imageTmpName, $imageDestination);
$update_query = "UPDATE Games SET image = '$newImageName' WHERE name = '$name'";
The row gets inserted in the database, but I still get the error and it doesn't print an error message.
Because of this error I also get a move_uploaded_file: failed error.
Does anyone know how I can fix this?
You can't fetch data from an INSERT query; it doesn't provide a result set, only a true/false success indication (see the manual or #BillKarwin answer). To achieve what (I presume) you are trying to do (fetch the last insert id) just use mysqli_insert_id:
$id = mysqli_insert_id($mysql);
Read the documentation: https://www.php.net/manual/en/mysqli.query.php
For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
Since your SQL is INSERT, and INSERT does not return a result, this line will return TRUE.
$insert = mysqli_query($mysql,$insert_query);
The boolean value TRUE is not a mysqli_result object, so you can't use it as the argument to a function that needs a mysqli_result.
It makes no sense to try to fetch results from an INSERT in MySQL.
This question already has answers here:
Find out if REPLACE statement has replaced or just inserted in MySQL
(3 answers)
Closed 4 years ago.
if I have a query with field 1 being a primary key:
$rep = "Replace into table (field1,field2) values ('value1','value2')";
$stmt = $db->query($rep);
Is there a way to tell if mysql inserted the row, or found and replaced the row?
For Posterity:
$rowCount = $stmt->rowCount();
if $rowCount == 1 it was an insert, if $rowCount == 2, it was a replace.
INSERT INTO AggregatedData (datenum,Timestamp)
VALUES ("734152.979166667","2010-01-14 23:30:00.000")
ON DUPLICATE KEY UPDATE
Timestamp=VALUES(Timestamp)
To achieve this type of task mysql provide us DUPLICATE KEY UPDATE.
Below is the example how you will create new record if record is not exists in database otherwise it will update record
$rep = "INSERT into table (primaryField,field2) values ('value1','value2') ON DUPLICATE KEY UPDATE primaryField=VALUES(primaryField)";
$stmt = $db->query($rep);
For more detail you can read this
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
I think this will help you.
This question already has an answer here:
MYSQL INSERT SELECT problem
(1 answer)
Closed 6 years ago.
$new_id = mysqli_insert_id($dbc)
foreach ($_POST['d_specialty'] as $key => $value) {
$q = "INSERT INTO d_specialty (d_id, s_id) VALUES ($new_id, (SELECT specialty.id FROM specialty WHERE specialty.name = $value))";
$r = mysqli_query($dbc,$q);
i want to insert two values, the first one is a variable and the second is a select statement but the code above does not work...
First, use insert . . . select:
INSERT INTO d_specialty (d_id, s_id)
SELECT $new_id, specialty.id
FROM specialty
WHERE specialty.name = $value;
Second, parameterize the query so $new_id and $value are passed in as parameters rather than put directly into the query string. One reason is to prevent SQL injection attacks. Another very important reason is to guard against potential syntax errors.
When using insert into select you don't need to use values. Remove that and then move the $new_id variable into the select statement:
INSERT INTO d_specialty (d_id, s_id)
SELECT $new_id, specialty.id
FROM specialty
WHERE specialty.name = $value