gpt4 book ai didi

PHP/MYSql PDO 选择变量

转载 作者:行者123 更新时间:2023-11-29 04:33:10 26 4
gpt4 key购买 nike

编辑: 好吧,当我将它设置为 28 时,虽然这对 $ID 非常有效,但它不适用于下面的实例,如果我试图获取 $name = $TableRows['name']; 变量。我知道我可以选择它,但其他变量需要这个。

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

我正在从 MySQLi 过渡到 PDO。我的问题是我想修改我的 SELECT 查询中的一个值,类似于我将 ID 的 WHERE 值设置为 $ID = 28 。有谁知道如何不只是从 SELECT 查询中输出 millisecondstime,而是可以将其更改为输出 $minutes $secondsrounded $millisecondsround?这就是我想要的想法:

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

这是我的整个查询:

<?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>";
?>

最佳答案

您可以在 SELECT 列表中使用占位符,它们将被替换为参数数组中的值。

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

关于PHP/MYSql PDO 选择变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53328858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com