Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have looked at several posts on stackoveflow for creating an array and haven't found what I was looking for yet...so many different answers I would like to attempt and get one clear that is closest to what I am trying to accomplish.

Say I want to create an array using this query:

"select * from products"

How is this accomplished most efficiently?

Thank you.

PS - note that I am starting from scratch.

UPDATE

My config file:

`[root@CentOS testphp]# vi config.php
<?php
// Connection's Parameters
$db_host="localhost";
$db_name="tablename";
$username="username";
$password="password";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
?>`

The php code I am trying to create the array with...I am getting a Syntax Error:

Parse error: syntax error, unexpected T_VARIABLE in /var/www/html/testphp/test.php on line 2

I get the same error:

`[root@CentOS testphp]# vi test.php
<?php include('config.php')
$query = "select * from upload_products";
$dataArray = array();
$result = mysqli_query($query, $link);
while($row = mysqli_fetch_assoc($query)) {
$dataArray[] = $row;
}
var_dump($dataArray); //Now you have your array.
?>`

Any ideas...It feels like I am missing something here. And, yes I have read the documentation...used their code line by line, reviewed the code and still get the same error as I do with all the code examples I have found on the web.

share|improve this question

closed as not a real question by deceze, Robert Harvey Jun 18 '12 at 15:07

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
you should read php manual: php.net/manual/en/index.php, search about sql querying –  LeleDumbo Jun 18 '12 at 9:02
1  
If you are confused by the many different answers, asking yet another broad question won't help. What specific problem do you have with any of the existing solutions? –  deceze Jun 18 '12 at 9:03
    
Homework question? You seem to need to start with PHP before you delve into MySQL. Read The Manual. –  Clancy Hood Jun 18 '12 at 9:06
1  
What is it that you have trying to accomplish? Post your code if you have tried something. Too broad a question... MySQLi/PDO has several functions for the same...php.net/manual/en/mysqli-result.fetch-array.php they each come with sample code.... –  verisimilitude Jun 18 '12 at 9:07

4 Answers 4

There is not definite way to convert a database result object into an associative array directory. You will have to create this array your result.

$query = "...";
$dataArray = array();
$result = mysqli_query($query, $link);
while($row = mysqli_fetch_assoc($query)) {
    $dataArray[] = $row;
}
var_dump($dataArray); //Now you have your array.
share|improve this answer

You can use:

$query = mysqli_query("SELECT * FROM products");
$result = mysqli_fetch_assoc($query);

but you will receive only one row in $result variable. If you want to fetch all you have to apply any loop on it: e.g.:

$query = mysqli_query("SELECT * FROM products");
while($result = mysqli_fetch_assoc($query)){
 //do something with your results 
 //you can access result like: $result['column_name']
}

Is it clear for you now?

share|improve this answer
1  
Please stop advertising the deprecated mysql_ functions. –  deceze Jun 18 '12 at 9:06
    
Assuming OP even has a database, how do they connect to it? –  user1416258 Jun 18 '12 at 9:06
    
@deceze: Was just gonna post the same! :) –  verisimilitude Jun 18 '12 at 9:07
    
@emte - I am getting this error: Parse error: syntax error, unexpected T_VARIABLE in /var/www/html/jmdb1/test4.php on line 5 <<line 5 is the line with the query in it. Any ideas? –  lcm Jun 18 '12 at 9:18
    
sorry changed from mysql to mysqli. Of course it's better to user OOP... –  emte Jun 18 '12 at 9:20

You can use mysql_fetch_assoc or mysql_fetch_array to get the mysql query response as an array. Following are the code example of how to use both.

With mysql_fetch_assoc :

$query = "select * from products";
$result = mysql_query($query);
if (mysql_num_rows($result) != 0) {
   while ($row = mysql_fetch_assoc($result)) {
          //every row will represent a record as an array
          // your code   
   }
}

With mysql_fetch_array :

 $query = "select * from products";
 $result = mysql_query($query);
 if (mysql_num_rows($result) != 0) {
       while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
           //every row will represent a record as an array
           //  your code
      }
 }

If you have any doubt further, please refer php manual.

share|improve this answer
    
I tried example one on this page: php.net/manual/en/function.mysql-fetch-assoc.php and am getting this error: Parse error: syntax error, unexpected T_STRING in /var/www/html/jmdb1/test6.php on line 20 ....any thoughts? –  lcm Jun 18 '12 at 17:17
    
see, It is syntax error. PHP is not able to parse your syntax.. so go through your code deeply. BTW what code is written on line no. 20 –  Nishu Tayal Jun 18 '12 at 17:19
    
I guess where I am confused is that I am using a link that was provided by someone answering the question. That link happens to be on the php.net site and its not MY code per se. I can review it, but why would there be a syntax error in the first place? exit; is the only thing on line 20. –  lcm Jun 18 '12 at 17:24
    
php.net/manual/en/function.mysql-fetch-assoc.php Example #1 –  lcm Jun 18 '12 at 17:25
    
Please check, whether your mysql extension is loaded or not in PHP.ini. If it is loaded, then check the mysql connection in your code. This is the only way to debug the your testcode, as even i am not able to figure why your code is throwing the error, until i can review your code.. –  Nishu Tayal Jun 18 '12 at 17:32

Try this

@ $db = new mysqli('localhost', 'username', 'password', 'dbname');

  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }

  $query = "select * from books";
  $result = $db->query($query);

  $num_results = $result->num_rows;

  echo "<p>Number of books found: ".$num_results."</p>";

  for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();
    // echo something here
  }

  $result->free();
  $db->close();
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.