Simple query and error on Codeigniter - php

I have a problem with CodeIgniter and Mysql. I am getting an error with a very simple query:
$o = "INSERT INTO usuarios (user, password) VALUES ('deesggsd', 'dsggd')";
$query = $this->db->query($o);
$this->db->query($query);
produces:
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 '1' at line 1
1
Filename: C:\wamp\www\newWeb\system\database\DB_driver.php
Line Number: 330
But the query is actually executed; the row appears on the database. What i'm doind wrong?
If I execute the same query on phpmyadmin, all is ok.
Thanks!!!

What you are doing with $query = $this->db->query($o); is executing the query and storing the result to the $query variable. So you've already run the INSERT once which is why it stores properly to the database.
Now when you try to run $this->db->query($query); you're basically trying to run a mysql procedure using the result (TRUE) as your query string. This is where it throws the error. Make sense?
Try doing this instead:
$this->db->insert('usuarios', array(
'user' => 'deesggsd',
'password' => 'dsggd'
);
I suggest looking into Active Record and how interaction between PHP & mysql work in general. No offense but this is a beginner level mistake.

you should use activer recored it's sample and easy,
that will be solve your problem
$values = array('user'=>'deesggsd', 'password'=> 'dsggd');
$this->db->insert('usuarios',$values)
http://ellislab.com/codeigniter/user-guide/database/active_record.html

Related

MySQLi Syntax Error (PHP) on INSERT using Variables

I am attempting to insert some user-inputted data into my MySQL table using the following command:
$sql = "INSERT INTO Queued ('$role') VALUES ('$sname')";
Interestingly enough, I get the following error:
Error: INSERT INTO Queued ('Tops') VALUES ('Summoner')
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 ''Tops') VALUES ('Summoner')' at line 1
To be honest, I am relatively new at using PHP as well as MySQL, but I can't seem to find the error in my syntax; the Queued table does exist, $role and $sname are both strings so I encased them in single quotes. I suspect this is a newbie mistake, could anyone point me in the right direction?
This is due to use of single quotes ' around the column name. The query should be like:
$sql = "INSERT INTO Queued ($role) VALUES ('$sname')";
OR
$sql = "INSERT INTO Queued (`$role`) VALUES ('$sname')";
Try this format
$sql = "INSERT INTO Queued ('".$role."') VALUES ('".$sname."')";
`s role is to differentiate between built in SQL words and the column names, so if a word is used for name of a column that might be also a built in sql expression then `` are needed around it

How to call a stored procedure in CodeIgniter USING SQL SERVER

I'm bulding my application using Codeigniter and SQL server as my database (I am using the SQLSRV PHP extension to connect to SQL ). The problem happens when I try to call Stored Procedures:
$query = $this->db->query(
"EXECUTE verificacion_fechas '".$codigo."',".$estado.",'".$llave_maestra."','".$fecha_actual."'");
Another way I have tried to create the query with less data is the following , however, it is generating an error:
Error Number: 01000 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Ejecutando SQL directamente, sin cursor. EXECUTE provando15 34,2,'key05','2015-07-22'
I dont really know what im doing wrong. Can someone please help me?
remove the word Execute and it will work.
$query = $this->db->query(
"verificacion_fechas '".$codigo."',".$estado.",'".$llave_maestra."','".$fecha_actual."'");
Here's a sample on how to call a MYSQL STORED PROCEDURE in codeigniter
$this->db->query('CALL procedure_name());
Here's my solution:
first way(if you do not use a variable);
$query=$this->MSSQL->query("EXEC ProcedureName");
second way(if you use a variable);
$query =$this->MSSQL->query("EXEC ProcedureName '$var1','$var2','$var3' ");
or
$query =$this->MSSQL->query("EXEC ProcedureName #varName1='$var1',#varName2='$var2',#varName3='$var3' ");
Note:if you get result like empty array add SET NOCOUNT ON to after BEGIN in your procedure.

PHP Codeigniter Active Records Ignore single quote

Hi I have following string to insert into database
$data = array('url' => http://www.amazon.co.uk/hedge-trimmers/s?ie=UTF8&page=1&rh=i:aps,k:'hedge trimmers);
I tired to insert that 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 'hedge trimmers''' at line 1
SELECT * FROM seo_websites WHERE website = 'http://www.amazon.co.uk/hedge-trimmers/s?ie=UTF8&page=1&rh=i:aps,k:'hedge trimmers''
I think it is do with single quotes. Please I would really appreciate if someone can help me.Thank you
You can write your query using Active Record
$url="http://www.amazon.co.uk/hedge-trimmers/s?ie=UTF8&page=1&rh=i:aps,k:'hedge trimmers'";
$this->db->select('*');
$this->db->from('seo_websites');
$this->db->where('website',$url);
$data = array('url' => addslashes("http://www.amazon.co.uk/hedge-trimmers/s?ie=UTF8&page=1&rh=i:aps,k:'hedge trimmers"));

error in mysql insert query in MYSQL 5.6.12

I tried to use the following code to insert,
$op=$_POST["ans"];
$username=$_GET["username"];
mysql_query("insert into $username values('Q3','$op')")
or die(mysql_error());
But I got the following 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 'values('Q1','Wrong')' at line 1
Why am I getting this error? How can I fix it?
Your query structure is not making any sense. You're inserting into $username? That's not the name of the table, is it?
mysql_query("INSERT INTO `tablename` values('Q3','" . mysql_real_escape_string($op) . "')") or die(mysql_error());
Always be very careful to escape any and all user data being put into your queries, and please, please stop using mysql_query in new code.

Help with SHA1 or MD5 in PHP

I have created a form that inserts the entered data into the database. It works perfectly except when I put SHA1('$password') into the INSERT INTO VALUSE tag. If I put only '$password it works fine.
Putting SHA1 displays - 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
Can you help me out.
Thanks
$q = "insert into users (fullname,email,website,username,password) values ('$fn','$e','$w','$u', SHA1('$password')";
$r = mysql_query($q) or die(mysql_error()); //Run the query.
Looks like you are missing a parenthesis in your statement. Try:
$q = "insert into users (fullname,email,website,username,password) values ('$fn','$e','$w','$u', SHA1('$password'))";

Categories