php - Serialization with variable transfer to another database


I can't seem to get this to work, all static data works, but for the variables below,

   $customf = $row["ps_custom_fields"];
        $cfarr = unserialize($customf);
        $name = $cfarr[5];
        $desc = $cfarr[2];
        $cat = $cfarr[3];
        $tags = $cfarr[4];

I can't get them to carry the data to the next database and input all data i need, its for an advertising system that runs off another database that deals with all the invoices, purchases and data collection, the second database just outputs to the advert website.

//Connect to Local Server Database
    $link1 = mysql_connect("localhost","usr","pass") or die("Opps Local Connection Failed!");
    $db = mysql_select_db("pulseem1_ipb",$link1) or die ("Local Connection Failed Again!");

//Select Result to pull
    $result = mysql_query("SELECT ad_id, ad_image, ad_link FROM ipb_nexus_ads")
    or die ("Results Didnt pull Correctly!");

//Second Results to pull
$result2 = mysql_query("SELECT ps_custom_fields FROM ipb_nexus_purchases")
    or die ("Second Results Didnt pull Correctly!");

    while ($row = mysql_fetch_array($result,$result2))

//Put Results into a Variable

    $customf = $row["ps_custom_fields"];
    $cfarr = unserialize($customf);
    $name = $cfarr[5];
    $desc = $cfarr[2];
    $cat = $cfarr[3];
    $tags = $cfarr[4];
    $url = $row["ad_link"];
    $image = $row["ad_image"];


//Upload Advert to Remote Host with values of Variables
//Connect To Remote Database
    $link2 = mysql_connect("localhost","user","pass") or die("Opps Second Connection Failed!");
    $db = mysql_select_db("onlyclic_adverts",$link2) or die ("Second Connection Failed Again!");

// Insert Values from Original Database
    mysql_query("INSERT INTO busadverts (advert_name, advert_url, advert_image, advert_description, advert_catergory, advert_tags, active) VALUES ('$name','$url','$image','$desc','$cat','$tags','1')");

//Release Connection and Results



If I'm not mistaken, you are trying to pass two variables of data in the mysql_fetch_array. The function accepts the first argument passed as the resource and the second argument being a result type such as MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. So, this could be why you are seeing a problem with your results.

Without seeing the data coming in from the results, there are couple options to process that data. You could array_merge them before looping over the data or you could separately loop over each data set putting them into their respected variables.

Anytime you are not seeing the appropriate results from a variable, make sure you are var_dump certain parts of the code to see the results.

-- Answer to the comment --

Array Merge Example

while ($row = mysql_fetch_array($result1){
    $array1 = $row;

while ($row2 = mysql_fetch_array($result2){
    $array2 = $row;
//array merge
$items = array_merge($array1, $array2);

//Loop over data and setup variables
foreach ($items as $id => $item){
    //Setup what you need while looping over items

Just looping over data

while ($row = mysql_fetch_array($result1){
    //unserialize pieces from this array and put them in the appropriate variables

while ($row2 = mysql_fetch_array($result2){
    //unserialize pieces from this array and put them in the appropriate variables. 

People are also looking for solutions to the problem: php - constructor param not available in init


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.