I would like to add results into array and print on screen.
Here’s my code and nothing is printing…could someone help me take a look.
include('config.php');
$con = mysql_connect($host, $username, $password);
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db('members', $con) or die(mysql_error()) ;
$sql = "SELECT * FROM people where status like '%married%' ";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$result_array[] = $row['id']; // <-------**here**
}
return $result_array;
echo($result_array);
mysql_close($con);
Firstly, change:
To:
Then remove
return $result_array;– you don’t need it and when used outside of a function it just halts execution of the script.Finally, change
to:
EDIT: Some additional thoughts:
You don’t need parentheses around the argument to
echo– it’s actually more efficient to leave them out:echo $var;is quicker thanecho($var);If you’re only ever going to use the
idcolumn, then don’t select the whole row: useSELECT id FROMinstead ofSELECT * FROM.Are you sure you need the wildcards either side of “married”? You may well do (depends on what the possible values of
statusare), but you probably don’t. SoMay be better,