简体   繁体   English

PHP - 具有SQL查询的多个函数

[英]PHP - Multiple functions with SQL queries

So I have a PHP file with multiple functions that do different queries, example: 所以我有一个PHP文件,其中包含多个执行不同查询的函数,例如:

function getUserEmail($name)
{
    $link=getLink();
    $name=mysqli_real_escape_string($link, $name);
    $output=mysqli_fetch_array(mysqli_query($link, "SELECT email FROM users WHERE name='$name';"));
    mysqli_close($link);
    return $output[0];
}

function getUserName($id)
{
    $link=getLink();
    $id=mysqli_real_escape_string($link, $id);
    $output=mysqli_fetch_array(mysqli_query($link, "SELECT name FROM users WHERE id='$id';"));
    mysqli_close($link);
    return $output[0];
}

function getLink()
{
    $link = mysqli_connect('localhost', 'myUser', 'myPass');
    mysqli_select_db($link, 'myDB');
    if (!$link) die(mysqli_error($link));
    return $link;
}

(this code might not work, it's just an example) (这段代码可能不起作用,这只是一个例子)

I have it done similar to the example above, but I think because of the many functions being called on a page reload, the multiple DB link creation and closure is not the most efficient way to proceed (and is starting to lag because of the multiple features added to my project). 我已经完成了类似于上面的示例,但我认为由于在页面重新加载时调用了许多函数,因此多个DB链接创建和闭包不是最有效的方法(并且因为多个而开始滞后)添加到我的项目中的功能)。

Ny thought was: creating the $link to the DB on the header of the PHP and saving it to a $_POST var (like $_POST['link']) and then using it through the code, and closing it on the footer. Ny的想法是:在PHP的标题上创建$ DB链接并将其保存到$ _POST var(如$ _POST ['link']),然后通过代码使用它,并在页脚上关闭它。

This would prevent multiple link creations and closures, and I think would improve performance. 这可以防止多个链接创建和关闭,我认为会提高性能。

Any thoughts? 有什么想法吗?

If these functions are part of a class 如果这些函数是类的一部分

you can include the __construct() and '__destruct()` functions where you deal with database connection. 您可以在处理数据库连接的地方包含__construct()和'__destruct()`函数。 For example: 例如:

<?php
  class account {
    protected $link;

    function __construct() {
      $link=getLink();
    }

    function __destruct() {
      mysqli_close($link);
    }

    function getUserEmail($name)
    {
      $name=mysqli_real_escape_string($link, $name);
      $output=mysqli_fetch_array(mysqli_query($link, "SELECT email FROM users WHERE name='$name';"));
      return $output[0];
    }

    function getUserName($id)
    {
      $id=mysqli_real_escape_string($link, $id);
      $output=mysqli_fetch_array(mysqli_query($link, "SELECT name FROM users WHERE id='$id';"));
      return $output[0];
    }

    function getLink()
    {
      $link = mysqli_connect('localhost', 'myUser', 'myPass');
      mysqli_select_db($link, 'myDB');
      if (!$link) die(mysqli_error($link));
      return $link;
    }
  }

That way you don't have to worry about reopening the connection and closing it. 这样您就不必担心重新打开连接并关闭它。 The connection automatically opens when the account object is created and it automatically closes when the account object is no longer being referenced. 创建帐户对象时,连接会自动打开,并且当不再引用帐户对象时,连接会自动关闭。

Read more about constructors and destructors here . 在这里阅读有关构造函数和析构函数的更多信息。

Additionally, as suggested in the comments, you could include a broad getUser() method so that you wouldn't have to query the database for all of the user's separate entries. 此外,正如注释中所建议的那样,您可以包含一个广泛的getUser()方法,这样您就不必在数据库中查询所有用户的单独条目。 It would elliminate the amount of times you interacted with the database and would be a more efficient way to deal with the data. 它会消除您与数据库交互的次数,并且是处理数据的更有效方式。 Like so: 像这样:

function getUser($id)
{
  $id=mysqli_real_escape_string($link, $id);
  $output=mysqli_fetch_array(mysqli_query($link, "SELECT * FROM users WHERE id='$id';"));
  return $output;
}

Use a global 使用全局

$link=false;
function getLink()
    {
      global $link;
      if (!$link) {
      $link = mysqli_connect('localhost', 'myUser', 'myPass');
      mysqli_select_db($link, 'myDB');
      if (!$link) die(mysqli_error($link));
      }
      return $link;
    }

This way, the first call to getLink() establishes a connection, and all subsequent calls simply re-use the connection established by the first call. 这样,第一次调用getLink()建立连接,所有后续调用只是重新使用第一次调用建立的连接。 After making this change, you can keep the rest of your code the same. 进行此更改后,您可以保持其余代码相同。 Being able to use OOP I think is actually a great benefit of using mysqli over mysql , but if you don't want to use OOP this should do it for you. 能够使用OOP我认为使用mysqli不是mysql实际上是一个很大的好处,但是如果你不想使用OOP,那么应该为你做。

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

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