0

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

5
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by network18
0

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

-1

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

0

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.

0

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'.

Votes + Comments
Specific an in depth help.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.