pagination add in plugin - plugins

i have created pagination but it showed
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '22 LIMIT -4, 4' at line 1]
22 LIMIT -4, 4
if (isset( $_GET['page'])){
$page= ((int)$_GET['page']);
}
else{
$page=1;
}
$items_per_page = 4;
$offset = ( $page * $items_per_page ) - $items_per_page;
$query = "SELECT * FROM $table_name";
$total_query= "SELECT COUNT(1) FROM (${query}) AS combined_table";
$total = $wpdb->get_var( $total_query );
$result = $wpdb->get_results( $total . " LIMIT ${offset}, ${items_per_page}" );
$totalPage = ceil($total / $items_per_page);

Related

Zend 2: How do I execute multiple SQL queries using Zend\Db\Adapter?

Im executing this query:
$query = "
Select * From table1 Where id = 1;
Select * From table2 Where name = 'test';
";
With \Zend\Db\Adapter\Adapter:
$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();
How can I access the second result?
$rawResult->next() only returns values from first query.
Use two different queries.
/*
* First Query
*/
$query = "
Select * From table1 Where id = 1
";
$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();
/*
* Second Query
*/
$query = "
Select * From table2 Where name = 'test'
";
$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();
I don't believe it works the way you had it to where two queries in a row are sent this way.
To avoid code duplication you can wrap the duplicated code into a method.
$rawResult1 = $this->getResults("Select * From table1 Where id = 1");
$rawResult2 = $this->getResults("Select * From table2 Where name = 'test'");
function getResults(string $query)
{
$stmt = $this->dbAdapter->query($query);
return $stmt->execute();
}

prepare command not working with DBI using perl

$sth = $dbh->prepare("select CODE_ID,NAME_CODE,SUM(INR_COL + OUT_COL) AS "TOTAL SUM" from nwsa where CODE_ID='L01A' OR CODE_ID='L01B'OR CODE_ID='L01C' OR CODE_ID='L01D' OR CODE_ID='L01DA' OR CODE_ID='L01E' OR CODE_ID='L02A' OR CODE_ID='L02B' OR CODE_ID='L02C' OR CODE_ID='L02E' OR CODE_ID='L02N' group by CODE_ID,NAME_CODE "); # your query here
$sth->execute( );
$out = DBIx::Dump->new('format' => csv, # excel or csv
'output' => $FILENAME, # file to save as
'sth' => $sth);
error coming as bareword found near "TOTAL SUM" ..
what is the error..?
Escape the quotes:
$sth = $dbh->prepare("select CODE_ID,NAME_CODE,SUM(INR_COL + OUT_COL) AS \"TOTAL SUM\" ........
or use single quotes:
$sth = $dbh->prepare('select CODE_ID,NAME_CODE,SUM(INR_COL + OUT_COL) AS "TOTAL SUM" ........');

Case Statement In Update Query

This works in SQL Server, but anytime I try to run it in my powershell environment it gives me an error of incorrect syntax near d. What must I alter in order to have this run in powershell?
Function Run-Query
{
param([string[]]$queries)
$server='Server'
$dbname='Database'
$connStr='Server={0};Database={1};Integrated Security=SSPI;' -f $server,$dbname
For ($i = 0; $i -lt $queries.count; $i++)
{
$Command = New-Object System.Data.SqlClient.SqlCommand
$conn=New-Object System.Data.SqlClient.SQLConnection($connStr)
$conn.Open()
$Command.Connection = $conn
$Command.CommandText=$queries[$i]
$Command.ExecuteNonQuery()
$conn.Close()
}
}
$updatequery = "UPDATE d
SET [updatedate] = GETDate(),
[shipped] =
(
CASE WHEN NOT EXISTS(Select 1 *
from [server].[db].[dbo].[viewname] vw
WHERE vw.customername = d.customername)
THEN 'Yes'
ELSE
'PreviousCustomer'
END
)
FROM $fulllocation
WHERE ([shipped] IS NULL)
AND ([managerapproved] IS NOT NULL)
AND [readytoship] IN ('Go', 'Ready', 'Send')"
Run-Query -queries $updatequery
Define your query as a string literal like this:
$updatequery = #"
UPDATE d
SET [updatedate] = GETDate(),
[shipped] =
(
CASE WHEN NOT EXISTS(Select 1
from [server].[db].[dbo].[viewname] vw
WHERE vw.customername = d.customername)
THEN 'Yes'
ELSE
'PreviousCustomer'
END
)
FROM $fulllocation
WHERE ([shipped] IS NULL)
AND ([managerapproved] IS NOT NULL)
AND [readytoship] IN ('Go', 'Ready', 'Send')
"#

How to use postgres dbd placeholders for DB Object names

(or How to iterate thru information schema Using perl DBI (DBD::PG) and placeholders?)
Windows 7, ActiveState Perl 5.20.2, PostgreSQL 9.4.1 .
Cases A, B and C below were successful when using a placeholder for a COLUMN VALUE. In order
no placeholder used
passed a literal
passed a variable (populated with same literal)
It would be great to raise it up a level to DB Objects.. (tables, views etc)
Here's the output with the error for Case D:
Z:\CTAM\data_threat_mapping\DB Stats\perl scripts>test_placeholder.pl
A Row Count: 1
B Row Count: 1
C Row Count: 1
DBD::Pg::st execute failed: ERROR: syntax error at or near "$1"
LINE 1: SELECT COUNT(*) FROM $1 WHERE status = 'Draft';
^ at Z:\CTAM\data_threat_mapping\DB Stats\perl
scripts\test_placeholder.pl line 34.
Much obliged for any direction!
#!/usr/bin/perl -w
use strict;
use diagnostics;
use DBI;
my $num_rows = 0;
# connect
my $dbh = DBI->connect("DBI:Pg:dbname=CTAM;host=localhost",
"postgres", "xxxxx",
{ 'RaiseError' => 1, pg_server_prepare => 1 });
#---------------------
# A - success
my $sthA = $dbh->prepare(
"SELECT COUNT(*) FROM cwe_compound_element WHERE status = 'Draft';"
);
$sthA->execute(); # no placeholders
#---------------------
# B - success
my $sthB = $dbh->prepare (
"SELECT COUNT(*) FROM cwe_compound_element WHERE status = ?;"
);
$sthB->execute('Draft'); # pass 'Draft' to placeholder
#---------------------
# C - success
my $status_value = 'Draft';
my $sthC = $dbh->prepare(
"SELECT COUNT(*) FROM cwe_compound_element WHERE status = ?;"
);
$sthC->execute($status_value); # pass variable (column value) to placeholder
#---------------------
# D - failure
my $sthD = $dbh->prepare(
"SELECT COUNT(*) FROM ? WHERE status = 'Draft';"
);
$sthD->execute('cwe_compound_element'); # pass tablename to placeholder
I've tried single/double/sans quotes (q, qq)...
If
SELECT * FROM Foo WHERE field = ?
means
SELECT * FROM Foo WHERE field = 'val'
then
SELECT * FROM ?
means
SELECT * FROM 'Table'
and that's obviously wrong. Placeholders can only be used in an expression. Fix:
my $sthD = $dbh->prepare("
SELECT COUNT(*)
FROM ".$dbh->quote_identifier($table)."
WHERE status = 'Draft'
");
$sthD->execute();

OR clause in Zend DB update?

I'd like to do a Zend db update with an OR clause. What would be the equivalent statement to:
UPDATE mail
SET message_read = 1
WHERE id = 5
OR id = 10
When calling Zend_Db_Adapter::update(), multiple WHERE conditions will automatically be combined using AND (line 698 of Zend/Db/Adapter/Abstract.php in function _whereExpr).
You can get around this by creating your own Zend_Db_Expr which you will use as the WHERE condition and it will be left untouched.
For example:
$where[] = new Zend_Db_Expr(
$table->getAdapter()->quoteInto('id = ?', 5) . ' OR ' .
$table->getAdapter()->quoteInto('id = ?', 10)
);
// resulting expression:
// WHERE (id = 5 OR id = 10)
$table->update($data, $where);
If you had additional WHERE conditions, they would be combined with the OR condition by an AND.
Example:
$where[] = new Zend_Db_Expr(
$table->getAdapter()->quoteInto('id = ?', 5) . ' OR ' .
$table->getAdapter()->quoteInto('id = ?', 10)
);
$where[] = $table->getAdapter()->quoteInto('type = ?', 'sometype');
// resulting expression:
// WHERE (id = 5 OR id = 10) AND (type = 'sometype')
->where() will add a where clause to the query and will put an 'AND'. There is an orWhere method that exists to do that.
$select = $this->select();
$select->where('id = 5');
$select->orWhere('id = 10');
$this->fetchAll($select);