mysqli最快的方式只发布一个结果

时间:2013-02-05 17:04:09

标签: php mysqli fetch

我想知道是否可以在没有这么多参数和字符串的情况下执行以下脚本...我认为有很多可以删除...也许只有一个查询?

谢谢!

<?php
include("sqlvar.php");

$connection = new mysqli($host, $user, $pw, $db);
$marginidb = $connection->query("SELECT valore FROM DATIVENDITA WHERE parametro = 'margine'");
while($margine = $marginidb->fetch_object()) {
        $valoremargine = $margine->valore;
}
$connection->close();

echo $valoremargine;

$connection = new mysqli($host, $user, $pw, $db);
$ivadb = $connection->query("SELECT valore FROM DATIVENDITA WHERE parametro = 'iva'");
while($iva = $ivadb->fetch_object()) {
        $valoreiva = $iva->valore;
}
$connection->close();

echo $valoreiva;
?>

2 个答案:

答案 0 :(得分:1)

<?php
include("sqlvar.php");

$connection = new mysqli($host, $user, $pw, $db);

$marginidb = $connection->query("SELECT valore FROM DATIVENDITA WHERE parametro = 'margine' limit 1");
$margine = $marginidb->fetch_object();
echo $margine->valore;

$ivadb = $connection->query("SELECT valore FROM DATIVENDITA WHERE parametro = 'iva' limit 1");
$iva = $ivadb->fetch_object();
echo $iva->valore;

$connection->close();

?>

答案 1 :(得分:1)

以下是我认为你要求的内容:

<?php
include("sqlvar.php");
$result = array();
$connection = new mysqli($host, $user, $pw, $db);
$marginidb = $connection->query("SELECT valore FROM DATIVENDITA WHERE parametro = 'margine' OR parametro = 'iva'");
while($margine = $marginidb->fetch_object()) {
        array_push($result, $margine->valore);
}
$connection->close();
print_r($result);
?>

这样做是将结果存储为数组,以便访问您将使用的第一个结果$result[0],第二个结果将是$result[1]。您可以使用foreach循环循环这些结果。