从MySQL数据库进行PDO,$ _ GET和SELECTING [英] PDO, $_GET, and SELECTing from MySQL Database

查看:72
本文介绍了从MySQL数据库进行PDO,$ _ GET和SELECTING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在业余时间从事PHP Pastebin式的项目,以学习PHP和服务器管理,但是遇到了很多问题,但我一直无法解决.我决定根据自己到目前为止收集到的信息从sratch重新启动,并将这些代码放在一起.

So I'm working on a PHP Pastebin-esque project on my freetime to learn PHP and server management, and I've run into a LOT of issues, and I haven't been able to solve them. I decided to restart from sratch on my own with the information I've gathered so far, and threw this code together.

<?php
    require 'connection.php';
        $getid = $_GET["id"];
        $sql = 'SELECT paste FROM pasteinfo WHERE id=:id';
        $stmt = $con->prepare($sql);
        $stmt->bind_param(':id', trim($_GET["id"], PDO::PARAM_INT));
        $stmt->execute();

        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo $row['paste'];
        }

?>

我想用此代码实现的系统是,用户可以在URL中键入他们想要查看的任何粘贴的 id ,并在其中显示pasteinfo行,这是保存粘贴本身的行.他们应该使用的格式是viewpaste.php?id =(用户输入).

What I'm trying to achieve with this code is a system where a user can type the id of whatever paste they're interested in viewing in the url and have it display the pasteinfo row, which is the row that holds the paste itself. The format they should have is viewpaste.php?id=(user input).

如何解决此代码?如果您解释了最终可能在注释中添加的任何代码,我也可以从中学习,我也将不胜感激.谢谢!

How can I fix this code? I would also greatly appreciate if you explain whatever code you might end up putting in the comments so I can learn from it. Thanks!

推荐答案

尝试一下;

connection.php

connection.php

try{
$db = new PDO('mysql:host=localhost;dbname=database_name;charset=utf8mb4', 'database_username', 'database_password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (PDOException $ex){
echo $ex->getMessage();return false;
}

function retrieve($query,$input) {
 global $db;
 $stmt = $db->prepare($query);
 $stmt->execute($input);
 $stmt->setFetchMode(PDO::FETCH_OBJ);   
 return $stmt;
}

要检索数据,请调用retrieve()函数

To retrieve data, call the retrieve() function

检索页面,例如display.php

Retrieval page, say display.php

require 'connection.php';
$getid = $_GET["id"];
$result=retrieve("SELECT paste FROM pasteinfo WHERE id=?",array($getid));
$row=$result->fetch();
//To get paste column of that id
$paste=$row->paste;
echo $paste;

这篇关于从MySQL数据库进行PDO,$ _ GET和SELECTING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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