简体   繁体   中英

Simple query and error on Codeigniter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM