将功能更改为PDO [英] Change function to PDO

查看:66
本文介绍了将功能更改为PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的api中使用的一条语句.它是下面整个函数中的第二个$result变量.如何更改它以使用PDO?

This is a statement used in my api. It is the second $result variable in the whole function below. How can one change it to use PDO?

$result = query("SELECT p.IdPhoto, p.device_token, /*title,*/ p.IdUser FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto='%d'", $IdPhoto) ;

整个功能

function stream($IdPhoto=0) {

if ($IdPhoto==0) {

   //Load Photos    
    $result = query("SELECT IdPhoto, device_token, /*title,*/ IdUser FROM photos /*p*/ ORDER BY IdPhoto DESC LIMIT 300 ");

} else {

    //Want this to be PDO :)
    $result = query("SELECT p.IdPhoto, p.device_token, /*title,*/ p.IdUser FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto='%d'", $IdPhoto) ;

}

此代码是否可以代替第二个结果变量?我的目标是将其更改为PDO,以便可以更成功地进行扩展.到目前为止,还没有输出.

Would this code work in the place of the second result variable? My goal is to change it to PDO so it can scale more successfully. SO far, no output yet.

$dbh = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'A very secure password!!!');    
    $result = $dbh->query("SELECT p.IdPhoto, p.device_token, /*title,*/ p.IdUser FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto='%d'", $IdPhoto) ;

这些是我的数据库连接值

These are my database connection values

'host'     => 'localhost',
'dbname'   => 'pushchat',
'username' => 'pushchat',
'password' => 'A very secure password!!!',

更新 php日志文件

[08-Apr-2016 03:39:25 Europe/Berlin] PHP Notice:  Undefined index: IdPhoto in /Applications/MAMP/htdocs/Hi2/index.php on line 44

更新

在测试文件中使用了PDO语法,浏览器返回了一行.我如何才能通过j儿子立即调用该应用?

Used the PDO syntax in a test file and the browser returned a row. How can I get the app to call it now via j son?

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

function new_function()

{
echo "hi<br>";

$an_int = 12;    

// If this is an integer

if (is_int($an_int)) 

{

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);

$swag_Bag = 'p.device_token';

    print_r($swag_Bag);

}

}
new_function();

 ?>

更新

这是应用调用原始stream函数的方式.

This is how the app called the original stream function.

-(void)refreshStream
{
//The "stream" command from the web API
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"stream", @"command", nil] onCompletion:^(NSDictionary *json)
{
    //got stream
    [self showStream:[json objectForKey:@"result"]];

    ...

    }];
}

推荐答案

尝试一下:

$conn = new PDO('mysql:host=localhost;dbname=dbname', 'root', 'password');
function stream($IdPhoto=0) {
global $conn;

if ($IdPhoto==0) {

    // load the last 300 photos
    $stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 ");
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

} else {

//This is the statement i need to call explicitly via PDO

    //do the same as above, but just for the photo with the given id
    $stmt = $conn->prepare("SELECT p.IdPhoto, p.device_token, p.IdUser FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto= :idphoto ");
    $stmt->execute([':idphoto' => $IdPhoto]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

}

if (!$stmt->errorInfo()[0]) {
    // if no error occured, print out the JSON data of the 
    // fetched photo data
    print json_encode($result);
} else {
    //there was an error, print out to the iPhone app
    errorJson('Photo stream is broken');
}
}

这篇关于将功能更改为PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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