簡體   English   中英

Perl數組搜索mysql結果

[英]perl array search on mysql results

我有一個包含手機號碼的數據庫。 如何編寫將所有數字都放入數組並檢查該數組中是否存在新數字的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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM