gpt4 book ai didi

php - 从 MySQL 数据库中按 id 显示项目

转载 作者:行者123 更新时间:2023-11-30 01:25:54 26 4
gpt4 key购买 nike

INSERT INTO `echipe` (`id`, `nume_echipa`, `victorii`, `infrangeri`, `steag`) VALUES
(1, 'Trencin', 0, 0, '/img/Trencin.png'),
(2, 'Astra', 0, 0, '/img/Astra.png');

所以我想做类似的事情

Astra 与 Trencin,但要从表中获取指定 ID 并显示名称和其他信息。

我的PHP脚本

<?php
$con=mysqli_connect("localhost", "root", "", "pariuri");
// check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM echipe");

while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['nume_echipa'];
echo "<br>";
}

mysqli_close($con);
?>

最佳答案

使用原始 mysqli

<?php
$con = mysqli_connect("localhost", "root", "", "pariuri");
// check connection
if (mysqli_connect_errno())
{
trigger_error(mysqli_connect_error());
}

$sql = "SELECT * FROM echipe WHERE id = ?";
$stm = $con->prepare($sql) or trigger_error($con->error."[$sql]");
$stm->bind_param('s', $id);
$stm->execute();
$stm->bind_result($nume_echipa);
$stm-fetch();

echo $id, $nume_echipa;

使用原始 PDO

$dsn = "mysql:host=localhost;dbname=pariuri;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'root','', $opt);

$sql = "SELECT * FROM echipe WHERE id = ?";
$stm = $pdo->prepare($sql);
$stm->execute(array($id));
$row = $stm->fetchColumn();

echo $row['id'] . " " . $row['nume_echipa'];

使用 safeMysql

include 'safemysql.class.php';
$db = new Safemysql(array('db' => 'pariuri'));

$sql = "SELECT * FROM echipe WHERE id = ?i";
$row = $db->getRow($sql, $id)

echo $row['id'] . " " . $row['nume_echipa'];

关于php - 从 MySQL 数据库中按 id 显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17999210/

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