Home > database >  php count error while getting data from database
php count error while getting data from database

Time:06-12

i am geting the following error : Warning: count(): Parameter must be an array or an object that implements Countable in E:\system\xamp\htdocs\project\php tests\index.php on line 12

the code is as follows:

<?php 
if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $update = true;
    $record = mysqli_query($db, "SELECT * FROM data WHERE id=$id");

    if (count($record) == 1 ) {
        $n = mysqli_fetch_array($record);
        $name = $n['name'];
        $address = $n['address'];
    }
}

?>

how can i resolve this . kindly help

CodePudding user response:

this worked for me ;

<?php 
if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $update = true;
    $result = mysqli_query($db, "SELECT * FROM data WHERE id='$id' LIMIT 1");
    
if($result){
    if($result && mysqli_num_rows($result)>0){
        $n=mysqli_fetch_assoc($result);
  $name = $n['name'];
        $address = $n['address'];
    }
}
   
}
?>

CodePudding user response:

<?php 
if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $update = true;
    $record = mysqli_query($db, "SELECT * FROM data WHERE id=$id");
    $n = mysqli_fetch_array($record);
    if (count($record) == 1 ) {
        $name = $n['name'];
        $address = $n['address'];
    }
}
?>
  • count() use array not string
  • mysql_fetch_array() outside of if condition
  •  Tags:  
  • php
  • Related