-3
<?php
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error()); 
$result = mysql_query("SELECT <column name> FROM <table name>"); 
$data=array();  // to create an array to store result
while ( $row= mysql_fetch_array( $result ))
   {
    $data = $row;
   }

?> 

This query returns multiple lines of result like:

abc
def
ghk
...
...
...
...
...

My question is how can I change the looping structure so that each row from output value is feed to the parser one by one.

Using the code snippet above, only one is feed to the parser. But I want each row to be feed to parser one by one.

I found the solution. It works.

<?php 
 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); 
 mysql_select_db("Database_Name") or die(mysql_error());  
$result = mysql_query("SELECT <column name> FROM <table name>");  
while ( $row= mysql_fetch_array( $result ))  
{  
echo $row['column name']; // to check does it works fine. 
// pass the variable " $row['column name'] " as paramter to parser function it works.  
//I have tried it. 
}  

?>  

I was using wrong way to input in a array. The best way is to pass directly variable inside the loop to parser function. Then we wouldn’t need any external array to store the results.

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
Rakshit
  • 13
  • 1
  • 3
  • 1
    The mysql extension is outdated and on its way to deprecation. New code should use mysqli or PDO, both of which have important advantages, such as support for prepared statements. Note that `or die(mysql_error())` should never appear in production code, as [`die`](http://www.phpfreaks.com/blog/or-die-must-die) breaks HTML output and database error messages should never be revealed to non-admin users as it [discloses too much information](http://msdn.microsoft.com/en-us/library/ms995351.aspx#securityerrormessages_topic2). A better approach would be to properly implement error handling. – outis Jul 22 '12 at 08:09
  • what parser? What is it's input. where do you get stuck. Did you examin `$row`? – Nanne Jul 22 '12 at 08:11
  • you are only retrieving the data, where's the input? – Mr. Alien Jul 22 '12 at 08:11
  • possible duplicate of [PHP mySQL - Can you return an associated array with a number index?](http://stackoverflow.com/q/339371/), [Store and display MySQL result to/from PHP array](http://stackoverflow.com/q/7817338/), [For each result in MySQL query, push to array (complicated)](http://stackoverflow.com/q/3047896/), [mysql query result into php array](http://stackoverflow.com/q/8970117/), ... The list goes on. – outis Jul 22 '12 at 08:12
  • Perhaps you can use JSON array to parse the query – gkris Jul 22 '12 at 08:13
  • 1
    @gkris: JSON is for data interchange. It's not relevant it here. – outis Jul 22 '12 at 08:16

2 Answers2

2

My take at your question. Its kinda difficult to understand what you are asking.

how can I change the looping structure so that each row from output value is feed to the parser one by one.

Where is the parser?

<?php
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error()); 
$result = mysql_query("SELECT <column name> FROM <table name>"); 
$data=array();  // to create an array to store result
while ( $row= mysql_fetch_array( $result ))
{
    //here you did the mistake if you wanted to store it in a array (like you indicated in the above comment)
    $data[] = $row;


    // Pass anything to the parser here
}

// Or here you can pass it to the parser
// parser($data);

?>

Only one is feed to the parser. BUt I want each row to be feed to parser one by one.

Ok, this one I assume says this:

Only a single row is fed to the parser (due to the error I showed you above). You want to process all the rows

Here is the solution:

<?php
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error()); 
$result = mysql_query("SELECT <column name> FROM <table name>"); 

$answer = array();  // to create an array to store result

while ( $row= mysql_fetch_array( $result ))
   {

    $answer[] = parser($row);
   }

?>
footy
  • 5,803
  • 13
  • 48
  • 96
0

Use PDO and use fetchAll()

$pdo_options = array(
    PDO::ATTR_PERSISTENT => true,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
    );
$dataBase = new PDO('mysql:host=ipAddress;dbname=yourDBname', 'user', 'password', $pdo_options);


$request = $dataBase->prepare('SELECT col1, 
col2 
FROM table1 
WHERE colx = :var1');

$request->execute(array('var1' => $_POST['var1']));
$results = $request->fetchAll();
$request->closeCursor();
Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81