gpt4 book ai didi

mysql - perl 数组搜索 mysql 结果

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:36 25 4
gpt4 key购买 nike

我有一个包含手机号码的数据库。我如何编写 perl 脚本将所有数字放入数组并检查新数字是否已经存在于该数组中?

创建表:

CREATE TABLE consumeruser (
ConsumerId int(10) NOT NULL AUTO_INCREMENT,
ConsumerName varchar(45) DEFAULT NULL,
ConsumerMobNo varchar(10) DEFAULT NULL,
PRIMARY KEY (ConsumerId)
) ENGINE=InnoDB AUTO_INCREMENT=4494 DEFAULT CHARSET=latin1

脚本:

#!/usr/bin/perl -w
use strict;
use warnings qw(all);

use DBI;
use Getopt::Long;
use Pod::Usage;
use Text::CSV_XS;

my $username = 'root'; # set your MySQL username
my $password = 'xxxx'; # set your MySQL password
my $database = 'app'; # set your MySQL database name
my $server = 'localhost'; # set your server hostname (probably localhost)

my $dbh = DBI->connect( "DBI:mysql:$database;host=$server", $username, $password )
|| die "Could not connect to database: $DBI::errstr";

my $CustomerMobileNumber = 9999999;
my @MobileNumbers;
my $mobileNumberQuery = "select ConsumerMobNo from consumeruser";
my $sth = $dbh->prepare($mobileNumberQuery);
$sth->execute();
while ( my @row = $sth->fetchrow_array() ) {
push @MobileNumbers, $row;

if (/test for is present/) {
#9999999 found in array;
} else {
#9999999 not found in array;
}
}

最佳答案

您可以要求数据库搜索号码:

my $mobileNumberQuery = "SELECT 1 FROM consumeruser WHERE ConsumerMobNo = ?";
my $sth = $dbh->prepare($mobileNumberQuery);
$sth->execute(9999999);
if ($sth->fetchrow_array) {
print "Found.\n"
} else {
print "Not found.\n";
}

关于mysql - perl 数组搜索 mysql 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25947463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com