从php的sql查询获得的行数

时间:2016-09-25 08:03:34

标签: php html sql oracle

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Untitled</title>

</head>

<body>

<?php
// extract all the form fields and store them in variables
$WineType=$_POST['WineType'];
$Pairing=$_POST['Pairing'];
$PriceRange=$_POST['PriceRange'];

// the table will always have at three columns
$tablewidth = 600;

$sqlstatement = "SELECT * FROM wine1";

  /* Set oracle user login and password info */
  $dbuser = "xxxx";  /* your login */
  $dbpass = "xxxx";  /* your password */
  $db = "SSID"; 
  $connect = oci_connect($dbuser, $dbpass, $db); 

  if (!$connect)  {
    echo "An error occurred connecting to the database"; 
    exit; 
  }

  /* build sql statement using form data */
  $query = $sqlstatement; 

  /* check the sql statement for errors and if errors report them */
  $stmt = oci_parse($connect, $query); 

echo "SQL: $query<br>";

  if(!$stmt)  {
    echo "An error occurred in parsing the sql string.\n"; 
    exit; 
  }
  oci_execute($stmt);


$howmany=0;
while(oci_fetch_array($stmt))  {
$howmany=$howmany+1;
}

echo "Number of items: $howmany<br>";
?>

  <?php
  oci_close($connect); 
  ?>

</body>
</html>

您好,对于上面的代码。我正在尝试计算当我运行“SELECT * from wine1;”时从oracle服务器返回的行数。查询。

运行代码后,$ count似乎为零

我已经确认,在SQL服务器本身提交查询时,我能够检索存储在表中的所有六个项目。它似乎没有在PHP中工作。

3 个答案:

答案 0 :(得分:5)

在您的情况下使用

$numrows = oci_fetch_all($stmt, $res);
echo 'Total rows: '.$numrows;

用于迭代所有记录使用此

foreach($res as $rows) { 
  var_dump($rows); 
}

参考http://php.net/manual/en/function.oci-fetch-all.php

答案 1 :(得分:1)

您可以使用

int $mysqli_result->num_rows;

它返回结果集中的行数。

参考 - http://php.net/manual/en/mysqli-result.num-rows.php

答案 2 :(得分:1)

尝试像这样修改你的while循环

$howmany=0;
while (($row = oci_fetch_array($stmt, OCI_ASSOC)) != false) {
  $howmany=$howmany+1;
}

有一种简单的方法可以找到总行数。

$howmany = oci_num_rows($stmt);

希望这会有所帮助。