[英]MySQL syntax error, but only when using through PHP
I'm trying to re-hash user IDs in my database, but I'm getting stuck. 我正在尝试在我的数据库中重新哈希用户ID,但我遇到了困难。
<?php
include("session.php");
include("functions.php");
$conn = ConnectMySQL();
setTimezone($session->username);
$sql = "SELECT username, userid FROM users";
$result = mysql_query($sql) or die(mysql_error());
while($rows = mysql_fetch_array($result)){
$username = $rows['username'];
$old = mysql_real_escape_string($rows['userid']);
$new = mysql_real_escape_string($session->generateRandID());
$moresql = "START TRANSACTION;
UPDATE users SET userid='$new' WHERE userid='$old';
UPDATE comments SET user='$new' WHERE user='$old';
UPDATE forum_posts SET poster_name='$new' WHERE poster_name='$old';
UPDATE forum_topics SET topic_poster_name='$new' WHERE topic_poster_name='$old';
UPDATE images SET author='$new' WHERE author='$old';
UPDATE likes SET user='$new' WHERE user='$old';
UPDATE music SET author='$new' WHERE author='$old';
UPDATE ratings SET user='$new' WHERE user='$old';
COMMIT;";
$newresult = mysql_query($moresql) or die(mysql_error());
if(!$newresult){echo "There was a problem with changing $username's hash. \n";}
else{echo "Changed $username's hash<i>!</i> \n";}
}
mysql_close($conn);
?>
The whole query works through PHPMyAdmin absolutely fine, just returns zero rows obviously. 整个查询通过PHPMyAdmin完全正常工作,显然只返回零行。 But when I try it with actual values being passed through it using it with PHP it gets suck:
但是当我尝试将实际值通过它与PHP一起使用时,它会变得很糟糕:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE users SET userid='b8aca4b680707453fa4dfe1bf1d0fddb' WHERE userid='8' at line 2
There are no other errors - I'm at a loss to think of what it is. 没有其他错误 - 我不知道它是什么。 Thanks in advance,
提前致谢,
Sam 山姆
Multiple queries are not supported by mysql_query(). mysql_query()不支持多个查询。 You must execute one query at a time.
您必须一次执行一个查询。 See http://php.net/manual/en/function.mysql-query.php
请参见http://php.net/manual/en/function.mysql-query.php
From the documentation : 从文档 :
mysql_query() sends a unique query (multiple queries are not supported)
You cant't send a series of queries to the database through php. 你不能通过PHP向数据库发送一系列查询。 Consider writing a procedure on your actual database and then calling that from php.
考虑在您的实际数据库上编写一个过程,然后从php调用它。
Also, those functions are deprecated. 此外,这些功能已被弃用。 You should consider a different library.
你应该考虑一个不同的库。 PDO is a popular one.
PDO很受欢迎。
The idea I generally use when working with transactions looks like this (semi-pseudo-code) : 我在处理事务时通常使用的想法如下(半伪代码):
try {
// First of all, let's begin a transaction
$db->beginTransaction();
// A set of queries ; of one fails, an exception should be thrown
$db->query('first query');
$db->query('second query');
$db->query('third query');
// If we arrive here, it means that no exception was thrown
// i.e. no query has failed ; and we can commit the transaction
$db->commit();
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$db->rollback();
}
Note that, with this idea, if a query fails, an Exception must be thrown : 请注意,有了这个想法,如果查询失败,则必须抛出异常:
PDO can do that, depending on how you configure it See PDO::setAttribute and PDO::ATTR_ERRMODE and PDO::ERRMODE_EXCEPTION else, with some other API, you might have to test the result of the function used to execute a query, and throw an exception yourself. PDO可以这样做,具体取决于你如何配置它参见PDO :: setAttribute和PDO :: ATTR_ERRMODE和PDO :: ERRMODE_EXCEPTION else,使用其他一些API,你可能需要测试用于执行查询的函数的结果,以及自己抛出异常。
Unfortunately, there is no magic involved : you cannot just put an instruction somewhere and have transactions done automatically : you still have to specific which group of queries must be executed in a transaction. 不幸的是,没有任何魔法:你不能只是在某个地方放置指令并自动完成事务:你仍然必须具体说明必须在事务中执行哪组查询。
For example, quite often, you'll have a couple of queries before the transaction (before the begin), and another couple of queries after the transaction (after either commit or rollback) ; 例如,通常,在事务之前(开始之前)和事务之后的另外几个查询(在提交或回滚之后),您将有几个查询; and you'll want those queries executed no matter what happened (or not) in the transaction.
并且无论事务中发生了什么(或没有),您都希望执行这些查询。
===================================================== ================================================== ===
You can use mysqli also to achieve same 你也可以使用mysqli来实现同样的目的
For ex 对于前者
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* disable autocommit */
mysqli_autocommit($link, FALSE);
mysqli_query($link, "CREATE TABLE myCity LIKE City");
mysqli_query($link, "ALTER TABLE myCity Type=InnoDB");
mysqli_query($link, "INSERT INTO myCity SELECT * FROM City LIMIT 50");
/* commit insert */
mysqli_commit($link);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.