why it's not working..
i want to update my table in database this is my code.
for ($i=0; $i <83 ; $i++) {
$link="this is test".$i;
$sql = "UPDATE tbl_gallery_images SET thumbnail_url='$link' WHERE gallery_id=1";
// Prepare statement
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
echo $link."<br>";
}
when i echo that link it's show 100% right but in db just update 82 number in all rows even it is in loop.
thanks in advance.
UPDATE tbl_gallery_images SET thumbnail_url = '$link' WHERE gallery_id = 1
As it is, this query updates column thumbnail_url in all records that have gallery_id = 1. Running the same query 84 times in a loop results in the same set of records being updated again and again in each iteration, with an increasing thumbnail_url. After the last iteration, all records where gallery_id = 1 end up with thumbnail_url = 'this is test83.
A sensible solution would be to add another criteria in the WHERE clause, in order to update just one record per iteration. Assuming that you have some integer column called id to disambiguate the records where gallery_id = 1, you would typically go:
`UPDATE tbl_gallery_images SET thumbnail_url = '$link' WHERE gallery_id = 1 AND id = $i`
Related
my MYSQL Database looks like this:
short -- idnumber--position
abc.......8765...........4
def........7453...........1
abc.......7398...........5
def........7542...........2
I have the idnumber and want to Update all with the the same 'short' as the idnumber. Update should be Position-1.
i have idnumber: 8765 its position should be 3 and position of id 7398 should be 4
How do i do it correctly? My Code dont work and i got no echo
<?php
$pdo = new PDO('mysql:host=localhost;dbname=testdb1', 'root', '');
$idV = $_GET['id'];
$statement = $pdo->prepare("UPDATE idtabelle SET position = position-1 WHERE short IN
(SELECT short FROM idtabelle WHERE idnumber = :idV)");
$statement->bindParam(':idV', $idV);
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_ASSOC))
{
echo $row['short'];
};
?>
try this. you can give a start id.
UPDATE idtabelle
SET position = position -1
WHERE idnumber = 8765
AND position > 2
ORDER BY position ASC;
You're attempting to fetch a row result from an UPDATE statement which doesn't return rows. (Your SELECT subquery is returning it's rows only to the UPDATE query -- the UPDATE result is the only thing returned to you.)
What you should be doing is something like:
...
$statement->bindParam(':idV', $idV);
if ($statement->execute())
echo $statement->rowCount() . " rows updated";
else
echo "Update failed with error: " .print_r($statement->errorInfo(), TRUE);
...
If you need to get the value of "short" in addition to updating, then you should probably do it as two queries instead - a SELECT to get "short", and an UPDATE to update the rows after.
If i try to run your query on sqlfiddle.com i get the error: "You can't specify target table 'idtabelle' for update in FROM clause".
But you can wrap your subquery into another subquery (derived table):
UPDATE idtabelle
SET position = position-1
WHERE short IN (SELECT short FROM (
SELECT short
FROM idtabelle
WHERE idnumber = :idV
) temp);
http://sqlfiddle.com/#!9/7d88a/1
I have the following query.
$sql = "SELECT customer FROM furniture WHERE id = :id AND category = :cat";
$stmt = $connectdb->prepare($sql);
$stmt->execute(array(':id'=>$id, ':cat'=>"1"));
$resulta = $stmt->fetchAll(PDO::FETCH_ASSOC);
$rowcount = count($result);
This works perfectly. But I have a requirement to get the number of rows from WHERE id = :id AND category = :cat as well as to get the number of rows from WHERE category = :cat. Is it possible to do both of them without having to write all those SELECT query lines twice?
You can use conditional sum to get the 2 different counts something as
select
sum(id = :id AND category = :cat) as count1,
sum(category = :cat) as count2
from furniture;
Later you just fetch the records and get the values of count1 and count2
NOTE : If you just do row count it will always return 1 since its using the aggregate function
I would suggest that you write the query as:
select sum(id = :id) as numCatId, count(*) as numCat
from furniture
where cat = :cat;
Putting the condition in the where clause allows MySQL to use an index on furniture(cat) (or better yet furniture(cat, id). In general, it is a good idea to put common filtering conditions in the where clause. This reduces the number of rows needed for processing the rest of the query.
I have a table for storing storing page visit counts. The first statement firs makes a query to get the current stored data, and the second increments the first result and updates the table.
here is an example.
$conn = new PDO(...);
//get current value
$stmt = $conn->query("SELECT counter FROM table");
$newValue = $stmt->fetch(PDO::FETCH_ASSOC);
//increment
$stmt = $conn->prepare("update table set counter = ?");
$stmt->execute(array($newValue));
The above is just an example, But I need an approach where it involves making a single prepare() to count and update the counter by 1, on each page visit.
The following will take the column value and increment itself by one (or whatever you'd like) in a single statement
UPDATE table SET counter = counter + 1
You can simply use this query:
update table set counter = (counter+1)
Please try this :
UPDATE table SET counter = ((SELECT counter FROM table)+1)
I hope this help u :)
I have a form where you enter something and it then shows you the id and title of last row, but as its being created there and then it just shows the previous last row at time of submission,
ie if last row id is 23 it would show row 23 when I actually want it to show row 24, to get around this I added +1 to id in the query below:
ie:
$last_id = $lr_result['id'] + 1;
now this works fine for the id but now I'm trying to get the title of the same row but whatever I try I always get the last title (at time of submission),
ie row 23 title rather then row 24 title (or nothing at all in the case for the snippet below).
// fetch id
$lastrow = "SELECT * FROM foo ORDER BY id DESC LIMIT 1";
$lr_result = mysql_fetch_assoc(mysql_query($lastrow));
//fetch id and add 1
$last_id = $lr_result['id'] + 1;
// fetch title where id = last_id
$lasttitlerow = "SELECT * FROM foo WHERE id='{$last_id}'";
$ltr_result = mysql_fetch_assoc(mysql_query($lasttitlerow));
$last_title = $ltr_result['title'];
echo $last_id . $last_title;
As always all help is appreciated and thanks in advance.
SELECT * FROM foo WHERE id = (SELECT max(id) FROM foo)
perhaps? The inner query would return '23', and the outerquery will fetch the entire record. You wouldn't be able to retrieve id=24, because that record doesn't exist yet - you haven't inserted it.
I have a table that's PK is an int field.
My problem is I need to swap two of the values round.
This is my current code:
$newId = (int)$id; $newId = $newId - 1;
$result = mysql_query("UPDATE homeScroller SET id = '$newId' WHERE id = '$id'") or die('error '.mysql_error());
$result2 = mysql_query("UPDATE homeScroller SET id = '$id ' WHERE id = '$newId'") or die('error '.mysql_error());
The table contains at max 3 rows that contain info for an slideshow on the frontpage of the website. The frontpage orders them by their IDs. So to change the order I need to edit the ID's.
The table contains at max 3 rows that contain info for an slideshow on the frontpage of the website. The frontpage orders them by their IDs. So to change the order I need to edit the ID's.
If that's the case, then your application/database is poorly architect-ed, and you need to re-think your database design before you go any further.
Primary keys should be immutable, i.e.: they should never change. What you're talking about doing is changing primary keys.
Fix your application/database, and you won't have this problem. Are you sure you shouldn't just be adding a new field 'sort_order' and ORDER BY sort_order instead or ordering by the ID field?
Assign one with a temporary id (0, because auto_increment usually starts at 1, so 0 should be unused):
$result = mysql_query("UPDATE homeScroller SET id = 0 WHERE id = '$id'");
$result = mysql_query("UPDATE homeScroller SET id = '$id ' WHERE id = '$newId'");
$result = mysql_query("UPDATE homeScroller SET id = '$newId ' WHERE id = 0");
IMPORTANT: However, you shouldn't rely on the primary key for ordering results, if you want to be able to change the order easily. Add a sequence column and sort by that instead.
UPDATE homeScroller
SET id = CASE id WHEN $id THEN $newid ELSE $id END
WHERE id IN ($id, $newid)
Do not use PK for this need.
If you can add timestamp field (shown_at) to this table, do it!
if cant, so update all PKs imultaneously. Just increment them.
For example: suppose we have such strings:
1 photo1
3 myphoto
5 somephoto
than use modulo 3.
1%3 = 1
3%3 = 0
5%3 = 2
increment all records
2 photo1
4 myphoto
5 somephoto
2%3 = 2
4%3 = 1
6%3 = 0
So u can use modulo and increment ids