0

我正在尝试编写我的新框架并练习我的技能,我想按他们的名字选择文章index.php?article=the name of the article

然后我尝试选择它

    $this->db->connect();

    //sanitize data
    $title = $this->db->escape($title);

    $this->db->prepare("
    SELECT
        `date`, `title`, `content`, `author`
    FROM
        `articles`
    WHERE
        `title` like '%$title%'

    LIMIT 1 ;  ");

    //execute query
    $this->db->query();
    $article = $this->db->fetch('array');
    return $article;

这没用...

当我尝试直接在数据库上运行查询时 - 它不起作用(返回 0 行)(见下文)

SELECT 
    `date`, `title`, `content`, `author`
FROM
    `articles`
WHERE
    `title` like '%How to generate Lorem Ipsum%';

数据库示例:

SET @saved_cs_client     = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `articles` (
   `id` int(11) NOT NULL auto_increment,
   `date` varchar(25) NOT NULL,
   `title` varchar(50) NOT NULL,
   `content` text NOT NULL,
   `author` varchar(100) NOT NULL,
   PRIMARY KEY  (`id`)
 ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

LOCK TABLES `articles` WRITE;
/*!40000 ALTER TABLE `articles` DISABLE KEYS */;
INSERT INTO `articles` VALUES
  (1,'Dec 12, 2008','How to generate Lorem Ipsum','Nam accumsan enim tristique urna commodo mollis. Etiam eget leo est. Donec tincidunt quam nec nulla pulvinar sed tristique lorem tincidunt. Pellentesque nibh lectus; suscipit sed ullamcorper sed, laoreet ut tortor. Morbi ut ante tellus. Integer vitae felis id justo tempor adipiscing. Curabitur eget ipsum et urna ultricies pulvinar. Fusce enim dolor, interdum eu egestas vel, iaculis eget nisl. Aenean pretium diam accumsan quam tincidunt sit amet dictum lorem scelerisque. In gravida ultricies aliquet. Phasellus porta erat vel augue sodales feugiat! Pellentesque mattis malesuada ultrices. Mauris eleifend mi quis arcu tincidunt vehicula! Nam sodales commodo lacus, et commodo metus venenatis vel. Sed mollis molestie congue. Nulla ante leo, aliquet et convallis sed; consequat sed turpis. Duis augue leo, adipiscing at venenatis eget, eleifend vitae velit! ','John Squibb'),
  (2,'Jan 03, 1988','Using __autoload','Now in order to try out our new library and driver setup, we have to first make some changes to the way files are served in our framework. Open up the router.php file located in the controllers folder that we created in the first part of this tutorial. If we look at our __autoload function we\'ll see the code we wrote to handle the \'lazy loading\' of our models. Since we used the same naming convention for our libraries and drivers, a quick modification to this code will allow us to load those as easily.','Frank Rabbit');
4

2 回答 2

0

如果您已经设置$_GET['article']$titles,除了您选择了不正确的表或数据库之外,我没有看到任何问题。参考您的连接字符串并检查它是否是正确的数据库。检查您是否在本地或远程数据库中。

编辑1;也许改变title like '%$title%'有用title like '%".$title."%'

于 2013-04-26T08:51:43.437 回答
0

实际上,您准备查询的方式毫无意义。当您需要在其上绑定变量时,使用准备语句。你可以简单地这样做:

$this->db->select('date,title,content,author')
         ->like('title',$title, 'both')
         ->get('articles');

但是,如果你想准备声明,试试这个:

$sql = "SELECT date, title, content, author FROM articles 
        WHERE title like %?% limit 1";
$this->db->query($sql, array($title)); 
于 2013-04-26T08:54:29.817 回答