被mysqli困惑 [英] Confused by mysqli

查看:61
本文介绍了被mysqli困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,背景......是的,我是另一个邪恶的,被唾弃的,该死的

Perl贩子,但我不是想要开始一场火焰战争,我是一个很好的尝试

了解一些东西...


我可以像这样在Perl中编写脚本,这对我来说很漂亮(和

使用heredocs我认为确实可以防止perl对抗许多争论

,HTML全部被转义并且显式返回和东西 - 我可以看到这是
。 ..''print"< p class = \" text \"> stuff< / p> \ n";''很糟糕

我...所以我使用heredocs),简单,快速的mod_perl ...


....但我不想让我的视野有限,所以我想找出如何用PHP支付



#!/ usr / bin / perl

使用严格;


使用CGI;

使用DBI;


sub notLoggedIn();


my $ cgi = new CGI;

print $ cgi-> head呃;

我的$ sessionid = $ cgi-> cookie(''session'');


notLoggedIn除非$ sessionid;

我的$ dbh = DBI-> connect(''DBI:mysql:session'',''user'',''password'');


我的$ get_session_st =<<"" EOF";

SELECT *

FROM session

WHERE id =?

EOF

我的$ get_session = $ dbh-> prepare($ get_session_st);

$ get_session->执行($ sessionid);


我的$ session;

$ session = $ get_session-> fetchrow_hashref或notLoggedIn;

$ get_session->完成;
<我的$ k为
(密钥%{$ session}){

$ ENV {" SESSION_" .uc($ k)} = $ session-> {$ v};

}


print<<"" EOF";

<! - #include virtual =" /includes/page_header.shtml" - >

< p>您已登录。< / p>

< p>这里'这是您的会话数据......< / p>

< table>

EOF


while(my($ k,$ v)=每个%ENV){

print<<"" EOF" if $ k =〜/ ^ SESSION_ /;

< tr>

< td> $ k< / td>

< td> $ v< / td>

< / tr>

EOF

}


打印<<<" EOF";

< / table>

<! - #include virtual =" /includes/page_footer.shtml" - >

EOF


sub notLoggedIn(){

print<<"" EOF"然后退出;

<! - #include virtual =" /includes/page_header.shtml" - >

< p>抱歉,你不是登录。< / p>

<! - #include virtual =" /includes/page_footer.shtml" - >

EOF
}

整个过程非常简单。但无论如何......

Okay, background... yes, I am another of those evil, spurned, damnable
Perl mongers, but I''m not trying to start a flamewar, I''m juust tryung
to understand something...

I can write a script in Perl like so, and it''s pretty to me (and the
using of the heredocs I think does defend perl against many arguments
withthe HTML being all escaped and explicit returns and stuff -- which
I can see... ''print "<p class=\"text\">stuff</p>\n";'' is terrible to
me to.. so I use heredocs), and simple, and fast under mod_perl...

....but I don''t want my horizons limited so I wan to work out how to do
it in PHP:

#!/usr/bin/perl
use strict;

use CGI;
use DBI;

sub notLoggedIn();

my $cgi = new CGI;
print $cgi->header;
my $sessionid = $cgi->cookie(''session'');

notLoggedIn unless $sessionid;
my $dbh = DBI->connect(''DBI:mysql:session'',''user'',''password'');

my $get_session_st = <<"EOF";
SELECT *
FROM session
WHERE id = ?
EOF
my $get_session = $dbh->prepare($get_session_st);
$get_session->execute($sessionid);

my $session;
$session = $get_session->fetchrow_hashref or notLoggedIn;
$get_session->finish;

for my $k (keys %{$session}) {
$ENV{"SESSION_".uc($k)} = $session->{$v};
}

print <<"EOF";
<!--#include virtual="/includes/page_header.shtml"-->
<p>You are logged in.</p>
<p>Here''s your session data...</p>
<table>
EOF

while (my ($k, $v) = each %ENV) {
print <<"EOF" if $k =~ /^SESSION_/;
<tr>
<td>$k</td>
<td>$v</td>
</tr>
EOF
}

print <<"EOF";
</table>
<!--#include virtual="/includes/page_footer.shtml"-->
EOF

sub notLoggedIn () {
print <<"EOF" and exit;
<!--#include virtual="/includes/page_header.shtml"-->
<p>Sorry, you are not logged in.</p>
<!--#include virtual="/includes/page_footer.shtml"-->
EOF
}
this whole thing is pretty simple. But anyway...


>从我所看到的,Perl DBI中的正常构造如上所示
>From what I can see, the normal construct in the Perl DBI as seen above



在PHP中有点难以理解...


重写perl位并修改为多于一次的结果

设置...使用人们喜爱的那些令人震惊的城市示例:


使用DBI;

我的$ dbh = DBI-> connect( ''DBI:mysql:database'',''user'',''password'')

或die无法连接到数据库:$ DBI :: errstr\ n ;;


我的$ query =<<"" EOF";

SELECT *

来自城市
WHERE国家=?

EOF


我的$ statement = $ dbh-> prepare($ query);

$ statement->执行(''USA'');


我的@cities;

while(my $ city = $ statement- > fetchrow_hashref){

推@cities,$ city;

}


我的$ city( @cities){

#在每个城市做东西...... hashref的键与

#表中的列名相同

}


我不想,也不认为我*可以*使用PHP中的query()方法

关闭一个mysqli对象,因为我需要占位符。文档

没有表明我可以找到那个查询()(好像是
结合准备和执行,但似乎没有这么做)使用

占位符。所有的占位符示例都说使用prepare和

执行,以及bind_params但是我也不知道

的名字我得到的东西 - Perl DBI中的fetchrow_hashref()基本上是

返回一个关联数组(尽管在引用下)。


我可以在准备好之后使用fetch()来回复,执行,和

store_result - 虽然最后一些对我来说似乎有点傻 -

应该是自动的。


但是如果我尝试使用fetch_row()或fetch_assoc(),我得到PHP致命

错误:调用未定义的方法mysqli_stmt :: fetch_row() - 虽然

文档似乎表明我应该能够做到这一点......

is a bit hard to figure out in PHP...

To restate the perl bit and modify to a more-than-one-off- result
set... using that wunnerful cities example people love:

use DBI;
my $dbh = DBI->connect(''DBI:mysql:database'',''user'',''password'')
or die "Can''t connect to database: $DBI::errstr\n";

my $query = <<"EOF";
SELECT *
FROM cities
WHERE Country = ?
EOF

my $statement = $dbh->prepare($query);
$statement->execute(''USA'');

my @cities;
while (my $city = $statement->fetchrow_hashref) {
push @cities, $city;
}

for my $city (@cities) {
# do stuff over each city... keys of hashref are same as
# column names in table
}

I don''t want to, and don''t think I *can* use the query() method in PHP
off a mysqli object because I need placeholders. The documentation
makes no indication that I can find that query() (which seems to
combine prepare and execute, but doesn''t seem to do so exactly) can use
placeholders. All the placeholder examples say to use prepare and
execute, as well as bind_params However I also do not know the names of
the things I''m getting back -- fetchrow_hashref() in Perl DBI basically
returns an associative array (though couched under a reference).

I can get things back using fetch() after prepare, execute, and
store_result -- though the last of these seems kind of silly to me --
should be automatic.

But if I try to use fetch_row() or fetch_assoc(), I get PHP Fatal
Error: Call to undefined method mysqli_stmt::fetch_row() -- though the
documentation seems to indicate I should be able to do this...


>从文档中说的是,直接翻译perl,我可以
>From what the docs say, the direct translation of the perl that I can



睡着时写的(事实上,不是点缀,因为我实际上已经这样做了

而且这是令人讨厌的o当你希望自杀女孩获得
时梦想得到Perl ......),似乎应该是:


$ dbh = new mysqli (''localhost'',''user'',''password'',''database'');

if($ err = mysqli_connect_errno()){

print" Connect failed:$ err";

exit();

}


$ query =< << EOF

SELECT *

来自城市

国家=?

EOF;


$ statement = $ dbh-> prepare($ query);

$ statement-> bind_param(''USA'');

$ statement-> execute();

$ statement-> store_result();


$ cities = array();

$ i = 0;

while($ city = $ statement-> fetch_assoc()){

$ cities [$ i] = $ city;

$ i ++;

}


foreach($ cities为$ k = $ v){

#在每个城市做东西...... hashref的键与

#列中的列名相同le

}


但它不起作用。我得到了搞乱的错误字符串,上面写着

fetch_assoc()方法不存在...


所以我开车去了发布到usenet并将所有内容写成

的点,尽管这三个Perl程序员的美德否则会填补我的b / b
懒惰,傲慢和不耐烦的存在(他们应该这样做 - 请问

Larry Wall)。


那么有人可以告诉我,我做错了吗?

我差不多这样的事情会更容易,如果它没有*像Perl,

而不是对我感觉就像我正在写一些奇怪的东西

perl那个在滑块中写了几个宇宙...


-

道奇

write while asleep (fact, not embellishment, as I have in fact done so
and it''s damned annoying to get Perl in a dream when you were hoping
for a Suicide Girl...) , seems like it should be:

$dbh = new mysqli(''localhost'',''user'',''password'',''database'');
if ($err = mysqli_connect_errno()) {
print "Connect failed: $err";
exit();
}

$query = <<<EOF
SELECT *
FROM cities
WHERE Country = ?
EOF;

$statement = $dbh->prepare($query);
$statement->bind_param(''USA'');
$statement->execute();
$statement->store_result();

$cities = array();
$i = 0;
while ($city = $statement->fetch_assoc()) {
$cities[$i] = $city;
$i++;
}

foreach ($cities as $k =$v) {
# do stuff over each city... keys of hashref are same as
# column names in table
}

But it doesn''t work. I get that messed up error string that says the
fetch_assoc() method doesn''t exist...

So I''m driven to the point of posting to usenet and writing all that
out, despite those three Perl programmer virtues that otherwise fill my
lazy, hubristic and impatient being (they''re supposed to do so--ask
Larry Wall).

So can someone tell me what I''m doing totally wrong?
I almost thing this would be easier if it were *nothing* like Perl,
instead of feeling to me like I''m writing something in some strange
perl that was written in a universe a few over in Sliders...

--
Dodger

推荐答案

cgi = new CGI;

print
cgi = new CGI;
print


cgi-> header;

我的
cgi->header;
my


sessionid =
sessionid =


这篇关于被mysqli困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆