简体   繁体   English

PHP / MYSQL PDO选择变量

[英]PHP/MYSql PDO Select with variable

Edit: Ok, so while this does work great for $ID when I set it equal to 28 , it is not working for instance below if I was trying to get the $name = $TableRows['name']; 编辑:好的,所以当我将$ID设置为28 ,这对于$ID确实很有效,但是如果我尝试获取$name = $TableRows['name']; ,则在下面的实例中不起作用$name = $TableRows['name']; variable. 变量。 I know I could select it, but other variables require this. 我知道我可以选择它,但是其他变量需要这样做。

$ID = 28; 
$name = $TableRows['name']; 

try { ini_set('display_errors', true); 
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$stmt = $conn->prepare(" SELECT ? AS name FROM TABLE1 e WHERE id = ?"); 
$stmt->execute(array($name, $ID));
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
    echo $v;
}
}

I'm in the process of transitioning from MySQLi to PDO. 我正在从MySQLi过渡到PDO。 My problem is I'd like to modify one of the values in my SELECT query, similar to how I set my WHERE value of ID to $ID = 28 . 我的问题是我想修改SELECT查询中的值之一,类似于将ID WHERE值设置为$ID = 28 Does anyone know how instead of just outputting millisecondstime from the SELECT query, I could change it to output $minutes $secondsrounded $millisecondsround instead? 有谁知道如何而不是仅从SELECT查询中输出millisecondstime时间,而是可以将其更改为输出$minutes $secondsrounded $millisecondsround This is the idea that I am going for: 这就是我要的想法:

$stmt = $conn->prepare("SELECT name, $minutes, $secondsrounded, $millisecondsround FROM TABLE1 e

This is my entire query: 这是我的整个查询:

<?php
echo "<center><div style='max-width: 1100px'><table id ='myTable' class='display' cellspacing='0' width='100%'>
    <thead>
    ";
echo "<tr><th>Name</th><th>Time</th></tr></thead>";

class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
        parent::__construct($it, self::LEAVES_ONLY); 
    }

    function current() {
        return "<td style='width: 150px; border: 1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() { 
        echo "<tr>"; 
    } 

    function endChildren() { 
        echo "</tr>" . "\n";
    } 
} 

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

$name = $TableRows['name'];

$milliseconds = $TableRows['millisecondstime'];

$time = $milliseconds / 1000;
$days = floor($time / (24*60*60));
$hours = floor(($time - ($days*24*60*60)) / (60*60));
$minutes = floor(($time - ($days*24*60*60)-($hours*60*60)) / 60);
$seconds = ($time - ($days*24*60*60) - ($hours*60*60) - ($minutes*60)) % 60;

$str_length = 3;
$millisecondsround = substr("000{$milliseconds}", -$str_length);
$secondsrounded = str_pad($seconds, 2, '0', STR_PAD_LEFT);

$ID = 28;   

try {
    ini_set('display_errors', true);
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare("SELECT name, millisecondstime FROM TABLE1 e 
        WHERE ID = ?
        ORDER BY millisecondstime ASC
        "); 

    $stmt->execute(array($ID));
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?> 

You can use placeholders in the SELECT list, and they'll be replaced with the values from the parameter array. 您可以在SELECT列表中使用占位符,它们将被参数数组中的值替换。

$stmt = $conn->prepare("
    SELECT name, ? AS minutes, ? AS secondsrounded, ? AS millisecondsround 
    FROM TABLE1 e 
    WHERE id = ?");
$stmt->execute(array($minutes, $secondsrounded, $millisecondsround, $ID));

Try this instead: 尝试以下方法:

$stmt = $conn->prepare("SELECT name, millisecondstime FROM TABLE1 e
WHERE ID = :ID
ORDER BY millisecondstime ASC
"); 

   $stmt->execute(array('ID' => $ID));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM