1

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

4

4 回答 4

1

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.

于 2012-08-07T13:50:00.583 回答
0
$data = array ();
while ( $row = ...)
{
    // appends the contents of $row to a new value in $data
    $data[] = $row;
}

print_r ($data); // <-- to see the output
于 2012-08-07T13:47:50.777 回答
0
$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.

于 2012-08-07T13:48:29.043 回答
0

Replace

echo $row['col1'];
echo $row['col2'];
echo $row['col3'];

with

$array = $row;

and return $array.

于 2012-08-07T13:50:08.153 回答