hi all.....

my script try to do 3 part:

1- get value of all user from active directory 2003
2- store it in table in mysql
3- view the same value in html

first point and third one work perfect but second point not work at all
connect to database work and select table work but insert the array to table in mysql not work at all

i need to know where is the issue

<?php

/**
 * @author Phiron
 * @copyright 2010
 */


$phpAD_ldap = "ldap://192.168.100.2";
$phpAD_user = "*******";
$phpAD_pass = "*******";
$phpAD_dn = "OU=bad,DC=boy,DC=com,DC=ad";
$error = "";

$attributes = array("displayname", "samaccountname");
$filter = "(!(cn=''))";



$ad = ldap_connect($phpAD_ldap, 389) 
          or die("Couldn't connect to AD!");

ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ad, LDAP_OPT_REFERRALS, 0); 
ldap_get_option($ad,LDAP_OPT_ERROR_STRING,$error);

$bd = ldap_bind($ad,$phpAD_user,$phpAD_pass)
          or die("Couldn't bind to AD!");

$result = ldap_search($ad, $phpAD_dn, $filter, $attributes);
$entries = ldap_get_entries($ad, $result);



echo $error;

ldap_unbind($ad);

?>
<html>
<head>

</head>
<body>
    <table border="1" align=center>
        <tr>
            <td align=center>
                <big> displayname
            </td>
            <td align=center>
                <big> samaccountname
            </td>
        </tr>



<?

$con = mysql_connect("localhost","DBAdmin","0123501848");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

$sel = mysql_select_db("amaz", $con);
    if (!$sel)
    {
    die('Could not select DB: ' . mysql_error());
    }





for ($i=0; $i<$entries["count"]; $i++)
{

$dis = $entries[$i]["displayname"][0];
$sam = $entries[$i]["samaccountname"][0];

        mysql_query("INSERT INTO ldap (displayname, samaccountname)
        VALUES ('$dis', '$sam')");        

}

mysql_close($con);
?> 



<?php

for ($i=0; $i<$entries["count"]; $i++)
{
    echo '<tr>';
    echo '<td>';
    echo $entries[$i]["displayname"][0];
    echo '</td>';
    echo '<td>';
    echo $entries[$i]["samaccountname"][0];
    echo '</td>';
    echo '</tr>';
}

?>

</table>


</body>
</html>

    Is mysql_query() returning false? If so, what is the error message returned by the SQL server (see [man]mysql_error/man)?

      i add mysql_error() but no error show up and mysql_query not return with error

      it's like array having null value and it make me confuse because the same array i use it to create html table and it's work perfect

          for ($i=0; $i<$entries["count"]; $i++)
          {
      
      $dis = $entries[$i]["displayname"][0];
      $sam = $entries[$i]["samaccountname"][0];
      
              mysql_query("INSERT INTO ldap (displayname, samaccountname)
              VALUES ('$dis', '$sam')");        
      
      } 
      
      echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
      
      mysql_close($con); 
      
      

        Try something like this instead to see if you're getting any SQL errors:

                    mysql_query("INSERT INTO ldap (displayname, samaccountname) 
                    VALUES ('$dis', '$sam')") or die("MySQL error: " . mysql_error() . "<br>\n");

          i did what you said and something strange show up

          that is the error

          \n"); } echo mysql_errno($con) . ": " . mysql_error($con) . "\n"; mysql_close($con); ?> 
          

          and that's the code

          <?
          
          $con = mysql_connect("localhost","DBAdmin","0123501848");
              if (!$con)
              {
              die('Could not connect: ' . mysql_error());
              }
          
          $sel = mysql_select_db("amaz", $con);
              if (!$sel)
              {
              die('Could not select DB: ' . mysql_error());
              }
          
          
          
          
          
          for ($i=0; $i<$entries["count"]; $i++)
          {
          
          $dis = $entries[$i]["displayname"][0];
          $sam = $entries[$i]["samaccountname"][0];
          
                  mysql_query("INSERT INTO ldap (displayname, samaccountname)
                  VALUES ('$dis', '$sam')") or die("MySQL error: " . mysql_error() . "<br>\n"); 
          
          }
          echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
          
          mysql_close($con);
          ?> 
          
          

            Try changing '<?' to '<?php', since the former ("short tag") has been deprecated and disabled by default for a while now (and even removed in future versions of PHP).

              really i hate my self because i wast more than 1 day in diagnostic and analyses and search Google for solution and i found myself so stupid.

              no really I'm stupid

              bradgrafelman yes that is the solution

              just put "<?php code ?>" instead of "<? code ?>"

              and really thanks bradgrafelman for open my mind and knock my head.

                Write a Reply...