从MySQL数据库按ID显示项目

时间:2013-08-01 16:09:59

标签: php mysql database

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 vs 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);
?>

1 个答案:

答案 0 :(得分:0)

使用原始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'];