1

I have a function to use but I don't know the content of this function. The only thing I know is that the function returns an array of associative arrays and the keys for the arrays. The data that the function returns come from a database. Can you help in how to read the data from this array? I am confused with the arrays. For now I am doing this:

$array = myfunction($var);
if(!empty($array))
{
    while($row = mysql_fetch_array($array))
    {
        print"$row[elem1]
        $row[elem2]";
    }
}

I take the error: Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in... I know that something is missing, but I till now I can't fix it.

2
  • 1
    Your version is so unnecessarily perplexed, I really have no idea where to start from... Commented Apr 6, 2012 at 11:52
  • Hi @Dr.Kameleon it is the same for me. I wrote what exactly I know. The part of code I know that is not correct but it is just a try. Commented Apr 6, 2012 at 12:15

4 Answers 4

2

If the function is returning an array then why are you using it in mysql_fetch_array. It is useless. Instead use this

foreach($array as $key => $value){

   echo $key;
   echo '<br>';
   echo $value;
}

This will print the whole array. Or a short method is

echo '<pre>';
print_r($array);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

Comments

1

Something like -

$array = myfunction($var);
foreach($array as $key => $row) {
    print"{$row['elem1']} {$row['elem2']}";
}

7 Comments

"The data that the function returns come from a database"
@webbandit it is not clear however whether or not the data has already been fetched. The asker has an array and seems to want to iterate over it.
@webbandit - OP clearly states that the function returns an array of arrays!
@nnichols no too clearly :) It states that "The data that the function returns come from a database" and uses mysql_fetch_array() function from one side and "an array of associative arrays and the keys for the arrays" from another. Not too clearly...
@webbandit - "The only thing I know is that the function returns an array of associative arrays" - seems quite clear to me.
|
0

You have to pass Resource Identifier to mysql_fetch_array() function.

Something like:

$sql = mysql_query('SELECT * FROM `table`');
if(!empty($array)) {

 while($row = mysql_fetch_array($array)) {
        print"$row[elem1]
        $row[elem2]";
 }
}

Comments

0

The error is correct .. the issue is not associative arrays in php but you are not using a valid mysql resource

Please see http://php.net/manual/en/function.mysql-fetch-array.php for documentation

Examples

$mysqli = new mysqli("localhost","root","","test");
$result = $mysqli->query("SELECT * FROM test");
$row = array() ;
echo "<pre>" ;
if($result->num_rows > 0)
{
    while($row = $result->fetch_array(MYSQLI_NUM))
    {
            print implode (",", $row) . PHP_EOL;

    }

}

Or

$array = myfunction($var);
if(!empty($array))
{
    foreach($array as $key => $row)
    {
        if(is_array($row))
        {
            print implode (",", $row) . PHP_EOL;
        }
        else
        {
            print $row . PHP_EOL ;
        }
    }

}

Comments

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.