(PHP) What variable I put inside mysqli_affected_rows

0
  <?php
$conexao = mysqli_connect("localhost","root","","banco");

$nome = isset($_REQUEST['nome'])?$_REQUEST['nome']:"";
$telefone = isset($_REQUEST['telefone'])?$_REQUEST['telefone']:"";
$sql = "insert into tb_banco(nome, telefone)values('$nome','$telefone')";
$salvar = mysqli_query($conexao,$sql);

$registro = mysqli_affected_rows($salvar);

if($registro==0){
    echo "telefone já cadastrado,use outro";
}else{
    echo "Cadastro realizado com sucesso";
}

?>
    
asked by anonymous 11.06.2018 / 22:35

1 answer

3

According to the PHP documentation on link :

int mysql_affected_rows ([ resource $link_identifier = NULL ] )

This is a function that returns an integer value containing "the number of rows affected by the last INSERT, UPDATE, REPLACE, or DELETE associated with link_identifier ."

link_identifier : in turn is the connection to the mysql database. This argument is not required when you only have a MySQL database connection.

Bringing this to your example would be:

$conexao = mysqli_connect("localhost","root","","banco");

$nome = isset($_REQUEST['nome'])?$_REQUEST['nome']:"";
$telefone = isset($_REQUEST['telefone'])?$_REQUEST['telefone']:"";
$sql = "insert into tb_banco(nome, telefone)values('$nome','$telefone')";
$salvar = mysqli_query($conexao,$sql);

$registro = mysqli_affected_rows($conexao);

if($registro==0){
    echo "telefone já cadastrado,use outro";
}else{
    echo "Cadastro realizado com sucesso";
}
    
11.06.2018 / 22:43