0
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);
?>
4

1 回答 1

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'];
于 2013-08-01T16:29:52.727 回答