【问题标题】:Using MYSQLI in seperate / distinct PHP functions在单独/不同的 PHP 函数中使用 MYSQLI
【发布时间】:2014-01-28 10:24:00
【问题描述】:

我正在从 MYSQL 过渡到 MYSQLI,需要帮助我将 MYSQLI 放入单独/不同的函数中。

从我在网络上找到的所有“教程”中,它们都包含在一个大长代码中的所有内容,而不是我的主脚本可以调用的不同/单独的函数。

例如:-

  • 连接到 MYSQLI
  • 做选择
  • 退出 MYSQLI

我想要的是:-

MYSQLI.PHP

     <?
     function connect_mysqli()
     {
     $con=mysqli_connect("localhost","wrong_user","my_password","my_db");
     // Check connection
     if (!$con)
     {
     die("Connection error: " . mysqli_connect_errno();
     }
     // Return the connection back to where i called it ??
     }
     function do_query ($sql)
     {
     $row = $con->query("$sql")->fetch_array();
     return $row;
     }
     function close_mysqli()
     {
      $mysqli->close();
     }
     ?>

在我想调用的脚本中:-

另一个.php

      <?
      include_once("MYSQLI.PHP");
       connect_mysqli();

         ....

        do some SELECT
       do some UPDATE

       close_mysqli();
        ?>

到目前为止,从我收到的错误代码来看,与 mysqli 的“连接”没有被传递到/从我的其他脚本中传递

有没有人得到一个使用函数的 mysqli 的工作/测试示例(不仅仅是一半的代码) - 而是一个简单 SELECT 的工作示例

一旦我做到了,我可以做剩下的。

【问题讨论】:

  • 你看过OOP吗?您是否愿意考虑这样做,因为它可以很好地解决您的问题。
  • 你的连接其实是没有通过的。了解变量范围

标签: php sql mysqli


【解决方案1】:

将您的包含文件修复为

/**
 * @return mysqli
 */
function connect_mysqli()
{
    $con = mysqli_connect("localhost","wrong_user","my_password","my_db");
    // Check connection
    if (!$con)
    {
        die("Connection error: " . mysqli_connect_errno());
    }

    return $con;
}

function do_query ($con, $sql)
{
    $row = $con->query("$sql");
    if($row) {
        return $row->fetch_array();
    }
    return null;
}

function close_mysqli($con)
{
    $con->close();
}

现在您可以运行这样的脚本

include_once("MYSQLI.PHP");
$connection = connect_mysqli();

if(null !== $connection) {
    print_r(do_query($connection, "SELECT * FROM yourTable"));

    close_mysqli($connection);
}

但是为了正确处理创建一个连接接口和一个这样的mysqli实现

interface myConnectionClass {
    function connect();
    ....
}

和一个mysqli实现

class myMysqlIConnection implements myConnectionClass {
    function connect() {
       //do more... save connection etc...
       return true; //sucess
    }
}

【讨论】:

  • 你好。你的回答效果很好。除了我不能用“if($row)”子程序得到结果) - 如果我使用下面的代码,它工作正常:) - while($row = mysqli_fetch_array($result)) { print_r($row); }
【解决方案2】:

Example Demos 向下滚动有很多例子......

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!$mysqli->query("SET @a:='this will not work'")) {
        printf("Error: %s\n", $mysqli->error);
    }
    $result->close();
}

$mysqli->close();
?>

【讨论】:

  • 为什么我为他在谷歌上搜索得到-1?我在 5 秒内完成了他自己能够完成的工作。
  • 这应该是一条评论。
  • 但它直接回答了他的问题,并且会完成这个话题,所以不需要创建评论。
  • 它根本没有回答他的问题。你所要做的就是说出他能读到的东西。他想要的是他可以调用的函数,那不是函数。
  • 嗯,它包含了他所要求的“有没有人有一个使用函数的 mysqli 的工作/测试示例(不仅仅是一半的代码) - 而是一个简单 SELECT 的工作示例” - 我应该重新发明车轮?当我把它写出来时,你知道它看起来不会有太大不同......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多