I am trying to make ONE dynamic function for count in mysql:
functions.php:
function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else{
$q = "SELECT COUNT(*) FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total[0];
}
HTML Code:
<?php echo countEntries("news", "category", "1"); ?>
<?php echo countEntries("post", "type", "Sports"); ?>
But still got blank page without any error!!!
You can try this out.
function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) AS count FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else{
$q = "SELECT COUNT(*) AS count FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total['count'];
}
Here you give an alias to the count(*) and use that to access the returned result as $total['count'].
Hope it helps.
First things you forgot to close else past,second just add this line "ini_set("display_errors", 1);" at the top of your php.this will shows the error in your php.
Your code:
function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else
$q = "SELECT COUNT(*) FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total[0];
}
my code:
function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) AS count FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else{
$q = "SELECT COUNT(*) AS count FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total['count'];
}
Thanks guys, Its working well now:
function countEntries($table, $where, $what)
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else
$q = "SELECT COUNT(*) FROM " . $table . " LIMIT 1";
$record = mysql_query($q);
$total = mysql_fetch_array($record);
return $total[0];
}
echo countEntries('news', "type", "sport");
Related
I have 2 functions which work together. But I can't add parameters on the second one, I don't understand how it works.
1st function
if(!function_exists('pixiehuge_select_all')) {
function pixiehuge_select_all($table, $where = null, $order = null, $limit = null, $andwhere = null, $noteq = false, $or = false) {
global $wpdb;
$q = "SELECT * FROM `{$table}`";
$signWhere = ($or) ? 'OR' : 'AND';
// Select where
if(empty($noteq)) {
if(!empty($where)) {
$q .= " WHERE `" . esc_sql($where[0]) . "`='" . esc_sql($where[1]) . "'";
}
if(!empty($where) && !empty($andwhere)) {
$q .= " " . esc_sql($signWhere) . " `" . esc_sql($where[0]) . "`='" . esc_sql($where[1]) . "'";
}
} else {
if(!empty($where)) {
$q .= " WHERE `" . esc_sql($where[0]) . "`!='" . esc_sql($where[1]) . "'";
}
if(!empty($where) && !empty($andwhere)) {
$q .= " " . esc_sql($signWhere) . " `" . esc_sql($where[0]) . "`!='" . esc_sql($where[1]) . "'";
}
}
if(!empty($order)) {
$q .= " ORDER BY `" . esc_sql($order[0]) . "` " . esc_sql($order[1]);
}
if(!empty($limit)) {
$q .= " LIMIT 0, {$limit}";
}
// Check if plugin exists
if(!function_exists('huge_app')) {
return false;
}
$result = $wpdb->get_results($q, ARRAY_A);
return $result;
}
}
2nd function The one I'm trying to edit
if(!function_exists('pixiehuge_streams')) {
function pixiehuge_streams($id = false, $streamCat = false, $slug = false) {
global $tables;
// Get Stream(s)
if($id) {
$streams = pixiehuge_select_all($tables['streams'], ['id', esc_sql($id)]);
} elseif($streamCat) {
$streams = pixiehuge_select_all($tables['streams'], ['category', esc_sql($streamCat)]);
} elseif($slug) {
$streams = pixiehuge_select_all($tables['streams'], ['slug', esc_sql($slug)]);
} else {
$streams = pixiehuge_select_all( $tables['streams'] );
}
return $streams;
}
}
What I'm trying to get $streams = "SELECT * FROM streams ORDER BY id DESC LIMIT 4"; (but if I do this I get error 500)
So how can I rewrite $streams by adding parameters to pixiehuge_select_all() in the second function to get the order by id desc and limit 4 ?
Just look at your function parameters and complete it.
$streams = pixiehuge_select_all($tables['streams'], [], ['id','DESC'] ,4);
I have a grid that loads fine until I try to apply a filter then i get the following error.
Fatal error: Uncaught Error: Call to a member function fetch_assoc() on bool in
// build the query.
$result = $conn->query($query) or die("SQL Error 1: " . mysqli_error());
$sql = "SELECT FOUND_ROWS() AS `found_rows`;";
$rows = $conn->query($sql);
$rows = mysqli_fetch_assoc($rows);
$total_rows = $rows['found_rows'];
$query = "SELECT SQL_CALC_FOUND_ROWS profile_pic_url, username, full_name, biography, edge_followed_by, edge_follow FROM owner ORDER BY edge_followed_by DESC LIMIT $start, $total_rows".$where." ";
}
}
$result = $conn->query($query) ;
$sql = "SELECT FOUND_ROWS() AS `found_rows`;";
$rows = $conn->query($sql);
$rows = mysqli_fetch_assoc($rows);
$total_rows = $rows['found_rows'];
$orders = null;
// get data and store in a json array
while($row = $result->fetch_assoc()) {
#kryptur - this is where $where is defined
// filter data.
if (isset($_GET['filterscount']))
{
$filterscount = $_GET['filterscount'];
if ($filterscount > 0)
{
$where = " WHERE (";
$tmpdatafield = "";
$tmpfilteroperator = "";
for ($i=0; $i < $filterscount; $i++)
{
// get the filter's value.
$filtervalue = $_GET["filtervalue" . $i];
// get the filter's condition.
$filtercondition = $_GET["filtercondition" . $i];
// get the filter's column.
$filterdatafield = $_GET["filterdatafield" . $i];
// get the filter's operator.
$filteroperator = $_GET["filteroperator" . $i];
if ($tmpdatafield == "")
{
$tmpdatafield = $filterdatafield;
}
else if ($tmpdatafield <> $filterdatafield)
{
$where .= ")AND(";
}
else if ($tmpdatafield == $filterdatafield)
{
if ($tmpfilteroperator == 0)
{
$where .= " AND ";
}
else $where .= " OR ";
}
// build the "WHERE" clause depending on the filter's condition, value and datafield.
switch($filtercondition)
{
case "CONTAINS":
$where .= " " . $filterdatafield . " LIKE '%" . $filtervalue ."%'";
break;
case "DOES_NOT_CONTAIN":
$where .= " " . $filterdatafield . " NOT LIKE '%" . $filtervalue ."%'";
break;
case "GREATER_THAN":
$where .= " " . $filterdatafield . " > '" . $filtervalue ."'";
break;
case "LESS_THAN":
$where .= " " . $filterdatafield . " < '" . $filtervalue ."'";
break;
case "GREATER_THAN_OR_EQUAL":
$where .= " " . $filterdatafield . " >= '" . $filtervalue ."'";
break;
case "LESS_THAN_OR_EQUAL":
$where .= " " . $filterdatafield . " <= '" . $filtervalue ."'";
break;
}
if ($i == $filterscount - 1)
{
$where .= ")";
}
$tmpfilteroperator = $filteroperator;
$tmpdatafield = $filterdatafield;
}
I have an sql search query that returns the matched values within a where clause. I need to update this - so for example, if someone searches "elephhant" it will return the result "elephant". How do I go about doing this? Thanks
$the_word = GET['word_eng'];
$split = substr($the_word, 0, 3);
if ($noOfWords == 1) {
$searchString = " word_eng LIKE '" . $the_word ."%'";
}
else {
$searchString = $whereClause = "";
$orderBy = " order by case `word_eng` ";
foreach ($word as $order=>$entry) {
$searchString .= " OR word_eng LIKE '" . $entry . "'";
$orderBy .= " when '$entry' then $order ";
}
$orderBy .= " end ";
}
$whereClause = ($searchString != "") ? " WHERE " . preg_replace('/OR/',
'', $searchString, 1) : $whereClause;
$sql = "SELECT word_eng FROM words " . $whereClause . " OR word_eng LIKE '" .$split "%' " .$orderBy." LIMIT 50";
$result = $conn->query($sql);
I have problem in updating the value into my database. The problem here is that the when i am updating the data for all rows, the data from last row will be updated into the first row. Other rows will not be updated including the last row.
Below is my code for updating...
$sql = "SELECT * FROM product where username = '$username'";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$rows = mysqli_fetch_array($result);
$id = $rows['pro_id'];
$boxid = $rows['box_id'];
$name = $_POST['pro_name'];
$quan = $_POST['pro_quan'];
$sold = $_POST['pro_sold'];
for ($i = 0; $i < count($_POST['pro_name']); $i++)
{
$sql = "UPDATE product
SET pro_name = '" . $name[$i] . "',
pro_quan = " . $quan[$i] . ",
pro_sold = " . $sold[$i] . "
WHERE pro_id = " . $id . "
AND box_id = '" . $boxid . "' ";
$results=mysqli_query($con, $sql);
}
So, i have no ideas what have gone wrong. Thanks for helping
You need to place the result in a loop.
Something like that:
$sql = "SELECT * FROM product where username = '$username'";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
while ($rows = mysqli_fetch_array($result))
{
$id = $rows['pro_id'];
$boxid = $rows['box_id'];
$name = $_POST['pro_name'];
$quan = $_POST['pro_quan'];
$sold = $_POST['pro_sold'];
for ($i = 0; $i < count($_POST['pro_name']); $i++)
{
$sql = "UPDATE product
SET pro_name = '" . $name[$i] . "',
pro_quan = " . $quan[$i] . ",
pro_sold = " . $sold[$i] . "
WHERE pro_id = " . $id . "
AND box_id = '" . $boxid . "' ";
$results = mysqli_query($con, $sql);
}
}
ORDER BY is not working. I've tried ways I know but it won't order by, why?
returns : Call to a member function result_array() on boolean
public function getDetailbyClsandSection($class_id, $section_id, $exam_id) {
$query = $this->db->query("SELECT exam_schedules.*,subjects.name,subjects.type FROM exam_schedules,teacher_subjects,exams,class_sections,subjects WHERE exam_schedules.teacher_subject_id = teacher_subjects.id and exam_schedules.exam_id =exams.id and class_sections.id =teacher_subjects.class_section_id and teacher_subjects.subject_id=subjects.id and class_sections.class_id =" . $this->db->escape($class_id) . " and class_sections.section_id=" . $this->db->escape($section_id) . " and exam_id =" . $this->db->escape($exam_id) . " and exam_schedules.session_id=" . $this->db->escape($this->current_session));
return $query->result_array();
}
$query = $this->db->query("SELECT exam_schedules.*,subjects.name,subjects.type FROM exam_schedules,teacher_subjects,exams,class_sections,subjects WHERE exam_schedules.hide = 'N' and exam_schedules.teacher_subject_id = teacher_subjects.id and exam_schedules.exam_id =exams.id and class_sections.id =teacher_subjects.class_section_id and teacher_subjects.subject_id=subjects.id and class_sections.class_id =" . $this->db->escape($class_id) . " and class_sections.section_id=" . $this->db->escape($section_id) . " and exam_id =" . $this->db->escape($exam_id) . " and exam_schedules.session_id=" . $this->db->escape($this->current_session) . " ORDER BY exam_schedules.date_of_exam ASC");