/** * Executes a query on the MySQL server * * This executes the passed SQL and returns the recordset or errors out * * @param string $sql SQL to be executed * @param int $ignore_errors If 1 this function supresses any * error messages * @return mysqli_result|false Returns results of query */ public function dbQuery($sql, $ignore_errors = 0) { if ($this->_verbose) { $this->_errorlog("DEBUG: mysqli - inside database->dbQuery"); $this->_errorlog("DEBUG: mysqli - SQL query is " . $sql); } // Run query if ($ignore_errors) { $result = @$this->_db->query($sql); } else { $result = @$this->_db->query($sql) or trigger_error($this->dbError($sql), E_USER_ERROR); } // If OK, return otherwise echo error if ($this->_db->errno == 0 and $result !== false) { if ($this->_verbose) { $this->_errorlog("DEBUG: mysqli - SQL query ran without error"); $this->_errorlog("DEBUG: mysqli - Leaving database->dbQuery"); } return $result; } else { // callee may want to suppress printing of errors if ($ignore_errors) { return false; } if ($this->_verbose) { $this->_errorlog("DEBUG: mysqli - SQL caused an error"); $this->_errorlog("DEBUG: mysqli - Leaving database->dbQuery"); } return false; } }
/** * Executes a query on the MySQL server * This executes the passed SQL and returns the recordset or errors out * * @param string $sql SQL to be executed * @param int $ignore_errors If 1 this function supresses any * error messages * @return mysqli_result|false Returns results of query */ public function dbQuery($sql, $ignore_errors = 0) { if ($this->_verbose) { $this->_errorlog("\n***inside database->dbQuery***"); $this->_errorlog("\n*** sql to execute is {$sql} ***"); } // Modifies "CREATE TABLE" SQL if (preg_match('/^\\s*create\\s\\s*table\\s/i', $sql)) { $p = strrpos($sql, ')'); if ($p !== false) { $option = substr($sql, $p + 1); if ($option !== '' && $option !== false) { // Replaces engine type $sql = substr($sql, 0, $p + 1); $option = rtrim($option, " \t\n\r\v;"); $option = str_ireplace('type', 'ENGINE', $option); if ($this->_use_innodb === true) { $option = str_ireplace('MyISAM', 'InnoDB', $option); } } else { // Appends engine type $option = ' ENGINE=' . ($this->_use_innodb === true ? 'InnoDB' : 'MyISAM'); } // Appends default charset if necessary if (($this->_charset === 'utf8' || $this->_charset === 'utf8mb4') && !preg_match('/DEFAULT\\s+(CHARSET|CHARACTER\\s+SET)/i', $option)) { $option .= " DEFAULT CHARSET={$this->_charset}"; } $sql .= $option; } } // Run query if ($ignore_errors) { $result = @$this->_db->query($sql); } else { $result = @$this->_db->query($sql); if ($result === false) { trigger_error($this->dbError($sql), E_USER_ERROR); } } // If OK, return otherwise echo error if ($this->_db->errno == 0 && $result !== false) { if ($this->_verbose) { $this->_errorlog("\n***sql ran just fine***"); $this->_errorlog("\n*** Leaving database->dbQuery ***"); } return $result; } else { // callee may want to suppress printing of errors if ($ignore_errors) { return false; } if ($this->_verbose) { $this->_errorlog("\n***sql caused an error***"); $this->_errorlog("\n*** Leaving database->dbQuery ***"); } return false; } }
/** * @param string $keyword * @param int $keywordAdditionalWeight */ public function run($keyword, $keywordAdditionalWeight = 0) { if ($this->_databaseConnection) { $prefix = config('coaster::blog.prefix'); $blogPosts = $this->_databaseConnection->query("\n SELECT wp.*, weights.search_weight FROM wp_posts wp RIGHT JOIN\n (\n SELECT ID, sum(search_weight) as search_weight\n FROM (\n SELECT ID, 4 AS search_weight FROM {$prefix}posts WHERE post_type = 'post' AND post_status = 'publish' AND post_title like '%" . $keyword . "%'\n UNION\n SELECT ID, 2 AS search_weight FROM {$prefix}posts WHERE post_type = 'post' AND post_status = 'publish' AND post_content like '%" . $keyword . "%'\n ) results\n GROUP BY ID\n ) weights\n ON weights.ID = wp.ID;\n "); if ($blogPosts) { $defaultSeparator = new Path(false); foreach ($blogPosts as $blogPost) { $postData = new \stdClass(); $postData->id = 'WP' . $blogPost['ID']; unset($blogPost['ID']); foreach ($blogPost as $field => $value) { $postData->{$field} = $value; } $postData->content = $blogPost['post_content']; $postData->fullName = ucwords(str_replace('/', ' ', config('coaster::blog.url'))) . $defaultSeparator->separator . $blogPost['post_title']; $postData->fullUrl = config('coaster::blog.url') . $blogPost['post_name']; self::_addWeight($postData, $blogPost['search_weight'] + $keywordAdditionalWeight); } } } }