I am facing very weird condition right now. I wrote a query in CodeIgniter with a WHERE condition like this:
$queryps = $this->db->query("SELECT count(workorderno) as total from crm_workorder where workorderno =".$sitecode."");
But I am getting this 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 '' at line 1
SELECT count(workorderno) as total from crm_workorder where
workorderno =
Now the weird thing is this the variable $sitecode is not blank. When I echo the query, it shows this:
SELECT count(workorderno) as total from crm_workorder where workorderno =2
But in SQL query, I am getting above error. There is nothing in WHERE condition.
I tried every possible way to find out the reason behind it but I am not able to figure out this. Thanks.
This is what you need, this must be in your model.
<?php
$this->db->select("SELECT count(workorderno) as total");
$this->db->from("crm_workorder");
$this->db->where("workorderno",$sitecode);
$queryps = $this->db->get();
?>
try this
$this->db->select('count(workorderno) as total');
$this->db->from("crm_workorder");
$this->db->where("workorderno",$sitecode);
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
$row = $query->row_array();
print_r($row);
}
$queryps = $this->db->query("SELECT COUNT(workorderno) AS total FROM crm_workorder WHERE workorderno=$sitecode");
And make sure that $sitecode has a value.
TESTED
ok try this code. your error will be solve.
$queryps = $this->db->query("SELECT count(workorderno) as total from crm_workorder where workorderno ='$sitecode'");
Related
By executing below query i getting this error message
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'status=1' at line 1 SELECT username,useremail FROM tbl_cart where user_id= 8AND status=1
query
$query = $this->db->query('SELECT username,useremail FROM tbl_cart where
user_id= '.$this->session->userdata('userId').'AND status=1' );
$resultdata['results'] = $query->result_array();
You need to insert an space before AND
$query = $this->db->query('SELECT username,useremail FROM tbl_cart where
user_id= '.$this->session->userdata('userId').' AND status=1' );
^ here
The better way to do it with CI is
$query = $this->db->select('username, useremail')
->where('user_id', $this->session->userdata('userID'))
->where('status',1)
//instead of from CI does get
->get('tbl_cart')->result_array();
result_array() returns the results as an array of arrays while result returns query as an array of objects
if you are using CI it is better to fully take advantage of the query builder classes
I'm working at summing two different rows and their field values (forum_total_topics and forum_total_posts) from a MySQL database in ExpressionEngine using the Active Record Class provided. I have tried multiple versions of code to sum the numbers. I have attempted and failed at passing a MySQL query to the request by saying.
$SQL = "SELECT forum_id, sum(forum_total_topics) + sum(forum_total_posts)
FROM exp_forums"
ee()->db->select($SQL);
$query = ee->db->get('exp_forums');
echo $query;
to echo the total sum, and getting the following error:
Error Number: 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 '' at line 2
SELECT (SELECT SUM(forum_total_topics) + SUM(forum_total_posts) FROM (exp_forums)
A messy solution
So I've tried splitting the entire request up to find a solution.
I finally got the follow piece of code working by returning multiple arrays and summing those, but it looks quite messy. How, can I return and sum the two rows from the table in one line or so?
$topics = ee()->db->select_sum('forum_total_topics')->get('exp_forums');
foreach($topics->result_array() as $topicrow) {
$totalTopics = $topicrow['forum_total_topics'];
}
$posts = ee()->db->select_sum('forum_total_posts')->get('exp_forums');
foreach($posts->result_array() as $postrow) {
$totalPosts = $postrow['forum_total_posts'];
}
$total = $totalTopics + $totalPosts;
Any suggestions would be greatly appreciated!
Tried suggestions
I attempted suggestions like so,
$SQL = "SELECT forum_id, SUM(forum_total_topics + forum_total_posts) AS total
FROM exp_forums
GROUP BY forum_id";
$query = ee()->db->select($SQL);
echo $query;
With this error instead.
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_driver could not be converted to string
Filename: libraries/Functions.php(679) : eval()'d code
Line Number: 98
Try it like this instead
SELECT forum_id, sum(forum_total_topics+forum_total_posts)
FROM exp_forums
GROUP BY forum_id
sqlfiddle demo
The first thing that you tried does not work due to a small syntax error I believe...
It should be:
$SQL = "forum_id, sum(forum_total_topics) + sum(forum_total_posts) FROM exp_forums"
The initial SELECT is being added in your class function so it should not appear in your $SQL:
ee()->db->select($SQL);
That's why your error starts:
SELECT (SELECT
This is also the case with the code under "Tried Suggestions".
Try this
SELECT forum_id, SUM(forum_total_topics + forum_total_posts) AS total
FROM exp_forums
GROUP BY forum_id
I wanted to use the CI Active Record methods to perform this query, but it gives me different results than if I execute the query in plain SQL. This is the query:
SELECT B.id as id
FROM default_log_workout A, default_log_workout B
WHERE A.id=$id AND B.completed < A.completed AND A.workout_id=B.workout_id
ORDER BY B.completed desc
LIMIT 1
CodeIgniter:
$this->db->select("B.id as id");
$this->db->from("log_workout A, log_workout B");
$this->db->where(array("A.id" => $id, "B.completed < A.completed", "A.workout_id=B.workout_id"));
$this->db->order_by("B.completed desc");
$this->db->limit(1);
$res = $this->db->get();
The query should return the next older row based on a given "id". The plain SQL works, the CI calls end up returning the OLDEST row, not the next oldest. I figure it's just a syntax error in my CI calls... but I can't figure it out. I've since moved on to solve the problem with $this->db->query("the SQL") but this is still bugging me.
Anyone know why the CI version doesn't work?
The ORDER BY clause should have two parameters:
$this->db->order_by('B.completed', 'DESC');
I'm not sure if Active Record will parse your comma separated table string in your from() clause. I suggest rewriting it in an ANSI standard from ... join ... on syntax.
$this->db->select("B.id as id");
$this->db->from("log_workout A");
$this->db->join("log_workout B","A.workout_id = B.workout_id AND B.completed < A.completed");
$this->db->where("A.id" => $id);
$this->db->order_by("B.completed", "desc");
$this->db->limit(1);
$res = $this->db->get();
And, as Yan pointed out, the order by should have two parameters.
I have created a mysql seect statement with a nested select statement. Now my mysql skills (or lack there of) are very limited. Below is the code that I wrote. I was getting blank results or the Warning. mysql_fetch_Array error. Now I am currently receiving an error that says "Subquery returns more than 1 row" Can anyone point me in the right direction on how I can begin to fix this problem. Thanks for the help.
<?php
session_start();
$memberId = $_GET['id'];
$loggedId = $_SESSION['id'];
include('../connect_DB.php');
$sql = 'SELECT bins.tag_Id, tagging_Info.plant_Id, tagging_Info.photo_Id FROM bins inner join tagging_Info on bins.tag_Id = tagging_Info.tag_Id inner join collections on collections.id = bins.collection_Id WHERE collections.member_Id ='.$memberId.' and collections.id=(SELECT id FROM collections where member_Id='.$memberId.')';
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$collection = "Success"; // test to see if working
}
echo $collection;
?>
Have you tried executing just the subquery to see how many records are actually being returned and to ensure that $memberId is actually set?
It would make sense for a user to have multiple collections so you should probably adjust your main query to use IN instead of = on the subquery results.
I am using the get_where() function in codeigniter, and I am getting mysql errors, dependent on what I set the limit and offset too, for example this code,
$this->db->get_where('em_user', $whereArr, 30, 0)->num_rows()
returns a mysql error that looks like this,
Error Number: 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 'WHERE
email = 'your#emailaddress.com' AND
password = 'letmein' LIMIT 1' at
line 2
SELECT * WHERE email =
'your#emailaddress.com' AND
password = 'letmein' LIMIT 1
However if I run this code,
$this->db->get_where('em_user', $whereArr, 30, 30)->num_rows()
it seems to run fine, it seems to run fine, it returns no results but I don not get the error(I assume the no results is because there is an offset of 30 and I only have 2 records in my table).
The sql that this code produces looks like this,
SELECT * FROM (`em_user`) WHERE `email` = 'your#emailaddress.com' AND `password` = 'letmein' LIMIT 30, 30
I dont understand how having a limit of 1 at the end of query can cause so much grief, can anyone enlighten me please?
The line
SELECT * WHERE email = 'your#emailaddress.com' AND password = 'letmein' LIMIT 1
has no FROM clause. I assume it should be:
SELECT * from em_user WHERE email = 'your#emailaddress.com' AND password = 'letmein' LIMIT 1
$this->db->get_where('em_user', $whereArr, 30, 30)->num_rows() won't get any results. num_rows() would give the you the number of results.
I think this error not related to your line of code
$this->db->get_where('em_user', $whereArr, 30, 0)->num_rows()
because in the error message it's appear LIMIT 1 but in your code you limit 30
anyway you can try this line instead of get_where
$this->db->where($whereArr)->limit(30,0)->get('em_user');
and note this line will return num rows not records
Also you can view the query to be sure if it's right or not by add this line after your get_where or query
die($this->db->last_query());
$this->db->select('*');
$this->db->from('em_user');
$this->db->where('email',$email);
$this->db->where('password',$password);
$this->db->limit('30');
$query=$this->db->get();
$result=$query->result();