0

I've created the following code but for some reason it is echoing Array instead of the result:

<?php
    include("../config.php");
    include("functions.php");

    $count = "SELECT `monthly_slots` FROM users WHERE `username` = 'Bill'";
    $count = mysql_query($count);
    $data = mysql_fetch_assoc($count);
    echo "$data";
?>

Any ideas?

I have no idea why it is outputting Array because there should only be one result from that query.

6 Answers 6

2

mysql_fetch_assoc() returns an array, you need to use print_r($data) instead, to dump out the array's contents.

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

1 Comment

also, you might want to echo "<pre>" before and "</pre>" after the print_r to nicely format it in the browser
2

try this

print_r($data);

This will output the contents of the array.

The return type of mysql_fetch_assoc is array. So you should use print_r to see the result.

Returns an associative array that corresponds to the fetched row and moves the internal data
pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC 
for the optional second parameter. It only returns an associative array.

Comments

1

As the array manual page explains, when you output an array variable in string context (as echo does here) it will become just "Array".

To see the array contents use print_r or var_dump instead:

print_r($data);

Or better yet just access the content you wanted:

print($data["monthly_slots"]);

Comments

0

use print_r($data) to print the $data array, or $data['column_name'] for a perticular row. because mysql_fetch_assoc() aloways returns an array.

Comments

0

Solutions

  • You could simply print out the whole contents of the array with

    print_r($data)

however I wouldn't usually recommend doing it like that considering you're only looking for a specific item soo..

  • You could also try referencing the output as the first item (assuming there is only one result) with

    echo "$data[0]";.

  • you can also simply reference the specific item in the array you are looking for with

    echo $data['monthly_slots'].

References:

Comments

0

It is expected. You should try print_r($data)

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.