1

名称键值。. . . . . 要更新的用户名

#!/usr/bin/perl -w
# Use the DBI module

use  strict;
use warnings;
use DBI;

# CONFIG VARIABLES
my $platform = "mysql";
my $database = "prod";
my $host     = "localhost";
my $username = "root";
my $password = "admin";

# DATA SOURCE NAME
my $dsn = "dbi:$platform:$database:$host";

# PERL DBI CONNECT
my $connect = DBI->connect($dsn, $username, $password);

# VARS for Examples
my $query;
my $query_handle;
my $id;
my $demo;

# Example 2 using do() UPDATE   

# SAMPLE VARIABLE AND VALUES TO PASS INTO SQL STATEMENT
$id   = "username";
$name = "Arty";

# do() THE UPDATE
$query        = "UPDATE jos_config SET values = '$name' WHERE namekey = $id;";
$query_handle = $connect->prepare($query);

# EXECUTE THE UPDATE
$query_handle = $connect->do($query);

print STDERR "ERROR: $DBI::errstr";
print STDERR "INFO: $query_handle rows updated";

undef $query;

错误信息:

DBD::mysql::db do failed: 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 'values = 'Arty' WHERE namekey = smtp_username' at line 1 at /home/arty/Documents/SmtpDbchange2.pl line 40.
ERROR: 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 'values = 'Arty' WHERE namekey = smtp_username' at line 1Use of uninitialized value $query_handle in concatenation (.) or string at /home/arty/Documents/SmtpDbchange2.pl line 43.

请协助

4

4 回答 4

6

您的语法错误来自“值”是 SQL 中的保留字这一事实。当它用作标识符时,您需要引用该标记:

UPDATE jos_config SET `values` = ...

通常,MySQL 会在查询摘录的开头向您显示语法错误的位置,就像它为您所做的那样。

您的下一个问题将是您没有正确引用您比较的文字值namekey,并且该文字值看起来像一个标识符而不是 MySQL 的字符串。这里的解决方案是忘记插值变量并使用参数化查询:

$connect->do('UPDATE jos_config SET `values`=? WHERE namekey=?', undef, $name, $id);
于 2012-09-12T04:16:41.017 回答
0

看起来您在 namekey 值 $id 周围缺少一些引号:

$query = "UPDATE jos_config SET values = '$name' WHERE namekey = '$id';";
于 2012-09-12T02:54:13.357 回答
0

如果您收到这样的错误,请打印出违规语句:

print $query;

这表明:

UPDATE jos_config SET values = 'Arty' WHERE namekey = username;

然后盯着它,直到你看到丢失的引号。

然后停止这样做并使用占位符:

$query = "UPDATE jos_config SET values=? WHERE namekey=?;";
$query_handle = $connect->prepare($query);
my $result = $connect->do($query, {}, $name, $id);
于 2012-09-12T03:56:43.327 回答
0

你可以试试这个:

my $sthSetid = $dbh->prepare("UPDATE $table SET nameOfColumnToChange = ? WHERE id = ?");
$sthSetid->execute($newData, $refGetid[0]);
$sthSetid->finish;
于 2021-08-07T15:23:23.660 回答