// MySQl host $userdb = 'root'; // MySQL username $passdb = 'pass'; // MySQL password $namedb = $dbName ? $dbName : 'jscharting'; // MySQL database name $link = mysqli_connect($hostdb, $userdb, $passdb, $namedb); if (mysqli_connect_errno()) { die('Could not connect: ' . mysqli_connect_error()); } else { echo 'Connected to DB: ' . $namedb; } return $link; } $linkDB = ConnectToMySql(); $stmtDB = mysqli_stmt_init($linkDB); $sqlStatement = 'SELECT * FROM AreaData'; mysqli_stmt_prepare($stmtDB, $sqlStatement); mysqli_stmt_execute($stmtDB); $resultDB = mysqli_stmt_get_result($stmtDB) or die($stmtDB->error); $rowCount = mysqli_num_rows($resultDB); if ($rowCount < 1) { echo '<br/>No records.'; } else { echo '<br/>Total records in AreaData: ' . $rowCount; } ?> </head> <body>
private function getDataDB() { if (!empty($this->startDate)) { $this->sqlStatement = str_ireplace("#StartDate#", "'" . date_format($this->startDate, 'Y-m-d H:i:s') . "'", $this->sqlStatement); } if (!empty($this->endDate)) { $this->sqlStatement = str_ireplace("#EndDate#", "'" . date_format($this->endDate, 'Y-m-d H:i:s') . "'", $this->sqlStatement); } $this->linkDB = ConnectToMySql(); $this->stmtDB = mysqli_stmt_init($this->linkDB); $countParams = count($this->sqlParams); if (!empty($this->storedProcedure)) { $storedProcedureCall = $this->storedProcedure; if (0 !== strpos(strtolower($this->storedProcedure), 'call ')) { $storedProcedureCall = "call " . $storedProcedureCall . "("; } for ($i = 0; $i < $countParams; $i++) { $storedProcedureCall .= "?,"; } $storedProcedureCall = rtrim($storedProcedureCall, ','); $storedProcedureCall .= ")"; if (mysqli_stmt_prepare($this->stmtDB, $storedProcedureCall)) { /* Bind parameters. Types: s = string, i = integer, d = double, b = blob */ $a_params = array(); $param_type = ''; foreach ($this->sqlParams as $key => $value) { if (is_numeric($value[0])) { $a_params[] = $value[0]; $param_type .= "d"; } else { if ($value[0] instanceof DateTime) { $param_dt = date_format($value[0], 'Y-m-d H:i:s'); $a_params[] = $param_dt; $param_type .= "s"; } else { $a_params[] = $value[0]; $param_type .= "s"; } } } call_user_func_array('mysqli_stmt_bind_param', array_merge(array($this->stmtDB, $param_type), $this->refValues($a_params))); /* execute query */ mysqli_stmt_execute($this->stmtDB); $this->resultDB = mysqli_stmt_get_result($this->stmtDB) or die($this->stmtDB->error); } else { die('Sql error: ' . $this->stmtDB->error); } } else { if (mysqli_stmt_prepare($this->stmtDB, $this->sqlStatement)) { if ($countParams > 0) { $a_params = array(); $param_type = ''; foreach ($this->sqlParams as $key => $value) { if (is_numeric($value[0])) { $a_params[] = $value[0]; $param_type .= "d"; } else { if ($value[0] instanceof DateTime) { $param_dt = date_format($value[0], 'Y-m-d H:i:s'); $a_params[] = $param_dt; $param_type .= "s"; } else { $a_params[] = $value[0]; $param_type .= "s"; } } } call_user_func_array('mysqli_stmt_bind_param', array_merge(array($this->stmtDB, $param_type), $this->refValues($a_params))); } /* execute query */ mysqli_stmt_execute($this->stmtDB); $this->resultDB = mysqli_stmt_get_result($this->stmtDB) or die($this->stmtDB->error); } else { die('Sql error: ' . $this->stmtDB->error); } } }