Trying to access array offset on value of type bool error

terinao :

i dont know whats wrong please help? 4 error message from while loop from assigning db data to arrray

$conn = new mysqli('localhost','root','','project_nlt');
$qry = "select * 
        from useri_nfo 
        where skills='$skill' 
        order by u_id ASC";
if ($row = $conn->query($qry)) {
    $numrow = $row->num_rows;
    $name = array($numrow);
    $profile_img=array($numrow);
    $skills = array($numrow);
    $address = array($numrow);
    $i = 0;
    echo $numrow;
    while ($r = $row->fetch_array(MYSQLI_ASSOC) && $i < $numrow) {
        echo $r;
        $name[$i] = $r['user_name'];
        $profile_img[$i] = $r['dp'];
        $skills[$i] = $r['skills'];
        $address[$i] = $r['address'];
        echo $name[$i];
        $i++;
    }
}
RiggsFolly :

In PHP you do not need to initialize arrays. PHP makes array usage extreemly simple

While loops will stop when a false is returned from the fetch, so no limit on rowcount is needed.

So amend the code to

$conn = new mysqli('localhost','root','','project_nlt');
$qry = "select * 
        from useri_nfo 
        where skills='$skill' 
        order by u_id ASC";
if ($row = $conn->query($qry)) {
    while ($r = $row->fetch_array(MYSQLI_ASSOC)) {
        //echo $r; you cannot echo an array, try `var_dump() or print_r()

        $name[]         = $r['user_name'];
        $profile_img[]  = $r['dp'];
        $skills[]       = $r['skills'];
        $address[]      = $r['address'];
    }
}

Your script is open to SQL Injection Attack. Even if you are escaping inputs, its not safe! You should consider using prepared parameterized statements in either the MYSQLI_ or PDO API's instead of concatenated values

So this should really be coded as

$conn = new mysqli('localhost','root','','project_nlt');
$qry = "select * 
        from useri_nfo 
        where skills = ?  
        order by u_id ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $skills)
$stmt->execute();
$result = $stmt-<get_reslt();

while ($r = $result->fetch_array(MYSQLI_ASSOC)) {
    //echo $r; you cannot echo an array, try `var_dump() or print_r()

    $name[]         = $r['user_name'];
    $profile_img[]  = $r['dp'];
    $skills[]       = $r['skills'];
    $address[]      = $r['address'];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Trying to access array offset on value of type bool in PHP 7.4

Warning: Trying to access array offset on value of type bool

Notice: Trying to access array offset on value of type bool on line 83

PHP 7.4 Notice : Trying to access array offset on value of type bool in

PDO Trying to access array offset on value of type bool

How to fix Trying to access array offset on value of type bool?

How to solve PHP ACF error: "Trying to access array offset on value of type bool"?

Custom Class for a plugin options give error, Notice: Trying to access array offset on value of type bool in C

DATE_FORMAT in query generate this error: Notice: Trying to access array offset on value of type bool in

Error : Trying to access array offset on value of type int

mySQL error 'Trying to access array offset on value of type null'

error : Trying to access array offset on value of type null ( php 8 )

Get Error "Warning: Trying to access array offset on value of type null in"

PHP 8.1 Warning: Trying to access array offset on value of type bool in array construction

Trying to access array offset on value of type null

Trying to access array offset on value of type null in

Trying to access array offset on value of type bool - Laravel API json response

Problems receiving data from a record for update operation (Trying to access array offset on value of type bool)

how can i solve "Trying to access array offset on value of type int" error in php

Codeigniter 4 while unlinking shows the error: Trying to access array offset on value of type null

How to solve error: Trying to access array offset on value of type null with Prepared Statement PHP

Error: Trying to access array offset on value of type null in my PHP code

php 7.4 xampp Trying to access array offset on value of type null

Trying to access array offset on value of type null in laravel

php Notice: Trying to access array offset on value of type null in

Trying to access array offset on value of type null...in login details

Notice: Trying to access array offset on value of type null in line(static)

Message: Trying to access array offset on value of type null

Laravel 8 ErrorException: Trying to access array offset on value of type null