1

My code shown below will successfully return an array of values using print_r. How can I can get each value to be inserted into SQL Server "on its own row" in the same column? So far, I have only been able to get the first value in the array to be written to my table. The additional value(s) are not written. Thx, _ JT

$host = '209.49.180.234';
$hostname = gethostbyaddr( $host );
$rbl  = 'hostkarma.junkemailfilter.com';
$lookup = $hostname . '.' . $rbl;
$value = gethostbynamel($lookup);

    if ($lookup != $value){

       print_r($value);
    }

I am unclear where to insert the loop. Like this?:

$host = '209.49.180.234';
$hostname = gethostbyaddr( $host );
$rbl  = 'hostkarma.junkemailfilter.com';
$lookup = $hostname . '.' . $rbl;
$value = gethostbynamel($lookup);
   if ($lookup != $value)
    foreach($value as $val) {
         mssql_query("INSERT INTO $table5 (Whitelist_code) VALUES ('".$val."')");
     }

mssql_close($conn);

I got it to work. Thanks guys! _ JT

1
  • loop through the array with foreach() Commented Jun 27, 2011 at 22:11

2 Answers 2

2

Try this

<?php
  foreach($value as $val) {
    mssql_query("INSERT INTO table (value) VALUES ('".$val."')");
  }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sander but I am unclear where to insert the loop. Like this?:$host = '209.49.180.234'; $hostname = gethostbyaddr( $host ); $rbl = 'hostkarma.junkemailfilter.com'; $lookup = $hostname . '.' . $rbl; $value = gethostbynamel($lookup); if ($lookup != $value) foreach($value as $val) { mssql_query("INSERT INTO $table5 (Whitelist_code) VALUES ('".$val."')"); } mssql_close($conn);
1

use a foreach loop

foreach ($value as $a):
    //access your rows as $a
    //ex.- echo $a; put your sql stuff here instead
endforeach;

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.