Hi,


Code below returns this.

Array
(
    [0] => 3
    [id] => 3
    [1] => John
    [name] => John
    [2] => Goog
    [surname] => Goog
)

Everything repeated. Why? Also, I need to serialize. I need it for some other reason.

$sql="SELECT * FROM friends WHERE id='3'";
	$runSql=mysql_query($sql);
	if(mysql_num_rows($runSql)>0){
		$newArray=mysql_fetch_array($runSql);
		return serialize($newArray);
	} else {
		return "ERROR: No friend found";
	}

echo "<pre>";
print_r(unserialize($response));

Thanks

Recommended Answers

All 4 Replies

The mysql_fetch_array function does this. It retrieves the table columns by id AND by name. You can use mysql_fetch_row instead.

it shouldn't bother u... u can use either id or the field name to serialize ur data... thats the way mysql_fetch_array works...

Hi,


Code below returns this.

Array
(
    [0] => 3
    [id] => 3
    [1] => John
    [name] => John
    [2] => Goog
    [surname] => Goog
)

Everything repeated. Why? Also, I need to serialize. I need it for some other reason.

$sql="SELECT * FROM friends WHERE id='3'";
	$runSql=mysql_query($sql);
	if(mysql_num_rows($runSql)>0){
		$newArray=mysql_fetch_array($runSql);
		return serialize($newArray);
	} else {
		return "ERROR: No friend found";
	}

echo "<pre>";
print_r(unserialize($response));

Thanks

I don't recommend using mysql_fetch_row though. This will return an array with numeric indices and the script will fail if you add a new column to your table.
I personally prefer using mysql_fetch_assoc . You can also use mysql_fetch_array($result,mysql_assoc) to get the records based on its associative names.

Hi,


Code below returns this.

Array
(
    [0] => 3
    [id] => 3
    [1] => John
    [name] => John
    [2] => Goog
    [surname] => Goog
)

Everything repeated. Why? Also, I need to serialize. I need it for some other reason.

$sql="SELECT * FROM friends WHERE id='3'";
	$runSql=mysql_query($sql);
	if(mysql_num_rows($runSql)>0){
		$newArray=mysql_fetch_array($runSql);
		return serialize($newArray);
	} else {
		return "ERROR: No friend found";
	}

echo "<pre>";
print_r(unserialize($response));

Thanks

mysql_fetch_array() can return the array in the associative, a numerical way or in both way. It has the third parameter called as '$result_type' and its default value is 'MYSQL_BOTH' , it can have other values like 'MYSQL_ASSOC' or 'MYSQL_NUM'.

commented: Specific an in depth help. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.