0

I want to pass the array of values fetched from the database as a parameter to a php function.If I echo the value, it doesn't display it.

      <?php
  include "config.php";
  $sql="select * from project where comp_id='1'";
  $sql1=mysql_query($sql);
  $rows=array();
  while( $fet=mysql_fetch_array($sql1))
  {
   $rows[]=$fet;
   }

  echo fun_parameter($rows);
  function fun_parameter($rows)
   {
     echo" rows". $rows['start_date']."values";   //  not working
   }
    foreach($rows as $row)
     {
     echo $name=$row['start_date'];   /working

     } 

   ?>
manasi sakhare
  • 1,051
  • 7
  • 18
  • post $rows values to see what is coming in the array – Abdul Manan May 19 '15 at 05:35
  • 1
    row is a multi-dimensional array use print_r($rows) and see the result. If you want to print then process with loop as it is there in foreach. – Sundar May 19 '15 at 05:38
  • $rows shows the word 'Array' –  May 19 '15 at 05:38
  • @sundar print_r($rows) display the array values like Array ( [0] => Array ( [0] => 1 [p_id] => 1 [1] => 1 [e_id] => 1.how to extrasct p_id value from this. –  May 19 '15 at 05:41
  • please find the answer – Sundar May 19 '15 at 05:47
  • Passing the value to the function is not the problem. Try to think harder if you want to become a good programmer. – Jake Opena May 19 '15 at 05:48
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 19 '15 at 12:47

2 Answers2

0

Simple looping can give the result,

 function fun_parameter($rows)
   {
     foreach($rows as $row)
     {
        echo $row['p_id'];   /working
    echo" rows". $row['start_date']."values";
     } 

     //echo" rows". $rows['start_date']."values";   //  not working
   }

If you want to fetch particular column from multi dimensional array then take a look into array_column() in php

Sundar
  • 4,580
  • 6
  • 35
  • 61
-1

in your code you are trying to echo an array $row without its index specified, you need to loop through that array as it has got mulitple value, use the following code:

<?php
  include "config.php";
  $sql="select * from project where comp_id='1'";
  $sql1=mysql_query($sql);
  $rows=array();
  while( $fet=mysql_fetch_array($sql1))
  {
   $rows[$i]['start_date']=$fet['start_date'];
   }

  echo fun_parameter($rows);
  function fun_parameter($rows)
   {
   foreach($rows as $row)
     {
    echo" rows". $row['start_date']."values";   //  use loop it will work

     }

   }
    foreach($rows as $row)
     {
     echo $name=$row['start_date'];   // i left this foreach here because you had it in your code    
     } 
Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33