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
  • 2
    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. Commented Aug 7, 2012 at 13:47
  • The already are in an array. $row is an array. If you want a multidimensional array containing all rows, use $rowset[] = $row inside the while loop Commented Aug 7, 2012 at 13:48
  • 1
    You put those variables in an array just as you would put any variable into an array. How do you normally do it. The answer to your question is in your question! And as @Matt pointed out you should be using a better database connection method. Your code is likely vulnerable to SQL Injection Commented Aug 7, 2012 at 13:48
  • thanks guys, will also look into new methods of connection Commented Aug 7, 2012 at 13:54

4 Answers 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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot, and thank you to all the other responses as well
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

Comments

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.

1 Comment

Why was this down-voted? It's the correct answer AND will help OP to improve his code moving forward.
0

Replace

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

with

$array = $row;

and return $array.

2 Comments

Why not $array[] = $row? If you're going to preserve the indices, this is how you would do it efficiently.
Thanks, I didn't even think of that... it's been a while since I've coded in PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.