Displaying Results from Prepared Statements
<p>I have the following table:</p>

<pre><code>| id | name  | age |
|----|-------|-----|
| 1  | Peter | 23  |
| 2  | Amie  | 34  |
| 3  | Eddy  | 45  |
| 4  | Peter | 56  |
| 5  | Eddy  | 67  |
</code></pre>

<p>I now want to retrieve the age of all users with the name <code>Peter</code>.</p>

<p><strong>JS:</strong></p>

<pre><code>function getAge() {
    $.ajax({
        type : "POST",
        url : "backend.php",
        data : {
            q : "user",
            name : 'Peter'
        },
        success : function(data) {
            console.log(data)
            if (data.length &gt; 0) {

            } else {
                console.log("empty")
            }
        },
        error : function(jqXHR, status, error) {
            console.log(status, error);
        }
    });
}
</code></pre>

<p><strong>PHP:</strong></p>

<pre><code>if (isset ( $_POST )) {
   if ($_POST ['q'] == "user") {
        $name = $_POST ["name"];

        $stmt = $link-&gt;prepare ( "SELECT age FROM tab1 WHERE name= ?" );

        $stmt-&gt;bind_param ( "s", $name );
        $stmt-&gt;execute ();
        $stmt-&gt;store_result ();

        if ($stmt-&gt;num_rows &gt;= 1) {
            echo "Yes"; // only this Yes is being returned
            $arr = array ();
            while ( $stmt-&gt;fetch () ) {
                $arr [] = $name;
            }
            return $arr;
        } else {
            echo "0 records found";
        }

        $link-&gt;close ();
}
</code></pre>

<p>Instead of an array with <code>23</code> and <code>56</code>, I only get a <code>Yes</code>.</p>

<p>So how can I get the array with the ages?</p>