简体   繁体   中英

Why am I getting this array to string conversion error?

Im trying to take the first element in an array and insert it into mysql or, if the array is empty, display a message saying the info is unknown. I keep getting this array to string error and its not updating mysql. Here's what I have

$phone_number_parser = preg_match_all("/([0-9]?[- .(]*[0-9]{3}[- .)][0-9]{3}[- .][0-9]{4})/", $result['adtext'], $phone_number);

    var_dump($phone_number[0]); 
    //go to the next row
    if (empty($phone_number))
    {
    $query2 = "UPDATE usedcars SET phonenumber = 'UNKNOWN' WHERE `key` = $x";
    echo "<font  color='#FF0000'>PHONE NUMBER UNKNOWN</font><br>";
    mysqli_query($conn, $query2);
    }
    else
    {

    $query3 = "UPDATE usedcars SET phonenumber = ('$phone_number[0]') WHERE `key` = $x";
     echo "<font  color='#00FF00'>INSERTING NUMBER $phone_number[0] INTO DATABASE</font><br>";
     mysqli_query($conn, $query3);
     }

    $x++;
    }

not only am I getting this error and not updating mysql values, the display message is showing the echo from query3 whether the array is empty or not. What am I doing wrong here?

You are comparing wrong values.

var_dump($phone_number[0]); 
//go to the next row
if (is_array($phone_number[0]) && count($phone_number[0]) == 0)  // check if an array...
{
   //..codes goes here...
}
else
{

    $query3 = "UPDATE usedcars SET phonenumber = (' ".$phone_number[0][0]." ') WHERE `key` = $x";
    echo "<font  color='#00FF00'>INSERTING NUMBER $phone_number[0][0] INTO DATABASE</font><br>";
    mysqli_query($conn, $query3);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM