SQL query error, can't figure it out - php

$sql="SELECT count(actid) AS tr
FROM useractions ua
WHERE qid=-1
OR qid IN (
SELECT qid
FROM questions q
WHERE q.visible=".VISIBLE."
)
AND ua.actid =".$actid;
The query above gives 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
Whats wrong with the statement?
I did a dump and got this:
string "SELECT count(actid) AS tr
FROM useractions ua
WHERE qid=-1
OR qid IN (
SELECT qid
FROM questions q
WHERE q.visible=1
)
AND ua.actid =" (length=270)
$actid is the result of another query, shown below. It's then passed to the function that has the query show above.
foreach ($_POST['q'] as $qid) {
list($actid) = mysql_fetch_row(mysql_query("SELECT actid FROM useractions WHERE qid='$qid'"));
upd_facts_status($actid);
}

Verify that the VISIBLE constant has a value and that the $actid variable has a value.

Probalby a typo - VISIBLE is a constant? And if it is a string, it should be in quotes.
You should enable all error messages too:
ini_set("display_errors", true);
error_reporting(E_ALL);

Related

WordPress database error: You have an error in your SQL syntax

Below WordPress database error is coming:
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 '' at line 1 for query SELECT * FROM wp_author_followers WHERE
author_id = made by require('wp-blog-header.php'),
require_once('wp-includes/template-loader.php'),
Here is my code.
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}author_followers WHERE author_id = $author_id", OBJECT );
$followcounter = count($results);
return $followcounter;
Everything was correct, Just Author ID was not there, I have fixed this issue. Thanks for your attention and help.
echo "SELECT * FROM {$wpdb->prefix}author_followers WHERE author_id = $author_id";
Try to echo $author_id before the SQL query, and check SQL statement after replacing $author_id with its value.
Thanks

Mysqli query with zerofill

Hello stackoverflow community, I need help with this mysqli query. My auto increase column in database is ZERO-FILL element for example 00001 so when I try to query like this:
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '".$this->clean_k_name."',
pavard = ".$this->clean_k_surname.",
WHERE korteles_nr = '00001'");
And I get error:
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 'WHERE korteles_nr = 00001' at line 18
I was searching Internet for solving this problem. But can't find so please help!
Query should be like this:-
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '$this->clean_k_name',
pavard = '$this->clean_k_surname'
WHERE
korteles_nr = '00001'");
You have to remove the comma after $this->clean_k_surname variable

SUM two SQL rows with CodeIgniter in EE

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

Use array in where clause not working

Hi I have a query in php file which is used to filter the data in file from mysql database
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = \"69\"";
In this line if Reg_no = \"69\"" , if i change the 69 to any value data is being modified but if i use an array instead of 69 then its not working like this
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = " . $fc . "";
But if i use
$fc = 69;
echo $fc;
Then its working but not on that line please tell me how to code this The error on which i get is
Error
Error while accessing the database:
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(*) from deposit where Reg_no =
from your Reg_no =\"69\""
and your Reg_no =". $fc."";
are you not missing the "" of the $fc
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = \"" . $fc . "\"";
to match your 69 example.
In your original question you stated this error text
Error
Error while accessing the database:
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(*) from deposit where Reg_no =
If the $fc would be an array you would see this in the query as such. If i remember correctly it would look like that ...
Error
Error while accessing the database:
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(*) from deposit where Reg_no = Array
As it does not i assume that the variable $fc is empty. Did you check the variable or better create the query and log it somewhere to check the query as it gets sent to the sql server.
As mentioned, if it would be an array PHP would convert it when wrongly used to the text "Array" which you should find in the query.
i guess you try to do something like
"where Reg_no IN (".implode(",", $fc).")";

MySql Syntax error - 42000

I'm getting this error :
Warning: PDO::query() [pdo.query]: 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 'When, RDV, Comments FROM distributions WHERE IDFond = 1' at line 1 in /Applications/XAMPP/xamppfiles/htdocs/JG/DistributionManager.class.php on line 56
When executing this code :
$Distribution_Manager->getListByFunds($Selected_Fond->id());
foreach ($Distribution_Manager as $Distrib)
{
echo $Distrib->Comments();
}
Here is the concerned function :
public function getListByFunds($FundID)
{
$Distribution = array();
$q = $this->_db->query('SELECT id, IDClient, IDFond, Who, When, RDV, Comments FROM distributions WHERE IDFond = '.$FundID);
while ($donnees = $q->fetch(PDO::FETCH_ASSOC))
{
$Distribution[] = new Distribution($donnees);
}
return $Distribution;
}
Should be a little mistake but I'm lagging on it for almost 50 minutes !
Thanks in advance for the help ;)
WHEN is a mysql reserved word, so try using a different column name or enclose WHEN in backquotes.
When is mysql keyword, try this
SELECT id, IDClient, IDFond, Who, When AS anything..
or enclose this keyword to backquotes
SELECT id, IDClient, IDFond, Who, `When`, RDV..
Give back quotes for column names like id and try it
You're never closing your quotes on your statement.

Categories