PHP/SQL Syntax Error 1064 - php

I'm new to PHP so bear with me.
This is the code for entering into a bookings table on my site:
http://pastie.org/8190189
And this is the error I get when I enter data into a form on the front end of the site:
http://pastie.org/8190194
I've been working at it for hours but I can't get anywhere.
I thought the problem may be with the:
SELECT u_id FROM `joom_hl_puser_role` WHERE pid = '1'
The table puser_role just connects joomla users to hotels that they can manage on the site. (I want to enter the hotel manager's joomla user number to the table).
but I'm sure that the syntax is correct.
Thanks for your help.

The problem is your use of TIME();
TIME needs a parameter passed.
mysql> select TIME();
ERROR 1064 (42000): 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
Change it to
TIME(NOW())
From the MySQL manual for TIME
TIME(expr)
Extracts the time part of the time or datetime expression expr and
returns it as a string.
This function is unsafe for statement-based replication. Beginning
with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format is set to STATEMENT. (Bug #47995)
mysql> SELECT TIME('2003-12-31 01:02:03');
-> '01:02:03'
mysql> SELECT TIME('2003-12-31 01:02:03.000123');
-> '01:02:03.000123'

you are in inserting the TIME() and NOW() functions inside "double quotes". try try chaing it to look like this
$query = "INSERT INTO `#__chbookings_bookings` (`id` ,`company` ,`firstname`,`lastname`, ,`email` ,`reference_number` , `transactionid` ,`invoice_number` , `property` ,`payment_type` ,`people` ,`phonenumber` ,`checkin_date` ,`checkout_date`,`room_type` , `status` , `total_amount` ,`amount_paid` ,`pending_amount`, `date_booked`,`last_updated`, `comments`) VALUES (NULL , '".$user_id."','".$data['firstName']."', '".$data['lastName']."',
'".$data['email']."', ".TIME().",
'".$result["transaction_id"]."','".$invoice_number."',
(SELECT u_id FROM `#__hl_puser_role` WHERE pid = '".$data['property_id']."'),
0 ,'".$total_adult."', '".$data['phone']."',
'".$session->get('checkin_date')."', '".$session->get('checkout_date')."', 0, 0,
'".$final_amount."', '".$session->get('amount_payable')."',
'".$session->get('balance_amount')."',
".NOW().", ".NOW().", 'comments'";

Related

Codeigniter $this->db->select() not work with Round(Max()) or FORMAT(MAX())

I write following code
$this->db->select('SUM(qty) as total_qty,(FORMAT(SUM(amount),2)) as total_amount');
$this->db->where('Invoice_Rec_No',$Invoice_Rec_No);
$result=$this->db->get($this->invoice_products_tbl);
$total_data=$result->row();
but getting 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 'FROM (tbl_invoice_products) WHERE Invoice_Rec_No = 7' at line 2
SELECT SUM(qty) as total_qty, (FORMAT(SUM(amount), 2)) as total_amount FROM (tbl_invoice_products) WHERE Invoice_Rec_No = 7
Filename: C:\wamp\www\admin_followme247_master\system\database\DB_driver.php
I want to execute this query with codeigniter ActiveRecord.
User second parameter FALSE in db select method
$this->db->select('SUM(qty) as total_qty,
(FORMAT(SUM(amount),2)) as total_amount', FALSE);
CI db class is automatic add (apostrophe) when manipulate sql query, if you pass send parameterFALSEinselect` method then it is keep same as input.
You can format your code like this
$select = array(
'SUM(qty) as total_qty',
'(FORMAT(SUM(amount),2)) as total_amount'
);
$this->db->select($select);
$this->db->where('Invoice_Rec_No',$Invoice_Rec_No);
$result=$this->db->get($this->invoice_products_tbl);
$total_data=$result->row();
For simple column selection it is fine to use string but for complex selection like this one you can either use an array or select each column separately

Cannot insert column containing colon in column name

I am trying to insert a column with column name 00:18:e7:f9:65:a6 with the statement
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
...but it throws an error:
/* SQL Error (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 '"00:18:e7:f9:65:a6" INT' at line 1 */
in Heidi SQL. I am using MySQL database.
When I try to add the column manually b going to the structure of the table it works and does not give any error but when I run this statement it does not allow me. How can I solve this?
I just ran your query:
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
and got the same error. I then ran:
ALTER IGNORE TABLE tblwifi_information ADD `00:18:e7:f9:65:a6` INT
and it worked. Pretty sure you need to change " to `
Fail with double qoutes:
Success with backticks:
use backticks for columnname
change
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
TO
ALTER IGNORE TABLE tblwifi_information ADD `00:18:e7:f9:65:a6` INT
SQL FIDDLE
To add something to the story, when querying multiple tables, you have to use following expression:
`tablename`.`columnname`
(NOT `tablename.columnname` )
I took me some time to figure it out, so I hope it helps someone :)
Example:
SELECT *
FROM tb_product p
JOIN tb_brand b ON ( `p`.`tb_brand:IDbrand` = `b`.`IDbrand` )
WHERE IDfamily = 2
i dont really know what you are trying to achive here pal.
instead of:
00:18:e7:f9:65:a6
to
00_18_e7_f9_65_a6
then adjust your code to replace ":" to "_" in php or mysql.
in sql:
Replace(table_name, ':', '_');
in PHP:
srt_replace(':','_',$query);

Sphinx RT Index and SphinxQL Query

We are deploying the RT index in our architecture. But we need some clarification and some difficulties faced during the deployment.
Schema defined in Index:
index logtable
{
type = rt
path = /usr/local/sphinx20/var/data/logtable
rt_attr_string = TransactionId
rt_attr_uint = CustomerId
rt_attr_timestamp = DateOfTransaction
rt_attr_string = CustomerFeedback
rt_field = TransactionType
}
Faced Problem
Question 1:
How we can get the count() query result in SPHINXQL. Because its important for us, based on customer count we have to take it to show in our application.
Example below,
Query - select count(*) from logtable where CustomerId='871';
In SphinxQL - We didnt get this result and getting the following error.ERROR 1064 (42000): index logtable: invalid schema: Count(*) or #count is queried, but not available in the schema.
Question 2:
i declared as a STRING attribute in conf for the field of "TransactionId", but i cant able to retrieve the records if that fields use in where condition.
Example below,
select * from logtable where TransactionId='TRA23454';
Following error i am getting,
ERROR 1064 (42000): sphinxql: syntax error, unexpected $undefined, expecting CONST_INT or CONST_FLOAT or '-' near '"TRA23454"'
Please help us to close these issues if knows.
Kumaran
select * from logtable where TransactionId='TRA23454';
Answer :
select * from logtable where MATCH('#TransactionId TRA23454')
In first example instead of count(*) you need to use 'show meta;' query after search query, it will contain total_count field.
select id from logtable where CustomerId='871';
show meta;
In the second example string attributes can't be used in WHERE, ORDER or GROUP clauses.
Actually you need to convert TransactionId into integer and use integer attribute. It is quite simple to do using crc32 mysql function.

CodeIgniter inserting apostrophes in to database query

I have a bit of a strange problem that has been baffling me. All I am trying to do is run a query on a database table but for some reason, CodeIgniter is putting apostrophes into the query which is subsequently breaking the page.
My code looks like this:
$this->db->select("SUBSTRING(body,5)");
$this->db->order_by("date", "desc");
$this->data['query'] = $this->db->get_where('blog-entries', array('status' => 'P'), 3);
But I get an error on this page:
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 'FROM (`blog-entries`) WHERE `status` = 'P' ORDER BY `date` desc LIMIT 3' at line 2
The query is actually being run as:
SELECT SUBSTRING(body, `5)` FROM (`blog-entries`) WHERE `status` = 'P' ORDER BY `date` desc LIMIT 3
As you can see for some reason apostrophes have been added around the number 5 within the substring. If I remove the substring then everything works and if I remove the apostrophes and run the query directly on my db it also works.
Has any got any ideas as to why this may be happening or have a solution?
Your help would be greatly appreciated.
Many thanks,
G.
Use this:
$this->db->select("SUBSTRING(body,5)", FALSE);
As a default, Codeigniter tries to add back-ticks where it thinks is relevant. Sometimes it adds them where it shouldn't. Passing FALSE as the second parameter prevents it from doing this.

php mysql error for creating a table

I got an error while creating a table in php with mysql database, and I tried testing directly on mysql query engine it works fine. whereas in php code it gives below 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.
Below is the code I am writing
$query14 = mysql_query("create table $tablename (
project_id INT,
project_client_id INT,
project_partner_id INT,
project_manager_id INT,
project_employees INT,
project_name VARCHAR(500),
project_status TEXT,
project_summary LONGTEXT,
project_order INT,
project_start_date DATETIME,
project_end_date DATETIME
) ENGINE = INNODB;");
and below is the image attached and table structure should be and this is the sample table i create using with phpmyadmin interface.
And below is the full 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 ''8' (project_id INT, project_client_id INT, project_partner_id INT, project_mana' at line 1`
I didn't see any problem with your query. But what is the value of $tablename? Two error possibilities here :
The variable $tablename is empty.
$tablename is a key-word.
Please check the above two otherwise its all right.
UPDATE :
As per your updated question please try with the following code. I think it will help you.
$query14 = mysql_query("create table `$tablename` (
`project_id` INT,
`project_client_id` INT,
`project_partner_id` INT,
`project_manager_id` INT,
`project_employees` INT,
`project_name` VARCHAR(500),
`project_status` TEXT,
`project_summary` LONGTEXT,
`project_order` INT,
`project_start_date` DATETIME,
`project_end_date` DATETIME
) ENGINE = INNODB;");
Your query syntax is fine, it looks like $tablename is empty.
I think the issue is what is getting replaced for $tablename.
Having the full code example would be more useful in debugging the error.
The mysql create statement works just fine here on mysql 5.1.58 .
As you said in above comments that after echoing the $tablename you got the value 8 then it is not possible to create table.
you have to rename the value of $tablename like tbl_8.
remember with the name tbl-8 your also got error,,
The problem is, $tablename is 8 currently and for SQL table, table name must start with a letter. Current tablename is invalid.

Categories