简体   繁体   English

Mysqli查询一行

[英]Mysqli query in one line

The current procedure I am using to run a simple SQL query is the following: 我当前用于运行简单SQL查询的过程如下:

$a = $mysqli->query("SELECT column FROM table");
$b = $a->fetch_assoc();
$c = $b["column"];

This uses 3 different variable names and it gets frustrating trying to come up with suitable names. 这使用了3个不同的变量名,尝试提供合适的名称会令人沮丧。 I think that in the procedural style, I can run this in two lines as oppose to three: 我认为在程序风格上,我可以分两行运行,而反对三行:

$a = mysql_fetch_assoc(mysql_query("SELECT column FROM table"));
$b = $a["column"];

Is there any approach to use the object-oriented style in just one line of code? 有什么方法可以仅在一行代码中使用面向对象的样式?

Why don't you use OOP that you are already using? 为什么不使用已经在使用的OOP? If I'm not mistaken, you could use the facade design pattern. 如果我没记错的话,可以使用外观设计模式。 This is not exactly one line of code but it is more of a write once. 这不完全是一行代码,而是一次写入。

Create an object to do the querying. 创建一个对象进行查询。


//This is just a rough idea
class SimpleQuery {

    protected $mysqli;

    function __construct() {
       //Get a connection using whatever way you want
    }

    /**
     * @param string $query The query to execute
     * @param string $field The field to return(optional)
     * @return mixed
     */
    function query($query, $field = NULL) {

      $a = $this->mysqli->query($query);
      $b = $a->fetch_assoc($a);

      //Obviously this function needs to be adjusted to do what you seek, get multiple rows perhas
      if ($field) {
         return $b[$field];
      } else {
         return $b;
      }

    }

}

Then you can simply just: 然后,您可以只是:



$simpleQuery = new SimpleQuery();

$dbData = $simpleQuery->query('SELECT column FROM table','column');

Don't know if this works for you but it's worth writing. 不知道这是否对您有用,但是值得写。 The reason objects were created, to be reusable. 创建对象的原因是可重用的。 :-) :-)

Is there any approach to use the object-oriented style in just one line of code? 有什么方法可以仅在一行代码中使用面向对象的样式?

Sure. 当然。
This is what object-oriented style exactly for. 这正是面向对象样式的目的。

However, you don't quite understand the idea of the object-oriented style. 但是,您不太了解面向对象样式的概念。
The idea is, actually, is not to throw some OO-methods together to get some awkward and unreliable construct, but to create a class , which will have methods for all your needs. 实际上,这个想法不是要把一些面向对象的方法放在一起以获得一些笨拙和不可靠的构造,而是要创建一个类该类将具有满足您所有需求的方法。

@Touch's is absolutely right with his answer, but of course it's only a sketch. @Touch的答案绝对正确,但当然只是草图。 Such a class have to be extended to have all the methods you need: 必须扩展此类,以具有所需的所有方法:

Say, to have a column from table, 假设要在表格中添加一列,

$a = $db->getOne("SELECT column FROM table LIMIT 1");

Or to get a whole row: 或获得整行:

$row = $db->getRow("SELECT * FROM table LIMIT 1");

or all the rows 或所有行

$data = $db->getAll("SELECT * FROM table");

You may note that these snippets is not only shorter than silly approaches from other answers, but also have error reporting, query logging, profiling and many other useful things implemented already. 您可能会注意到,这些片段不仅比其他答案中的愚蠢方法要短,而且已经实现了错误报告,查询日志记录,性能分析和许多其他有用的功能。

You should also take into account that placeholders support is a must! 您还应该考虑到必须支持占位符 Otherwise all this stuff will be totally useless . 否则,所有这些东西将完全无用 Note that dedicated OO-based solution is the only way to keep the code in one line: with raw API methods you'll end up not with one but with dozen lines at the very least. 请注意,基于专用于OO的专用解决方案是代码保持在一行中的唯一方法:使用原始API方法,您最终将得到的不是一行,而是至少一行

Yet such a class can keep one-liners all right: 但是,这样的课程可以使一线好:

$name = $db->getOne('SELECT name FROM table WHERE id = ?i',$_GET['id']);
$data = $db->getInd('id','SELECT * FROM ?n WHERE id IN ?a','table', array(1,2));
$data = $db->getAll("SELECT * FROM ?n WHERE mod=?s LIMIT ?i",$table,$mod,$limit);

Here is an example of a class I am talking about. 这是我正在谈论的课程的示例。 , which you can use already. ,您已经可以使用了。

There's nothing stopping you form using 没有什么可以阻止您使用表单

$mysqli->query($query)->fetch_assoc()["column"];

Typical names are: 典型名称为:

  • $a -- $result $a a- $result
  • $b -- $row $b $row
  • $c -- $column $c $column

You should check this query before fetch(); 您应该在fetch()之前检查此查询;

I recommend use this: 我建议使用此:

    $query = mysql_query("SELECT column FROM table");
    if ($query)
    {
       $data = mysql_fetch_assoc($query);
    }
    else
    {
     echo mysql_error($query);
    }

If you will use one line mysql query for fetch result you have a problem with debug if query fails. 如果您将使用一行mysql查询获取结果,则查询失败时将出现调试问题。

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

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