I'm trying to go to my DB, get info from a bunch of columns based on two variables, and return all the values of all of those columns into a php list (or array), so for example, the final result would be something like $output = array($foo,$bar,$hello);
Here is the code I have right now to connect to the DB and my query, I know I am missing the vital code here, hoping someone can help me, thanks.
<? php
$var1 = 20.0;
$var2 = 15.0;
function getFromDB($var1, $var2){
mysql_connect("localhost", "test", "test") or die (mysql_error());
mysql_select_db("test") or die(mysql_error());
$queryString = "SELECT * from table1 WHERE foo=".$var1."AND bar=".$var2;
$go = mysql_query($queryString);
while($row = mysql_fetch_array($go)){
echo $row['col1'];
echo $row['col2'];
echo $row['col3'];
}
}
getFromDB($var1,$var2);
?>
The idea is to have the values of col1, 2, and 3 in an array. Thanks! Sam
Either one of these will work:
while($row = mysql_fetch_array($go)){
$output[] = array($row['col1'], $row['col1'], $row['col1']);
}
or:
while($row = mysql_fetch_array($go)){
$output[] = $row;
}
The one you should use depends on how the $row-data looks like.
$data = array ();
while ( $row = ...)
{
// appends the contents of $row to a new value in $data
$data[] = $row;
}
print_r ($data); // <-- to see the output
$array = array();
while ($row = mysql_fetch_array($go)) {
$array[] = array($row['col1'], $row['col2'], $row['col3']);
}
Please note that you should stop using mysql_*
functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this SO article .
Replace
echo $row['col1'];
echo $row['col2'];
echo $row['col3'];
with
$array = $row;
and return $array
.
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.