我有问题;我的数据库中有一个名为items的表,内容包括id
,allowComments
,fromUserId
,category
,categoryTitle
,price
,{{ 1}},likesCount
,imagesCount
,viewsCount
等。
我希望能够将我想要的reviewsCount
重新排列为最新的id
,然后向上移动。
以下是一个例子:
id
等
所以我的问题是,如何使用PHP使id allowComments fromUserId category categoryTitle price
1 1 1 3 Phone 20000
2 1 1 5 Car 100000
3 1 5 2 Console 20000
4 1 2 1 Fashion 100
5 0 1 3 Phone 12000
6 1 2 3 Phone 21300
id
成为最后一个3
,并将其他ID排序为有序推进?
让我们说这是我的php文件来从数据库中获取内容
id
答案 0 :(得分:1)
测试了上面的脚本没有用,所以我做了这样的研究,发现出了问题。使用下面的代码,我测试了它,它的工作
<?php
/* Database info */
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'globali2_olx';
/* Set DSN and Options */
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
/* Instantiate the PDO object */
try{
$pdo = new PDO($dsn, $dbuser, $dbpass, $options);
}
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
/* Set ID */
$id = 25;
/* Set query */
$query = "update location j1 inner join location j2 on j1.id <= j2.id
left outer join location j3 on j2.id < j3.id
set j1.id = j2.id + 1
where j1.id = $id and j3.id is null";
/* Prepare query */
$stmt = $pdo->prepare($query);
/* Bind values */
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
/* Execute query */
try {
$stmt->execute();
}
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
/* Set query */
$query = 'ALTER TABLE location AUTO_INCREMENT = 1';
/* Query database */
try {
$pdo->query($query);
}
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
?>
或者你可以使用这个简单的代码
<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config/config.php");
// Create connection
$con = mysqli_connect($host, $user, $pass, $database);
$id = 128;
$Sql_Query = "update product set id=3 where id=1";
$Sql_Query = "update product j1 inner join product j2 on j1.id <= j2.id
left outer join product j3 on j2.id < j3.id
set j1.id = j2.id + 1
where j1.id = $id and j3.id is null";
if(mysqli_query($con,$Sql_Query))
{
echo 'Record Updated Successfully';
}
else
{
echo 'Something went wrong';
}
mysqli_close($con);
?>
答案 1 :(得分:0)
好的,在下面的代码中,我评论了你需要知道的一切。阅读本文非常重要,这样您才能理解它是如何运作的!我也留下了这个链接:PHP Prepared Statements,这样您就可以在更加用户友好的环境中了解有关PDO和准备语句的更多信息。在这个问题的底部,您可以找到没有所有注释的代码,以使其更加清晰。
请记住,您需要在代码顶部设置正确的数据库信息。
评论中包含说明的代码:
<?php
/* Database info */
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$dbname = 'globali2_olx';
/* First we set up some information for our PDO object */
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
/* Next we try to instantiate the PDO object and see if we can connect to the database */
try{
$pdo = new PDO($dsn, $dbuser, $dbpass, $options);
}
/* Here we'll catch any database connection errors and output them so we know what's going on */
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
/* Now lets try to change row id 3 to the last row id */
/* First we setup our query */
$query = 'UPDATE items SET id=(SELECT max(id)+1) FROM items WHERE id = :id';
/* As you can see, we don't have the number 3 in there. Instead, we have a
placeholder ':id'. This is because we're going to use Prepared Statements.
They are not needed for this case, but I want to show how to do this
because you do need them if you want to insert data that's provided by a user.
This is to prevent SQL injection. SQL injection is very easy and allows
visitors to your website to completely delete your database if you don't
protect yourself against it. This will do just that. */
/* So lets prepare our query first */
$stmt = $pdo->prepare($query);
/* Set ID */
$id = 3;
/* Now we're going to bind the data to the placeholder */
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
/* Now all that's left to do is execute it so our database gets updated */
try {
$stmt->execute();
}
/* Again, catch any errors in our query and output them */
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
/* Next we need to re-arrange the id's */
/* First we setup our query */
$query = 'ALTER TABLE items AUTO_INCREMENT = 1';
/* Again, Prepared Statements are not nessesary here. So I'm gonna show
you how to do it without them. */
/* All we have to do is query the database directly */
try {
$pdo->query($query);
}
/* Again, catch any errors in our query and output them */
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
?>
清洁代码:
<?php
/* Database info */
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$dbname = 'globali2_olx';
/* Set DSN and Options */
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
/* Instantiate the PDO object */
try{
$pdo = new PDO($dsn, $dbuser, $dbpass, $options);
}
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
/* Set query */
$query = 'UPDATE items SET id=(SELECT max(id)+1) FROM items WHERE id = :id';
/* Prepare query */
$stmt = $pdo->prepare($query);
/* Set ID */
$id = 3;
/* Bind values */
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
/* Execute query */
try {
$stmt->execute();
}
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
/* Set query */
$query = 'ALTER TABLE items AUTO_INCREMENT = 1';
/* Query database */
try {
$pdo->query($query);
}
catch(PDOException $e){
$error = $e->getMessage();
return $error;
exit;
}
?>
答案 2 :(得分:0)
研究代码并使用最适合您的代码。修改表格,一切正常
UPDATE jobs j1
INNER JOIN jobs j2 ON j1.worker_id <= j2.worker_id
LEFT OUTER JOIN jobs j3 ON j2.worker_id < j3.worker_id
SET j1.worker_id = j2.worker_id + 1
WHERE j1.worker_id = 3 AND j3.worker_id IS NULL;
演示:
mysql> create table jobs (id serial primary key, worker_id int);
mysql> insert into jobs (worker_id) values (1), (2), (3), (4), (5);
mysql> select * from jobs;
+----+-----------+
| id | worker_id |
+----+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+----+-----------+
mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id
left outer join jobs j3 on j2.worker_id < j3.worker_id
set j1.worker_id = j2.worker_id + 1
where j1.worker_id = 3 and j3.worker_id is null;
mysql> select * from jobs;
+----+-----------+
| id | worker_id |
+----+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 4 |
| 5 | 5 |
+----+-----------+
无论我们想要改变什么值,它都有效:
mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id
left outer join jobs j3 on j2.worker_id < j3.worker_id
set j1.worker_id = j2.worker_id + 1
where j1.worker_id = 5 and j3.worker_id is null;
mysql> select * from jobs;
+----+-----------+
| id | worker_id |
+----+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 4 |
| 5 | 7 |
+----+-----------+
即使我们正在更改表中已具有最高值的行,它仍然有效:
mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id
left outer join jobs j3 on j2.worker_id < j3.worker_id
set j1.worker_id = j2.worker_id + 1
where j1.worker_id = 7 and j3.worker_id is null;
mysql> select * from jobs;
+----+-----------+
| id | worker_id |
+----+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 4 |
| 5 | 8 |
+----+-----------+