Getting all effected rown in MySQL UPDATE [duplicate] - php

This question already has answers here:
UPDATE/DELETE in mysql and get the list of affected row ids?
(3 answers)
Closed 9 years ago.
Title pretty much says it. I am using the deprecated mysql_ functions. I've got a query like:
UPDATE `table`
SET `row` = 'newtext'
WHERE `row` = 'oldtext'
Is there an easy way to get all effected row's primary key? Some glorious combination of mysql_insert_id and mysql_affected_rows? How can I do this without looping and doing each update one row at a time?

As a solution to your problem please refer the following sample code snippet
UPDATE `table` SET `row` = 'newtext' WHERE `row` = 'oldtext'
select id from table where row='oldtext'

Related

PHP script to copy mysql column to another table [duplicate]

This question already has answers here:
php and mysql copy record from one table to another
(4 answers)
Closed 3 years ago.
Is there any easy way to copy one a column in a table to another table without changing the order ?
I am a beginner here. I would be much thankful to you if you could supply in depth answer.
I think this query can help you out
INSERT INTO second_table SELECT * FROM first_table WHERE id IN(1,2,3,4...n)

Is it safe to get ID through lastInsertId()? [duplicate]

This question already has an answer here:
MySQL and the chance of the wrong id being returned by LAST_INSERT_ID()
(1 answer)
Closed 8 years ago.
I've a database with hundreds of records which inserted in each seconds, I have the below insert query and I want to know the insert id of this query:
$currQuery = $pdo->prepare("INSERT INTO some_table (...... )");
$currQuery->execute("....");
$queryInsertId = $pdo->lastInsertId();
I just want to know that is it safe to use lastInsertId() while I having hundreds of new records in every seconds? any ideas?
Yes it is safe. It returns the last insert id for the connection.

COUNT values in a column and store count in variable? [duplicate]

This question already has answers here:
How can I store the result of an SQL COUNT statement in a PHP variable
(4 answers)
Closed 8 years ago.
I have tried a few things and googled but can't get something that works. I want to use an SQL query to count how many rows in a table has "Rock" as it's value inside the "Genre" column. And then I need to store the count number inside a variable so that I can echo it later in my page. Any help? I'm new to php...
A query like this
$lo_rec = mysql_query("SELECT COUNT(`genre`) as `total`
FROM `tablename`
WHERE `genre` = 'ROCK'");
$result = mysql_fetch_object($lo_rec);
Then when you grab the results, assign it
$li_count = $result->total;
echo $li_count;
And before anyone says so, I know to use mysqli or PDO :-)

MySQL: How to pre-append text to data in an existing column? [duplicate]

This question already has answers here:
How to prepend a string to a column value in MySQL?
(5 answers)
Closed 9 years ago.
I have a populated MySQL TABLE where I need to pre-append the text "NEW_" for the name field. (Long story, but this is what needs to be done for now.)
I imagine this can be done with an UPDATE but I'm not sure how to add "NEW_" to the start of the 'name' column. I also work in PHP, so I need to do this in PHP where I read each record, extract 'name' field, add this and then do an UPDATE for the record? Or can this all be done in MySQL?
use this query
UPDATE table_name set `name` = CONCAT('NEW_',`name`)
You can use CONCAT like this:
UPDATE tbl SET name=CONCAT('NEW_',name)
WHERE name NOT LIKE 'NEW\_%';
The second line will stop this operating on columns with NEW_ already at the start.
You can just do it like this:
UPDATE myTable SET name = CONCAT('NEW_',name);
As posted before by myself this will not work:
UPDATE myTable SET name = 'NEW_' + name;

Can I use get the just insert table row id, from mysql_query? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: how to get last inserted ID of a table?
I use mysql_query() in PHP to insert a new record in my Database.
It will auto generate a row, with a user Id, can I use the result to get that Id?
mysql_insert_id()
Have you considered upgrading to PDO?

Categories