title: Why is my mysqli_fetch_assoc not grabbing the row info so I can insert details into my table?
body: <p>First off, I know about sql injection and that my code is not foolproof, prone to injection etc. Will be working on that next.</p>
  
  <p>Now : from my Android app to my PHP file I submit a JSON array of phone numbers like :</p>
  
  <pre><code>[{"phone_number":"+12345678"},
   {"phone_number":"+23456789"},
   {"phone_number":"34567890"},
   {"phone_number":"45678901"} 
   etc... etc...
  </code></pre>
  
  <p>These are contacts in my app user's phone. If these contacts are people who are also users of my app then I want to insert those numbers into my <code>contacts</code> table.</p>
  
  <p>But I can't get it to work. <code>mysqli_fetch_assoc</code> isn't working correctly. I don't know why.</p>
  
  <p>In my <code>contacts</code> table I have 3 columns - an <code>auto increment</code>, <code>user_id</code> and <code>contact_id</code>. The first two values are inserted correctly but the <code>contact_id</code> is always put in as '0', which is wrong. </p>
  
  <p>Here is my code : </p>
  
  <pre><code>require('dbConnect.php');
  
  //this is me, +567890123, my user_id in the user table
  $user_id = '20';
  
  //post all contacts in my phone as a JSON array
  $json  = $_POST['phonenumber'];
  $array = json_decode($json);
  
  foreach ($array as $value) {
      $phonenumber = $value-&gt;phone_number;
  
      $sql      = "SELECT username FROM user WHERE username = '$phonenumber'";
      $result   = mysqli_query($con, $sql);
      $num_rows = mysqli_num_rows($result);
  
      if ($num_rows &gt; 0) {
          echo "phonenumber is " . $phonenumber . "&lt;br&gt;";
  
          // we want to put $phonenumber in the contacts table, as one of +567890123 contacts
          // In the user table get the associated rows of $phonenumber
          while ($row = mysqli_fetch_assoc($result)) {
          // get the associated user_id in that row, that's what we want to put into the contacts table
              $contact_id                   = $row['user_id'];
              $insert_into_contacts_command = "INSERT INTO contacts VALUES(NULL, '$user_id','$contact_id')";
              $insert_into_contacts_table   = mysqli_query($con, $insert_into_contacts_command);
          }
  
      } //if +353864677745 is NOT in the user table...
      else {
  
          echo 'not a match.';
      }
  }
  </code></pre>