1

I've been stuck on this for a little while now. Am sure there is an easy solution for this but I am still wrapping my head around php.

I want to get some data from a DB and save it into another.

Using SQL I managed to get that data and save it in an array. Now I want to convert that data into a string to save to a different array (if there is a way to do that directly without first saving into an array, then I am all ears).

I get my data in this format.

Array ( [0] => Array ( [0] => 414208 [InterestedEntityId] => 414208 [1] => 126.61370996251785 [Score] => 126.61370996251785 ) [1] => Array ( [0] => 479516 [InterestedEntityId] => 479516 [1] => 73.531040944194 [Score] => 73.531040944194 ) [2] => Array ( [0] => 4129769 [InterestedEntityId] => 4129769 [1] => 54.390965049372674 [Score] => 54.390965049372674 )

I want to convert it into:

414208  126.61370996251785
479516  73.531040944194  
4129769 54.390965049372674

Here is my code:

$sql = mysql_query("SELECT Id, Score FROM users.`table1` WHERE UserId= $userID", $dbh1);

$table = array();

while ($row = mysql_fetch_array($sql))
    $table[] = $row;

$table = implode($table);


mysql_query("INSERT INTO $tbl_name(table) VALUES ('$table')", $dbh2);

Can someone please help in this?

2 Answers 2

1

A simple foreach loop would do the job.

Making a nice table:

$output = '<table border="1">';
foreach($table as $row){
    $output .= '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td></tr>';
}
$output .= '</table>';
echo $output;

Plain text:

$output = '';
foreach($table as $row){
    $output .= $row[0].' '.$row[1].'<br>';
}
echo $output;
Sign up to request clarification or add additional context in comments.

1 Comment

it should be $output .= $row[0].' '.$row[1] . '<br>'; (you forgot . before <br> )but other than that it's perfect. Thank you so much!
0

As a solution to your problem please try executing following code snippet.

 <?php
   try
   {
        $a=array(array(414208,126.61370996251785),array(414208,126.61370996251785));
        $string='';
        foreach($a as $key=>$value)
        {
           if(!is_array($value))
           {
              throw new Exception('Not an array.');
            }
           else
           {
            $string=join(' ',$value);
           }        
           }
      }
     catch(Exception $e)
    {
   echo $e->getMessage();
    }    
   echo $string;

 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.