help needed with associative arrays in php

Hello!
I am new to php and working on a search code in mysql using php for my thesis implimentation. I want to search ID of an article in a table ‘dnew’ in another table ‘data’ and then save the id of searched record against the first. searched records may be more than one so I am saving it with a comma. I have written a code which works fine but taking too much time. I want it to be done faster. Can any one help me to do it in a better way to make it faster. Some one told me to use associative arrays as it works like hash maps but I have no luck in that. Please some one help!!!

[php]

$query=mysql_query("SELECT*
FROMdnew
WHERE outlink_papers=’’
“);
while($roww = mysql_fetch_assoc($query)){
//$ID=1;
$con_id=” ";
$ID=$roww[‘IDD’];

$_SESSION[‘data’]=$roww[‘Outlinks’];

      $result1 = mysql_query("SELECT * 
                              FROM `data` WHERE`CitingPapers`LIKE '%".$roww['PID']."%'");
							  $countt=mysql_num_rows($result1);

                 while($row = mysql_fetch_assoc($result1)){
                             $PID=$row['PID'];
	 
                             $out=$_SESSION['data'];
                             $con_id=$out.",".$PID;			
                             $_SESSION['data']=$con_id;
  }

echo $_SESSION[‘data’];
@session_destroy();
}

[/php]

try
[php]$query=mysql_query(“SELECT* FROMdnew WHERE outlink_papers=’’”);

while($roww = mysql_fetch_assoc($query)) {

$ID=$roww['IDD'];
$_SESSION['data'] = $roww['Outlinks'];
$result1 = mysql_query("SELECT * FROM `data` WHERE`CitingPapers`LIKE '%"'.$roww['PID'].'"%'");

if(mysql_num_rows($result1) != 0) {
     while($row = mysql_fetch_assoc($result1)){
        $PID = $row['PID'];
        $out = $_SESSION['data'];
        $con_id = $out.",".$PID;
        $_SESSION['data'] = $con_id;
    }
    echo $_SESSION['data'];
} else {
    echo "No papers were found with that ID";
}
session_destroy();

}[/php]

that’s just a cleaned up vesion of your code. I don’t understand what some of these variables do. if you want that session to maintain the first line (outlinks), then you need to use .= instead because you’re just overwriting it with what you have now. if you want to save the id’s you’re getting from the second query, just make $PID an array.

[php]$query=mysql_query(“SELECT* FROMdnew WHERE outlink_papers=’’”);
$PID = array();
while($roww = mysql_fetch_assoc($query)) {

$ID=$roww['IDD'];
$_SESSION['data'] = $roww['Outlinks'];
$result1 = mysql_query("SELECT * FROM `data` WHERE`CitingPapers`LIKE '%"'.$roww['PID'].'"%'");

if(mysql_num_rows($result1) != 0) {
     while($row = mysql_fetch_assoc($result1)){
        $PID[] = $row['PID'];
        $out = $_SESSION['data'];
        $con_id = $out.",".$PID;
        $_SESSION['data'] = $con_id;
    }
    echo $_SESSION['data'];
} else {
    echo "No papers were found with that ID";
}
session_destroy();

}[/php]
That’ll save those id’s, then just use a loop to sort through those id’s.

Thanks alot for your kind reply and help!
I am using these lines instead of storing values in session now
[php]
$PID[] = $row[‘PID’];

		$id=implode(',',$PID);

[/php]

My main problem is that I have 16 lac records in my database and have to search all to collect id’s of those articles which are citing the paper under consideration. I have to update 20,000 records for my work and this code which I wrote takes 1 hr to update only 50 records. Is there any other way faster than this to do this work?

You could try using a for loop instead of the while, but i don’t think it would make much of a difference.

Sponsor our Newsletter | Privacy Policy | Terms of Service