简体   繁体   English

在Codeigniter上的简单查询和错误

[英]Simple query and error on Codeigniter

I have a problem with CodeIgniter and Mysql. 我对CodeIgniter和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. 如果我在phpmyadmin上执行相同的查询,一切正常。

Thanks!!! 谢谢!!!

What you are doing with $query = $this->db->query($o); 你在做什么$query = $this->db->query($o); is executing the query and storing the result to the $query variable. 正在执行查询并将结果存储到$query变量中。 So you've already run the INSERT once which is why it stores properly to the database. 因此,您已经运行了一次INSERT ,这就是为什么它可以正确存储到数据库中的原因。

Now when you try to run $this->db->query($query); 现在,当您尝试运行$this->db->query($query); you're basically trying to run a mysql procedure using the result (TRUE) as your query string. 您基本上是在尝试使用结果(TRUE)作为查询字符串来运行mysql过程。 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. 我建议研究Active Record以及一般情况下PHP和mysql之间的交互方式。 No offense but this is a beginner level mistake. 没有冒犯,但这是初学者级别的错误。

you should use activer recored it's sample and easy, 您应该使用activer recored的示例,简单易行,

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 http://ellislab.com/codeigniter/user-guide/database/active_record.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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