从函数调用PDO [英] Call PDO from a function

查看:55
本文介绍了从函数调用PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数从数据库返回一行并将其打印在浏览器中.

<?php  

$an_int = 12;    

// If this is an integer

if (is_int($an_int)) 

{

$conn = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'd]682\#%yI1nb3');
    global $conn;

$stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 ");

$stmt->execute();

$result = $stmt->fetch(PDO::FETCH_ASSOC);

print_r($result);

}

?>

如何从标准功能中调用上述代码?现在浏览器还没有输出任何东西.

<?php  

function new_function()

{

$an_int = 12;    

// If this is an integer

if (is_int($an_int)) 

{

$conn = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'd]682\#%yI1nb3');
    global $conn;

$stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 ");

$stmt->execute();

$result = $stmt->fetch(PDO::FETCH_ASSOC);

print_r($result);
}

}
new_function();

?>

浏览器不显示此当前语法的输出.有什么方法可以从new_function函数内部调用我的原始代码吗?有什么想法吗?

解决方案

好吧,在您的第一个文件中,您已经声明了global变量global $conn;我不完全知道为什么,但是无论如何都可以正常工作,因为$ conn在全球范围内.

但是在第二种情况下,当您在函数内声明global变量时,该函数将搜索位于全局范围内的$conn而不是显然具有连接句柄的局部变量$conn.

从功能中删除global $conn,它应该可以工作.

有关可变范围的更多信息- http://php.net/manual/zh/language.variables.scope.php

This function returns a row from the database and prints it in the browser.

<?php  

$an_int = 12;    

// If this is an integer

if (is_int($an_int)) 

{

$conn = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'd]682\#%yI1nb3');
    global $conn;

$stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 ");

$stmt->execute();

$result = $stmt->fetch(PDO::FETCH_ASSOC);

print_r($result);

}

?>

How can one call the above code from a standard fucntion? Right now the browser doesn't output anything yet.

<?php  

function new_function()

{

$an_int = 12;    

// If this is an integer

if (is_int($an_int)) 

{

$conn = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'd]682\#%yI1nb3');
    global $conn;

$stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 ");

$stmt->execute();

$result = $stmt->fetch(PDO::FETCH_ASSOC);

print_r($result);
}

}
new_function();

?>

The browser does not show output with this current syntax. Is there a way to call my original code from inside the new_function function... any ideas?

解决方案

Well, in your first file you have declared a global variable global $conn; I don't exactly know why but anyway that would work without any issue because $conn is in global scope.

But in your second case when you declare your global variable inside a function, the function would search for $conn which is in global scope instead of the local variable $conn which apparently has the connection handle.

remove the global $conn from your function and it should work.

more information on variable scopes - http://php.net/manual/en/language.variables.scope.php

这篇关于从函数调用PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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