Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用fetchrow\u hashref存储数据_Mysql_Perl_Hash - Fatal编程技术网

Mysql 使用fetchrow\u hashref存储数据

Mysql 使用fetchrow\u hashref存储数据,mysql,perl,hash,Mysql,Perl,Hash,我试图从MySQL数据库中获取信息,然后用perl对其进行操作: use strict; use DBI; my $dbh_m= DBI->connect("dbi:mysql:Populationdb","root","LisaUni") or die("Error: $DBI::errstr"); my $Genotype = 'Genotype'.1; #The idea here is eventually I will ask the database how many G

我试图从MySQL数据库中获取信息,然后用perl对其进行操作:

use strict;
use DBI;

my $dbh_m= DBI->connect("dbi:mysql:Populationdb","root","LisaUni") 
or die("Error: $DBI::errstr");

my $Genotype = 'Genotype'.1;
#The idea here is eventually I will ask the database how many Genotypes there are, and then loop it round to complete the following for each Genotype:

my $sql =qq(SELECT TransNo, gene.Gene FROM gene JOIN genotypegene ON gene.Gene =       genotypegene.Gene WHERE Genotype like '$Genotype');
my $sth = $dbh_m-> prepare($sql);
$sth->execute;

my %hash;

my $transvalues = $sth->fetchrow_hashref;
my %hash= %$transvalues;

$sth ->finish();
$dbh_m->disconnect();       

my $key;
my $value;

while (($key, $value) = each(%hash)){
 print $key.", ".$value\n; }
这段代码不会产生任何错误,但是%hash只存储从数据库中获取的最后一行(我是从一开始就想到这样写的)。如果我键入:

while(my $transvalues = $sth->fetchrow_hashref){
print "Gene: $transvalues->{Gene}\n";
print "Trans: $transvalues->{TransNo}\n";
}
然后它会打印出所有的行,但我需要在关闭与数据库的连接后提供所有这些信息

我还有一个相关的问题:在我的MySQL数据库中,行由例如'Gene1'(基因)'4'(TransNo)组成。一旦我像上面那样从数据库中取出这些数据,TransNo还会知道它与哪个基因有关吗?或者我需要为此创建某种哈希结构吗?

您调用的是“错误”函数
fetchrow\u hashref
将返回one行作为hashref,您应该将它的用法包装在一个循环中,当
fetchrow\u hashref
返回
undef
时结束它

您似乎正在寻找
fetchall\u hashref
,它将把所有返回的行作为散列,第一个参数指定了要用作键的字段

$hash_ref = $sth->fetchall_hashref ($key_field);
每一行将作为内部hashref插入
$hash\u ref
,使用
$key\u field
作为键,您可以在
$hash\u ref
中找到该行

文件上怎么说? fetchall_hashref方法可用于从准备和执行的语句句柄获取要返回的所有数据

它返回对散列的引用,该散列包含所获取的$key\u字段列的每个不同值的键

对于每个键,对应的值都是对包含所有选定列及其值的哈希的引用,如fetchrow_hashref()所返回


文档链接

在Perl中的SQL语句中,您可以从$SQL变量中取出变量,并将其包含在execute语句中。试试这个:
my$sql=qq(选择……像在哪里?);my$sth=$dbh\u m->prepare($sql)$执行某事物这将防止SQL注入攻击,是一个很好的实践。谢谢-我现在将其更改为:my$transvalues=$sth->fetchall_hashref('Gene');print$transvalues->{Gene1}->{TransNo}我如何循环它以打印所有基因?By。。创建一个在hashref上迭代的循环,您基本上回答了这个问题。样本(同样,请接受答案,将问题标记为已解决)