0

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?

1
  • 2
    This guy @Riad is a beast(euphemism for awesome coder) who held my little newb hand and walked me though my code. MUCH RESPECT AND THANKS! Commented Nov 16, 2014 at 10:46

1 Answer 1

1

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);
 }
Sign up to request clarification or add additional context in comments.

7 Comments

That fixed the display strings but Im still getting the error and mysql still not updating @Riad
Notice: Array to string conversion in...and these are the lines $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); @Riad
try to echo $query3 ..and see the result... also what does var_dum($phone_number[0]) shows?
( ! ) Notice: Array to string conversion in C:\wamp\www\blahblah\usedcarnumberextractor.php on line 65 Call Stack # Time Memory Function Location 1 0.0156 249056 {main}( ) ..\usedcarnumberextractor.php:0 INSERTING NUMBER Array INTO DATABASE array (size=0) empty PHONE NUMBER UNKNOWN array (size=0) empty PHONE NUMBER UNKNOWN array (size=1) 0 => string '-347-637-8795' (length=13) this a sample. Btw, as you can see sometimes it will show the NUMBER UNKNOWN string even when the array contains a phone number. How did that happen? @Riad
paste the echo $query3 output...and append the result of var_dump($phone_number[0]) to the question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.