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'm trying to get value from database, here is my code:

function client() {
    $code=$_GET['actvcode'];
    $db = mysql_connect('localhost','root','0000');

    if (!$db) die("Error connecting to MySQL database.");

    mysql_query('SET NAMES utf8');
    mysql_select_db("test" ,$db);
    $query = 'SELECT client FROM cmum_codes WHERE code="'. mysql_real_escape_string( $code ) .'"';

    if (!($result = @mysql_query($query))) {
        die(mysql_error());
    }

    $str = $result;
    return $str;
}

I always get the word "Array", what is wrong?

share|improve this question
    
I imagine you're doing this echo client(); is that the case? –  Dale Jun 9 '13 at 22:08
    
yes i'll get name of client from actvcode –  Ashraf Damra Jun 9 '13 at 22:15
    
Thanks for all Joust I've Toggle $str = $result; return $str; with $rows = mysql_fetch_array($result); return $rows['client']; –  Ashraf Damra Jun 9 '13 at 23:22
add comment

3 Answers

up vote 1 down vote accepted

Assuming you're only expecting one result (in which case you should add LIMIT 1 to your MySQL query), you must use mysql_fetch_array:

return mysql_fetch_array($result);

Which will return the row, so when you call the client function:

$client = client();
echo $client['field_you_want_to_echo'];

Of course, you should look into the mysqli extension instead of the deprecated mysql.

share|improve this answer
    
function client() { $code=$_GET['actvcode']; $db = mysql_connect('localhost','root','0000'); if(!$db) die("Error connecting to MySQL database."); mysql_query('SET NAMES utf8'); mysql_select_db("test" ,$db); $query = 'SELECT client FROM cmum_codes WHERE code="'. mysql_real_escape_string( $code ) .'"'; if (!($result = @mysql_query($query))) { die(mysql_error()); } return mysql_fetch_array($result); $client = client(); echo $client['client']; } –  Ashraf Damra Jun 9 '13 at 22:30
add comment

mysql_query doesn't return your resultset, it returns a MySQL resource. You need to then use mysql_fetch_array. You shouldn't be using any of the mysql_ functions as they're deprecated. Instead, use either MySQLi or PDO.

share|improve this answer
add comment

You must use mysql_fetch_array($resul) and add it on a variable for exemple $array and use ... foreach to get result
Foreach($array as $str){ echo $str; }

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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