double results in my array ( mysql_fetch_array )
From the manual:
mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
By default, mysql_fetch_array
gives both associative and numeric indexes. You don't want this. You can limit it with the second parameter:
$query_result_array = mysql_fetch_array($query_result, MYSQL_NUM); // numeric keys only
$query_result_array = mysql_fetch_array($query_result, MYSQL_ASSOC); // associative keys only
You can also use mysql_fetch_row
to only get numeric keys, or mysql_fetch_assoc
to only get associative keys.
$query_result_array = mysql_fetch_row($query_result); // numeric keys only
$query_result_array = mysql_fetch_assoc($query_result); // associative keys only
mysql_fetch_array returns duplicate data
This is the intended functionality of mysql_fetch_array()
. If you want to not have the "duplicates" and just have the associative-array, use mysql_fetch_assoc()
instead.
Example:
while ($row = mysql_fetch_assoc($data)) { ... }
Why mysql_fetch_array outputs multiple duplicate results
That's the normal behavior of mysql_fetch_array
. It provides both numerical and associative indices.
If you want only one of them, use mysql_fetch_assoc
or mysql_fetch_row
$result=mysql_fetch_row($rslt);
Array
(
[0] => 7
[1] => 260
)
or
$result=mysql_fetch_assoc($rslt);
Array
(
[m_id] => 7
[work] => 260
)
It's also worth mentioning that you can get this behavior using mysql_fetch_array
by passing a second argument.
// same as mysql_fetch_row
$result=mysql_fetch_array($rslt, MYSQL_NUM);
and
// same as mysql_fetch_assoc
$result=mysql_fetch_array($rslt, MYSQL_ASSOC);
More info: http://php.net/manual/en/function.mysql-fetch-array.php
Double data result mysql_fetch_array
You need to reinitialize the datajempoler2 array or it is retaining the older value and adding new elements to it.
<?php
while(my looping data) {
datajempoler2 = array(); // this line need to be added
$selectorang2 = mysql_query("SELECT likes.*, user.* FROM likes, user WHERE
likes.uid_fk = u ser.uid AND
likes.idstatus_fk = $data[idstatus]");
while ($peoplelike = mysql_fetch_assoc($selectorang2)) {
$datajempoler2[] = $peoplelike['nama'];
}
echo count($datajempoler2);
}
?>
Fetch array function doubling values in each position of array?
This is not an error, it's the intended behavior of mysql_fetch_array()
http://us.php.net/manual/en/function.mysql-fetch-array.php
You can either set the result_type
flag, or you can just use mysql_fetch_assoc()
instead. http://us.php.net/manual/en/function.mysql-fetch-assoc.php
Use query twice mysql_fetch_array
$stuff = mysql_query("SELECT * FROM users");
while($s = mysql_fetch_array($stuff)){
# ....
}
// add this line
mysql_data_seek( $stuff, 0 );
while($r = mysql_fetch_array($stuff)){
# ...
}
Should do the trick
Another way is of course to store your result in an array and reuse that
PHP adds mysql-results double to array.
There is no function like fetch_array()
in PHP, I think you're using a predefined MySQL class which internally calls mysql_fetch_array()
, so check the class, actually you are getting correct result but by using MYSQL_BOTH
(default), you'll get an array with both associative and number indices.
Use
$row = mysql_fetch_array($result, MYSQL_ASSOC));
Or
$row = mysql_fetch_array($result, MYSQL_NUM));
You must have forgotten to write a argument with fetch_array()
.
reference
More than one mysql_fetch_array()
The operator precedence of &&
is higher than =
. So, put parenthesis around the two parts and try this:
while(($row1 = mysql_fetch_array($result2)) && ($row2 = mysql_fetch_array($result4)))
PHP mysql_fetch_array/Row not displaying Unique elements
I do not know what is in your tables and what duplicates you are referring to. Because you got trashed so bad, I risked getting trashed helping you with unconventional code.
The comments about using depreciated mysql had no bearing on your code but they just don't like others that come here for a solution to think it is OK to use mysql. They do not realize when you post a question how frustrated you are and how they are adding to your frustration. They mean well but in your case you changed your code to accommodate them and you not realizing it had nothing to do with a solution.
NOTE: this solution uses depreciated mysql and not the newer mysqli
This will eliminate duplicates:
while ($row = mysql_fetch_array($results, MYSQL_NUM)) {
$data[] = serialize(array($row[1],$row[2],$row[3],$row[4],$row[5]));
}
$data = array_unique($data);
To try and keep the first duplicate:
while ($row = mysql_fetch_array($results, MYSQL_NUM)) {
$data[] = serialize(array($row[1],$row[2],$row[3],$row[4],$row[5]));
}
$data = array_reverse ($data);
$data = array_unique($data);
ksort($data); // puts them back in original order.
OR
while ($row = mysql_fetch_array($results, MYSQL_NUM)) {
$columns = serialize(array($row[1],$row[2],$row[3],$row[4],$row[5]));
if (!in_array($columns,$data)){
$data[] = serialize(array($row[1],$row[2],$row[3],$row[4],$row[5]));
}
}
Then the new loop is:
foreach($data as $key => $value){
$row = unserialize($value);
}
There are other ways to use this to eliminate some dups and exclude others.
$uniqueRows = serialize(array($row[1],$row[2]));
$data[$uniqueRows] = array($row[3],$row[4],$row[5]);
Related Topics
What Does the B in Front of String Literals Do
How to Dynamically Build Queries with Pdo
Display Custom Order Meta Data Value in Email Notifications Woocommerce
How to Include External CSS and Js File in Laravel 5
Accessing an Associative Array by Integer Index in PHP
How to Paginate a Merged Collection in Laravel 5
How to Change Variables in the .Env File Dynamically in Laravel
Jquery $.Ajax Request of Datatype JSON Will Not Retrieve Data from PHP Script
Using Mx Records to Validate Email Addresses
Get Last Modified File in a Directory
How to Create a Base64Encoded String from Image Resource
Storing Database Connection in a Session Variable
How to Escape Single-Quote (Apostrophe) in String Using PHP
PHP Sessions in a Load Balancing Cluster - How
How to Set an Arrays Internal Pointer to a Specific Position? PHP/Xml
Google Calendar API V3 - Authenticate with Hardcoded Credentials