警告:pg_num_rows() 期望正好 1 个参数,在 PHP 中给出 0


Warning: pg_num_rows() expects exactly 1 parameter, 0 given in PHP

我的代码中出现以下错误

警告:pg_num_rows(( 期望正好 1 个参数,0 在第 52 行的/var/www/funcoes.php 中给出

这是函数的代码:

function cadastrarFornecedor($nome){
    $qry = "INSERT INTO public.fornecedor (nome) VALUES ('".$nome."')";
    $result = pg_query($qry);
    if(pg_num_rows() > 0){
        return 1;
    }
    else{
        return 0;
    }
}

pg_num_rows将结果集变量作为参数。该错误告诉您需要参数,但没有提供任何参数。

请参阅签名:int pg_num_rows ( resource $result )

其中,$result:

PostgreSQL 查询结果资源,由 pg_query(( 返回 pg_query_params(( 或 pg_execute(( (等(

因此,您的代码应将查询结果集传递给 pg_num_rows ,如下例所示:

function cadastrarFornecedor($nome){
    $qry = "INSERT INTO public.fornecedor (nome) VALUES ('".$nome."')";
    $result = pg_query($qry);
    if(pg_num_rows($result) > 0){
        return 1;
    } else{
        return 0;
    }
}

来源: http://www.php.net/manual/en/function.pg-num-rows.php

(请注意,如果您插入具有DO INSTEAD触发器的视图、具有分区触发器/规则的表等,pg_num_rows将返回零。

警告:pg_num_rows(( 期望正好有 1 个参数,0 在第 52 行的/var/www/funcoes.php 中给出

错误不言自明

function cadastrarFornecedor($nome){
  $qry = "INSERT INTO public.fornecedor (nome) VALUES ('" . $nome . "')";
  $result = pg_query($qry);
  if (pg_num_rows($result) > 0) {
      return 1;
  }
  else {
      return 0;
  }
}