dongmu5815 2016-10-20 04:24
浏览 41

使用PDO与MySQL来$ _GET两列

I'm working on a Pastebin-esque project in my free time, and last night I solved an issue I've had for a couple of days. (see this thread) However, I managed to mess it all up when I tried to make the code fetch a second column, 'Title'.

Please read the hyperlinked thread and look at Odin's answer or see the code below.

How can I make that code fetch multiple columns?

The code: viewpaste.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;

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

Just in case you need a little bit more of an explanation of my project, I'm making a pastebin clone (from scratch) and am trying to make a page where a user can enter the id of whatever paste they're wanting to view in the URL and have my code grab all the title and paste data of that id. This should all be done with $_GET, and I had it solved until I realized I never got titles working, and here we are.

Thanks!

  • 写回答

1条回答 默认 最新

  • drhzn3911 2016-10-20 04:29
    关注

    Just specify the column in the SELECT query

    $result=retrieve("SELECT title, paste FROM pasteinfo WHERE id=?", array($getid));
    $row=$result->fetch();
    
    $paste=$row->paste;
    $title=$row->title;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题