使用PHP OOP在MYSQL DB中存储信息

时间:2017-06-11 13:09:59

标签: php mysql mysqli

我有以下代码所有迹象表明它正在工作我得到了"成功"消息和storeData()函数中的回声显示正确的信息。但是,如果数据没有存储在MySQL数据库中,则不会发生任何事情。谁能明白为什么?

我添加了echo只是为了检查它是否正在读取正确的$ this值并且它是。

 if ($result = $stmt->execute())
                      {
                          echo "Success";
                          echo $this->domainname;
                          echo $this->date;
                      }

完整代码 -

<?php
class domain
{
    protected $domainname;
    protected $whois;
    protected $dnsa;
    protected $dnsmx;
    protected $dnstxt;
    protected $dnsns;
    protected $blacklist;
    protected $ping;
    protected $tracert;
    protected $nmap;
    protected $ip;
    protected $date;

    private $servername = "localhost";
    private $username = "";
    private $password = "";
    private $dbname = "domainhistory";

    function __construct($domain)
      {
          echo $domain."Passed";
          $this->date = $date1 = date("Y/m/d");
          $this->domainname = $domain;
          $this->ip = gethostbyname("$domain");
          $this->whois = shell_exec("whois $domain | tr -d '\'\' ");
          $this->ssl = shell_exec("curl --insecure -v https://$domain 2>&1 | awk 'BEGIN { cert=0 } /^\* SSL connection/ { cert=1 } /^\*/ { if (cert) print }' | grep -v subject");
          $this->dnsa = shell_exec("dig $domain A | grep A | grep -v ns | grep $domain | grep -v ';' ");
          $this->dnsmx = shell_exec("dig $domain MX | grep MX | grep -v ns| grep $domain | grep -v ';' ");
          $this->dnstxt = shell_exec("dig $domain TXT | grep TXT | grep -v ns | grep $domain | grep -v ';' ");
          $this->dnsns = shell_exec("dig $domain NS | grep NS | grep -v A |  grep $domain | grep -v ';' ");
          $this->blacklist = shell_exec("curl -I -X GET api.moocher.io/badip/$ip | grep HTTP");
          $this->ping = shell_exec("ping -c 3 $domain");
          $this->tracert = shell_exec("traceroute -m 10 $domain");
          $this->nmap = shell_exec("nmap -n $domain");
      }
####This called when you request a value
      function __get($name)
        {
          ##Checks if the value is empty or not before returning.
          return $this->$name;
        }

### Function to store the object inside of the MYSQL Database
      function storeData()
      {
            $servername = $this->servername;
            $username = $this->username;
            $password = $this->password;
            $dbname = $this->dbname;
            #Opens the MYSQL Connection
            $conn = mysqli_connect($servername, $username, $password, $dbname);
                if (!$conn)
              {
                die("Connection failed: " . mysqli_connect_error());
              }
      #Prepares the mySQL statement, (stops SQL injection)
      $stmt = $conn->prepare("INSERT INTO `history` (`domain`, `date`, `whois`, `dnsa`, `dnsmx`, `dnstxt`, `dnsns`, `blacklist`, `ping`, `tracert`, `nmap`, `sslinfo`)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
      $stmt->bind_param('ssssssssssss', $this->domainame, $this->date, $this->whois, $this->dnsa, $this->dnsmx, $this->dnstxt, $this->dnsns, $this->blacklist, $this->ping, $this->tracert, $this->nmap, $this->sslinfo);
                  #Executes the prepared statement
                  if ($result = $stmt->execute())
                      {
                          echo "Success";
                          echo $this->domainname;
                          echo $this->date;
                      }
           #closes the mysql connection
            mysqli_close($conn);
      }
}
 ?>

更新

我完全按预期工作了(问题是$this->sslinfo $this->ssl,拼写错误$this->domainname)。

我在error_logs中仍然收到一个奇怪的错误:

  

mod_fcgid:stderr:PHP警告:mysqli_stmt :: bind_param()错误的参数计数i -

然而,它没有错误的参数计数 - 我在MySQL中有一个列,它是一个唯一的ID,所以可能与此有关。

0 个答案:

没有答案