How to define where condition in this below query?form the database except the zero value need to shown
private function totalVisitedPagesPerUser() {
$query = "SELECT COUNT(*), " . $this->_db->quoteName('customer_name') . "," .
"\n MAX(". $this->_db->quoteName('visit_timestamp') . ")," . $this->_db->quoteName('browser') . "," .
$this->_db->quoteName('os') . "," .
$this->_db->quoteName('session_id_person') . "," .
$this->_db->quoteName('ip') . "," .
"\n SUM(" . $this->_db->quoteName('impulse') . ")," .
$this->_db->quoteName('geolocation') .
"\n FROM ( SELECT * FROM #__realtimeanalytics_serverstats " . $this->whereQuery . " ORDER BY `visit_timestamp` DESC) AS INTABLE" .
$this->whereQuery .
"\n GROUP BY " . $this->_db->quoteName('session_id_person') .
"\n ORDER BY " . $this->_db->quoteName('customer_name');
$this->_db->setQuery($query);
$results = $this->_db->loadRowList();
if ($this->_db->getErrorNum()) {
throw new JRealtimeException(JText::sprintf('COM_JREALTIME_ERROR_RECORDS', $this->_db->getErrorMsg()), 'error');
}
return $results;
}
you can prepare where condition variable by checking conditions before query start, than append that variable at that point like:
$wherecondtion = "Where 1=1";
if($condition1){$wherecondtion."AND (add required filter 1)"}
if($condition2){$wherecondtion."AND (add required filter 2)"}
your subquery will be like:
"( SELECT * FROM #__realtimeanalytics_serverstats " . $wherecondtion . " ORDER BY `visit_timestamp` DESC)"
Is this the thing you needed or something different thing?
Related
Beginner here trying out PDO/MySQL. I am coding a simple SELECT * and displaying the results. The code below works fine to display the contents.
$sql = 'SELECT * FROM names'; //fieldnames are id, name, email
$stmt = $db->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch())
{echo $row[0] . " | " . $row[1] . " | " . $row[2] . "<br/>";}
However, when I use the fieldname as the index instead of the number, I get a PHP notice:
Notice: Undefined index: name
Codes below does not work.
{echo $row['id'] . " | " . $row['name'] . " | " . $row['email'] . "<br/>";}
I have seen several tutorials where they have used fieldname as the index. Can someone provide some further clarification on when/how the fieldname can be used instead of the number for the index?
My sql query is working fine, until I try to add the absolute last row. How can I get that part working?
INSERT INTO posts (ssp_order, ssp_id, ssp_ss_id, ssp_c_id)
SELECT COALESCE(MAX(ssp_order),0)+1 ,
" . $sspid . "," . $ssid . "," . $cid . "
FROM posts
WHERE ssp_ss_id = " . $ssid . "
ON DUPLICATE KEY UPDATE
ssp_status = 0,
ssp_order = SELECT COALESCE(MAX(ssp_order),0)+1 FROM posts
(please don't worry about the safety of the variables in there)
Reference the table used in the select statement rather than using a subquery
INSERT INTO posts (ssp_order, ssp_id, ssp_ss_id, ssp_c_id)
SELECT maxssporder ,sspid,ssid,cid from
(SELECT COALESCE(MAX(ssp_order),0)+1 as maxssporder,
" . $sspid . " as sspid," . $ssid . " as ssid," . $cid . " as cid
FROM posts p
WHERE ssp_ss_id = " . $ssid . ") q
ON DUPLICATE KEY UPDATE
ssp_status = 0,
ssp_order = q.maxssporder
Here's what I'm wanting to do in a "DELETE" statement:
delete from table1
where (username = "Jonathan" or username = "Amy")
and (field2 = "Jonathan" or field3 = "Jonathan" or field4 = "Jonathan" or field2 = "Amy" or field 3 = "Amy" or field4 = "Amy")
Surely there's a way to do this right?
EDIT:
Here's the actual code I'm using. I must be doing something wrong:
$sql = "DELETE FROM friend_requests
WHERE username in ('jesusfreak', " . $_SESSION['user']['username'] . ")
AND (requests in ('jesusfreak', " . $_SESSION['user']['username'] . ")
OR common_friends in ('jesusfreak', " . $_SESSION['user']['username'] . ")
OR suggest_favorited in ('jesusfreak', " . $_SESSION['user']['username'] . ")
OR suggest_prayed ('jesusfreak', " . $_SESSION['user']['username'] . ")
OR suggest_viewed in ('jesusfreak', " . $_SESSION['user']['username'] . "))";
$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->query($sql);
$conn->close();
Try with this:
delete from table1
where username in ('Jonathan','Amy')
and (field2 in ('Jonathan','Amy')
or
field3 in ('Jonathan','Amy')
or
field4 in ('Jonathan','Amy'));
In the function below I am implementing substring for the field $field but I am getting,
Error Notice: Error: Unknown column 'EBook_at' in 'field list'
I know there is no column like EBook_at but how substring the $field.
Expecting Output:
/image/56789011/5678901145678
public function prependText($adjustment) {
$prepend_text = $adjustment[0];
$field = $adjustment[1];
$this->db->query('UPDATE ' . DB_PREFIX . "hj_import SET `" . $field . "` = CONCAT( '" . $this->db->escape($prepend_text) . "'," . SUBSTR($field,0,8) . "`, `" . $field . "` )");
}
It is fixed by using below query :
$this->db->query('UPDATE ' . DB_PREFIX . "hj_import SET `" . $field . "` = CONCAT( '" . $this->db->escape($prepend_text) . "', substr($field, 1, 8 ),'/',`" . $field . "` )");
Hi I've searched for a solution to this and found several answers and after many edits to the code and no success I'm asking here directly.
$return_arr = array();
$fetch = tep_db_query("select * from products, " . TABLE_PRODUCTS_DESCRIPTION . " WHERE products.products_status = '1' and products.products_id = " . TABLE_PRODUCTS_DESCRIPTION . ".products_id and " . TABLE_PRODUCTS_DESCRIPTION . ".language_id = '" . (int)$languages_id . "' and " . TABLE_PRODUCTS_DESCRIPTION . ".products_name LIKE '%" . $_GET['term'] . "%' LIMIT 0,10");
if($fetch === FALSE) {
die(mysql_error());
}
while ($row = mysql_fetch_assoc($fetch))
{
array_push($return_arr, $row['products_name']);
}
print json_encode($return_arr);
This returns: mysql_fetch_assoc() parameter 1 resource, object given in ... on line 6
I get where it is, just can't seem to find the problem.
Thanks in advance
Use tep_db_fetch_array()
$return_arr = array();
$fetch = tep_db_query("select * from products, " . TABLE_PRODUCTS_DESCRIPTION . " WHERE products.products_status = '1' and products.products_id = " . TABLE_PRODUCTS_DESCRIPTION . ".products_id and " . TABLE_PRODUCTS_DESCRIPTION . ".language_id = '" . (int)$languages_id . "' and " . TABLE_PRODUCTS_DESCRIPTION . ".products_name LIKE '%" . $_GET['term'] . "%' LIMIT 0,10");
echo $fetch; die;
if($fetch === FALSE) {
die(mysql_error());
}
while ($row = tep_db_fetch_array($fetch))
{
array_push($return_arr, $row['products_name']);
}
print json_encode($return_arr);
string mysqli_real_escape_string ( mysqli $link , string $escapestr )
link- A link identifier returned by mysqli_connect() or mysqli_init()
escapestr- The string to be escaped.
You can find an example http://php.net/manual/en/mysqli.real-escape-string.php the first parameter needs to be the link identifier returned by mysqli_connect()