Hi there... I have a php class in which is making a ton of SQL queries.
I currently have a method in my class that is:
function __doMySql($query) {
if(!$result=@$this->mysqli->query($query)) {
$errno=$this->mysqli->errno;
$this->mysqli->close();
trigger_error("'elib' class - doMySql error: " . $errno);
}
return $result;
}
I would then call it as such (just one example - this on is being passed in a JSON format parameter):
function docsAdd($doc) {
# Escape special characters
$title=$this->mysqli->real_escape_string(trim($doc['title']));
$cat=$this->mysqli->real_escape_string(trim($doc['category']));
$auth=$this->mysqli->real_escape_string(trim($doc['author']));
$add_by=$this->mysqli->real_escape_string(trim($doc['add_by']));
// mod the same as added by since it is a new record
$pub_date=$this->mysqli->real_escape_string(trim($doc['pub_date']));
//sql Now Function $added_date=$this->mysqli->real_escape_string(trim($doc['add_date']));
//sql Now Function $edited_date=$this->mysqli->real_escape_string(trim($doc['edited_date']));
$qty=$this->mysqli->real_escape_string(trim($doc['qty']));
// available qty - same as qty because we are adding a new record
$path=$this->mysqli->real_escape_string(trim($doc['path']));
$sql = "INSERT INTO elib_docs (title, category, author, added_by, modified_by, published_date, added_date, edited_date, qty, available, active, path) VALUES ('$title', '$cat', '$auth', '$add_by', '$add_by', '$pub_date', NOW(), NOW(), '$qty', '$qty', '1', '$path')";
$this->__doMySql($sql);
$result = "The document titled '". $title. "' has been successfully added.";
//$result_array[] = $user_array;
return($result);
}
Now for some reason the above is not working. Now if i use the same "doMySql" method, but use something like:
function docsRetrieve($myquery) {
# Return a list of all the users\
# Check for wild card and remove query to sim a blank query
if ($myquery == "*" || $myquery == "") {
$sql = "SELECT docs.title, docs.path, docs.added_date, docs.qty, docs.available, docs.author, date_format(docs.published_date, '%b %e %Y') as published_date, cat.category from elib_docs AS docs INNER JOIN elib_categories AS cat ON docs.category=cat.id AND docs.active_doc='1'";
$sqlRes = $this->__doMySql($sql);
} else {
# run with the query
$sql = "SELECT docs.title, docs.path, docs.added_date, docs.qty, docs.available, docs.author, date_format(docs.published_date, '%b %e %Y') as published_date, cat.category from elib_docs AS docs INNER JOIN elib_categories AS cat ON (docs.category=cat.id AND docs.active_doc='1') AND (docs.title LIKE '%$myquery%' OR docs.author LIKE '%$myquery%' OR docs.added_by LIKE '%$myquery%' OR cat.category LIKE '%$myquery%' OR docs.published_date LIKE '%$myquery%')";
$sqlRes = $this->__doMySql($sql);
}
while ($row = $sqlRes->fetch_assoc()) {
$user_array[] = $row;
}
/* determine number of rows result set */
$row_cnt = $sqlRes->num_rows;
if ($myquery == "*" || $myquery == ""){
$result_array[] = "Your search returned ". $row_cnt. " documents.";
} else {
$result_array[] = "Your search returned ". $row_cnt. " documents, for the term '" . $myquery . "'.";
}
$result_array[] = $user_array;
return($result_array);
}
It works....
Can someone point me in the right direction for a generic sql method?