I trying to return an sql query with the code below. For some reason the boolean values I'm trying to filter by ('ShowOnHomePage' and 'Published') are throwing the following syntax error due to the question mark placeholder.
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 '?) AND ("DummyContentOne"."ShowOnHomePage" = ?)) "DummyContentOne" UNION ALL (S' at line 5
public function getSiteContent() {
if($this->_contentList!=false) {
return $this->_contentList;
}
$request = (Controller::has_curr() ? Controller::curr()->getRequest() : null);
$start = round(($request ? intval($request->getVar('start')) : 0));
$DummyContentOne = DummyContentOne::get()->filter('Published', true)->filter('ShowOnHomePage', true)->dataQuery()->query()->setOrderBy(null);
$DummyContentTwo = DummyContentTwo::get()->filter('Published', true)->filter('ShowOnHomePage', true)->dataQuery()->query()->setOrderBy(null);
$this->fixQueryColumns($DummyContentOne, $DummyContentTwo);
$db = DB::query(
'SELECT * '.
'FROM ('.$DummyContentOne->sql().') "DummyContentOne" '.
'UNION ALL ('.$DummyContentTwo->sql().') '.
'ORDER BY "Date" DESC, "Created" DESC '.
'LIMIT '.$this->config()->item_count.' OFFSET '.$start
);
// Merge into an array list
$list = new ArrayList();
foreach($db as $row) {
$className=$row['ClassName'];
$list->push(new $className($row, false, DataModel::inst()));
}
// Calculate the total number of items
$totalItems = $DummyContentOne->count() + $DummyContentTwo->count();
// Convert to a paginated list
$pagedList=ActionPaginatedList::create($list, $this->Link('more-content'), $request)
->setPageLength($this->config()->item_count)
->setPageStart($start)
->setTotalItems($totalItems)
->setLimitItems(false);
return $pagedList;
}
...
private function fixQueryColumns() {
$queries = func_get_args();
$columns = array();
// Debug::show($queries);
// Get all of the columns from each query
foreach($queries as $query) {
$columns = array_merge($columns, array_keys($query->getSelect()));
}
...
Any ideas what would cause this?
I'm using SS3.2
Thanks
Related
I'm trying to get all item series from a deposit but the deposit id is on a related table. On this function i'll list all the series between two sequences requested
public function getSeries($params = [])
{
$data = collect($params);
$inicio = $data->only('serie_in');
$fim = $data->only('serie_fi');
$depOrigem = $data->only('id_origem');
$series = [];
for($i = $inicio; $i <= $fim; ++$i)
{
$item = Item::leftJoin('produtoItem', 'produtoItem.id', '=', 'item.id_produtoItem')->where('serie', $i)->where('produtoItem.id_deposito', $depOrigem)->first();
if(empty($item))
{
$status = 'I';
}
else
{
$status = $item->status;
}
$series[] = [
'serie' => $i,
'status' => $status
];
}
$result = [
'status' => true,
'data' => $series
];
return $result;
}
This is the error message, i'm thinking that must be as sintax error, but a dont realy know what? Can you help me?
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 2031 (SQL: select * from item left join produtoItem on produtoItem.id = item.id_produtoItem where serie = ? limit 1) in file /usr/share/nginx/html/controleestoque/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671
I was calling the request on my controller without a validator, so i rewrite the getSeries method.
public function getSeries(Request $request){
$data = $request->only(['serie_in', 'serie_fi', 'id_origem']);
$result = $this->produto_service->getSeries($data);
return response()->json($result);
}
And my function work it !
I'm working on a system that has several server-side datatables but i facing issues with 2 joins when i try to order de columns.
I receive the following message when try to sort the columns:
Query error: Column 'notes' in order clause is ambiguous - Invalid query: SELECT *
FROM `tbl_project`
LEFT JOIN `tbl_client` ON `tbl_project`.`client_id`=`tbl_client`.`client_id`
LEFT JOIN `tbl_account_details` ON `tbl_project`.`created_by` = `tbl_account_details`.`user_id`
LEFT JOIN `tbl_notes` ON `tbl_project`.`notes` = `tbl_notes`.`notes_id`
WHERE `tbl_project`.`client_id` = '100'
ORDER BY `notes` DESC
LIMIT 10
This is the code with my query:
$id = $this->input->post("client_id");
$client_details = get_row('tbl_client', array('client_id' => $id));
$draw = intval($this->input->post("draw"));
$start = intval($this->input->post("start"));
$length = intval($this->input->post("length"));
$order = $this->input->post("order");
$search= $this->input->post("search");
$search = $search['value'];
$col = 0;
$dir = "";
if(!empty($order))
{
foreach($order as $o)
{
$col = $o['column'];
$dir= $o['dir'];
}
}
if($dir != "desc" && $dir != "desc")
{
$dir = "desc";
}
$valid_columns = array(
0=>'project_id',
1=>'client',
2=>'fullname',
3=>'notes',
4=>'origen',
5=>'end_date',
6=>'project_status',
7=>'action',
);
if(!isset($valid_columns[$col]))
{
$order = null;
}
else
{
$order = $valid_columns[$col];
}
if($order !=null)
{
$this->db->order_by($order, $dir);
}
$searchQuery = "";
if($search != ''){
$searchQuery = " (tbl_project.project_id like'%".$search."%' OR tbl_project.end_date like'%".$search."%' OR tbl_project.project_status like'%".$search."%' OR tbl_notes.notes like'%".$search."%' OR tbl_notes.eco like'%".$search."%' OR tbl_account_details.origen like'%".$search."%' OR tbl_client.name like'%".$search."%') ";
}
$this->db->select('*');
$this->db->from('tbl_project');
$this->db->join('tbl_client', 'tbl_project.client_id=tbl_client.client_id','left');
$this->db->join('tbl_account_details', 'tbl_project.created_by = tbl_account_details.user_id','left');
$this->db->join('tbl_notes', 'tbl_project.notes = tbl_notes.notes_id','left');
$this->db->where('tbl_project.client_id', $client_details->client_id);
if($searchQuery != '')
$this->db->where($searchQuery);
$this->db->limit($length,$start);
$cita = $this->db->get()->result();
For some reason the ORDER BY is not set as tbl_notes.notes
Any suggestion on how to fix this?
Thanks in advance
EDIT: i have added more code so there is more visibility of the process
The error occurs, because your column name is not unique, it exists in more than one table.
append the table name of the searched column to your query to make it unique:
for example in this line:
$this->db->order_by('my_table_name.'.$order, $dir);
that would generate something like
ORDER BY `my_table_name.notes` DESC
edit: or in case you have to address columns from several different tables you could change your $valid_columns array:
$valid_columns = array(
0=>'my_table_name1.project_id',
1=>'my_table_name2.client',
2=>'my_table_name2.fullname',
3=>'my_table_name3.notes',
// etc.
);
and maintain the remaining original code.
I have a PDO class so I can easily switch between database types.
//*** Function: createQuery, Purpose: Create the query object ***
function createQuery($theQuery)
{
$theQuery = str_replace('`', "'", $theQuery);
$this->query = $theQuery;
$this->sth = $this->pdo->prepare($theQuery, array(PDO::ATTR_CURSOR
=> PDO::CURSOR_FWDONLY));
}
//*** Function: query, Purpose: Execute the PDO ***
function query($paramArray = [], $column = null)
{
try
{
if(!empty($paramArray))
foreach ($paramArray as $key => &$val)
{ $this->sth->bindParam($key, $val);}
$this->sth->execute();
}
catch(PDOException $e) { $this->error_catcher( $e->getMessage());}
if($column != null)
{
$column = ($this->sth->columnCount() < $column) ? 0 : $column;
return $this->sth->fetchAll( PDO::FETCH_COLUMN, $column );
}
else
return $this->sth->fetchAll();
}
and it all works spiffingly well. However, sometimes, if the result is NULL the code throws an exception
here is a typical query to build a menu :
SELECT TOP (100) PERCENT caption, id
FROM dbo.com_menuitems
WHERE (suite = 'chrimbotu') AND (parentid = 0) AND (enabled = 1) AND (id IN
(
SELECT parentid AS id FROM dbo.com_menuitems AS com_menuitems_1
WHERE (suite = 'chrimbotu') AND (perms <= - 1)GROUP BY parentid
HAVING (parentid > 0))
)
ORDER BY parentid, vieworder, caption
and i load the query (from another script) like this :
function getTopLevel($mySQL)
{
$this->_myConn->createQuery($mySQL);
$result = $this->_myConn->query() or die (DumpSQL($mySQL).__FUNCTION__ .' Query failed.');
$this->_topLevel = $result[0]['perms'];
}
If I run the query with no 'chrimbotu' in the database, SSMS says [NULL, NULL] my script carks and throws the helpful error "Query failed".
If I add 'chrimbotu' to the database, but give it a perms of 9, SSMS says [NULL, NULL] but my script is happy.
Both queries return a NULL, but is one more "NULLER" than the other?
FYI: 0 is the lowest permission, < 0 is "everybody" stuff
I'm very much a beginner at Drupal development, MySQL, and pretty much everything, so this may seem trivial, but I would really appreciate any help I can get here.
I'm getting this error, and I'm not really sure what's wrong, because everything was working fine before, and I didn't change anything around the area that it mentions since the last time it was working perfectly.
The exact error is:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'ASC' at line 3: SELECT n.nid AS nid FROM {node} n INNER JOIN {embryo_log} a ON n.nid = a.nid WHERE (n.type = :db_condition_placeholder_0) AND (a.expiration_date > :db_condition_placeholder_1_0) ORDER BY n.sticky DESC, n.changed DESC ASC; Array ( [:db_condition_placeholder_0] => embryo_log [:db_condition_placeholder_1_0] => -1 ) in embryo_log_all() (line 599 of /data/www/drupal-7.34/sites/all/modules/embryo_log/embryo_log.module)."
Here is the entire function that the error is referencing, because I don't know what might be relevant:
function embryo_log_all($option = NULL, $action = NULL) {
$nodes_per_page = variable_get('embryo_log_per_page', variable_get('default_nodes_main', 10));
$output = '<div class="embryo_log">';
// If the optional param is numeric, assume it is a node or term id.
if (is_numeric($option)) {
if ($action == 'group') {
// Get all embryo_log with this term.
// TODO Change fully to a dynamic query
$result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {taxonomy_index} tn USING (nid) WHERE n.type = :n.type AND n.status = :n.status AND tn.tid = :tn.tid", array(':n.type' => 'embryo_log', ':n.status' => 1, ':tn.tid' => $option));
foreach ($result as $nid) {
$embryo_log = node_load($nid->nid);
$output .= '<h2>' . check_plain($embryo_log->title) . '</h2>';
$build = node_view($embryo_log);
$output .= drupal_render($build);
}
return $output . '</div>';
}
else {
$embryo_log = node_load($option);
// If it is an embryo_log, ok, else ignore it.
if ($embryo_log != NULL) {
if ($embryo_log->type == 'embryo_log') {
$build = node_view($embryo_log, 'full');
// Plain node title
$outp = '<h2>' . check_plain($embryo_log->title) . '</h2>';
$build['#node']->title = ''; // Don't render title
return $outp . drupal_render($build);
}
}
}
}
if (user_access('edit embryo_log')) {
$args = array(-1);
}
else {
$args = array(gmdate("U"));
}
// $query = "SELECT n.nid FROM {node} n INNER JOIN {embryo_log} a ON n.nid=a.nid
// WHERE n.type='embryo_log' AND a.expiration_date > %d
// ORDER BY " . variable_get('embryo_log_page_order', 'n.sticky DESC, n.changed DESC');
$query = db_select('node', 'n');
$query->extend('PagerDefault')
->limit($nodes_per_page);
$query->innerJoin('embryo_log', 'a', 'n.nid = a.nid');
$query->condition('n.type', 'embryo_log', '=')
->condition('a.expiration_date', $args, '>')
->fields('n', array('nid'))
->orderBy(variable_get('embryo_log_page_order', 'n.sticky, n.changed'), '');
// $query_result = pager_query(db_rewrite_sql($query, 'n', 'nid'), $nodes_per_page, 0, NULL, $args);
$query_result = $query->execute();
foreach ($query_result as $nid) {
$embryo_log = node_load($nid->nid);
$build = node_view($embryo_log, $option);
$output .= drupal_render($build);
}
$output .= '</div>';
$output .= theme('pager', array('tags' => NULL));
return $output;
}
And the line in there that is specifically being referenced in the error (line 599) is the one towards the bottom that says $query_result = $query->execute();
Any help would be greatly appreciated, because everything looks fine to me, and I'm at a loss.
Thanks!
I'm currently building my first PHP application. I want to read in bus times from a csv file and give back ti users the next bus from their residence to our university. It is my first try with PHP, so be gentle:
<?php
// configuration
require("../includes/config.php");
if (!empty($_SESSION["id"]))
{
//lookup database entries for house
$users = query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);
if ($users === false)
{
apologize("Sorry, there was an error");
}
//lookup database entries for house
$residences = query("SELECT * FROM residences WHERE id = ?", $users[0]["residence"]);
if ($residences === false)
{
apologize("Sorry, there was an error");
}
//if user specified a residence in his profile
if($residences[0]["id"] != 0)
{
$times = array();
//if there is no bus today, in this case sat and sun
if(date( "w", $timestamp) == 0 || date( "w", $timestamp) == 6)
{
$times[0] = "There is no bus today";
}
//load the busplan for his residence
else
{
//open file and load in array if time is higher than date("His");
$timesList = file_get_contents($users[0]["residence"] . ".csv");
$nextbuses = explode(',', $timesList);
$hoursMins = date("Gi");
$num = 0;
for($i = 0; $i < count($nextbuses); $i++)
{
if($hoursMins < $nextbuses[$i])
{
$times[$num] = $nextbuses[$i];
$num++;
}
}
}
render("shuttle_show.php", ["title" => "Next Shuttle from your residence.", "times" => $times]);
}
}
This uses the function query:
function query(/* $sql [, ... ] */)
{
// SQL statement
$sql = func_get_arg(0);
// parameters, if any
$parameters = array_slice(func_get_args(), 1);
// try to connect to database
static $handle;
if (!isset($handle))
{
try
{
// connect to database
$handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD);
// ensure that PDO::prepare returns false when passed invalid SQL
$handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (Exception $e)
{
// trigger (big, orange) error
trigger_error($e->getMessage(), E_USER_ERROR);
exit;
}
}
// prepare SQL statement
$statement = $handle->prepare($sql);
if ($statement === false)
{
// trigger (big, orange) error
trigger_error($handle->errorInfo()[2], E_USER_ERROR);
exit;
}
// execute SQL statement
$results = $statement->execute($parameters);
// return result set's rows, if any
if ($results !== false)
{
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
else
{
return false;
}
}
the other functions it uses are not relevant I guess. Now I cant seem to find why this keeps producing:
Notice: Undefined offset: 0 in /Applications/MAMP/htdocs/html/shuttle.php on line 16
Notice: Undefined offset: 0 in /Applications/MAMP/htdocs/html/shuttle.php on line 22
The relevant lines are
$residences = query("SELECT * FROM residences WHERE id = ?", $users[0]["residence"]);
and
if($residences[0]["id"] != 0)
Would appreciate some help! :)
Edit:
I transferred the same file to my linux system and without any changes I get a vardump. If I use the same vardump on MAMP on my Mac the array is empty. Now I get for:
var_dump($users);
array (size=1)
0 =>
array (size=5)
'id' => int 12
'username' => string 'frechdaxx' (length=9)
'mail' => string '*************#gmail.com' (length=23)
'hash' => string '$1$qr5axj4C$BET5zZGJza2DcHI8eD8fV0' (length=34)
'residence' => int 9573
Why is this a problem at all? the function query worked with the exact same syntax before when I accessed the user table and as we can see it gives back all other values correctly.
Why the difference between my environments? The array is empty on my MAMP server, but works on Linux. However, other code examples with the query function work perfectly on both environments
Why the big int of 9573? The value in the table is 2.
This is simply that $users[0]["residence"] doesn't exists (is not set). You should check it before attempting to use it. As you can see, it's only a notice, not a error. This means that in some case scenarios, the variable can be unset and PHP won't complain THAT much, but this is definitely not good as a general rule.
I would change it to:
$users = query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);
if (empty($users[0]["residence"]))
{
apologize("Sorry, there was an error");
}
Furthermore, that only "fixes" it. If you want to go to the root of the problem, it's in the query() function that is called in $users = query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);:
// execute SQL statement
$results = $statement->execute($parameters);
// return result set's rows, if any
if ($results !== false)
{
$fetched = $statement->fetchAll(PDO::FETCH_ASSOC);
// Check that ther was actually something fetched
if(!empty($fetched))
{
return $fetched;
}
}
/* You don't need to return false, null is the default returned value.
* If you *want* it, then delete the else. */
}
From the documentation, fetchall returns an empty array if there's nothing, so the function might return an empty array.
$users = query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);
if ($users === false) { }
You are strictly checking if query returned false, that only occurs when something was wrong with query or parameters. That does not check if any result was returned, and you are referring to the first returned record ($users[0]["residence"]) that is not present ($users is an empty array).
Since user id comes from $_SESSION["id"] it should be present and available in database, but there is possibility that somewhere in your code you are overwriting it with other id.