猿问

PHP 将 fetch_field() 更改为 mysqli

我的 php 知识相当有限,但我最近需要将一些网页从旧版本的 php 5.2 更新到 php 7.3。


我已经设法更新了对 mysqli 等的大多数 mysql 引用并让事情正常工作,但是有一个页面使用了日历,我真的在这部分和 fetch_field 部分苦苦挣扎,特别是我的任何例子发现似乎没有类似的格式。


我需要更新的代码如下;


require_once('Connections/connAsh.php');

mysql_select_db($database_connAsh, $connAsh);


function selectonerow($fieldsarray, $table, $uniquefield, $uniquevalue)

{

    //The required fields can be passed as an array with the field names or as a comma separated value string

    if (is_array($fieldsarray)) {

        $fields = implode(", ", $fieldsarray);

    } else {

        $fields = $fieldsarray;

    }

    //performs the query

    $result = mysql_query("SELECT $fields FROM $table WHERE $uniquefield = '$uniquevalue'") or die("Could not perform select query - " . mysql_error());


    $num_rows = mysql_num_rows($result);


    //if query result is empty, returns NULL, otherwise, returns an array containing the selected fields and their values

    if ($num_rows == NULL) {

        return NULL;

    } else {

        $queryresult = array();

        $num_fields = mysql_num_fields($result);

        $i = 0;

        while ($i < $num_fields) {

            $currfield = mysql_fetch_field($result, $i);

            $queryresult[$currfield->name] = mysql_result($result, 0, $currfield->name);

            $i++;

        }

        return $queryresult;

    }

}

我的编辑尝试是;


require_once('../Connections/connAsh.php')

$connAsh->select_db($database_connAsh);

function selectonerow($fieldsarray, $table, $uniquefield, $uniquevalue)

{

    //The required fields can be passed as an array with the field names or as a comma separated value string

    if (is_array($fieldsarray)) {

        $fields = implode(", ", $fieldsarray);

    } else {

        $fields = $fieldsarray;

    }


潇潇雨雨
浏览 101回答 2
2回答

慕田峪4524236

原来的功能在很多层面上都是错误的。重新创建它的功能是没有意义的。基本上你在这里讨价还价的只是几个 SQL 关键字。但这些关键字有助于提高可读性。出于某种原因,您决定智取几代对 SQL 语法非常满意的程序员,并制造难以理解的胡言乱语$row = selectonerow("some, foo, bar", "baz", "id", [$uniquevalue]);而不是几乎自然的英语$row = selectonerow("SELECT some, foo, bar FROM baz WHERE id=?", [$uniquevalue]);来吧。这不值得。让你的函数接受常规的 SQL 查询,而不是有限的难以理解的混乱。function selectonerow(mysqli $conn, string $sql, array $params = []): array{&nbsp; &nbsp; if ($params) {&nbsp; &nbsp; &nbsp; &nbsp; $stmt = $conn->prepare($sql);&nbsp; &nbsp; &nbsp; &nbsp; $stmt = $mysqli->prepare($sql);&nbsp; &nbsp; &nbsp; &nbsp; $stmt->bind_param(str_repeat("s", count($params), ...$params);&nbsp; &nbsp; &nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; &nbsp; &nbsp; $result = $stmt->get_result()&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $result = $conn->query($sql);&nbsp; &nbsp; }&nbsp; &nbsp; return $result->fetch_assoc();}此功能将允许您使用任何查询。例如,需要一个最高价格的行吗?$row = selectonerow("SELECT * FROM baz ORDER BY price DESC LIMIT 1");需要更复杂的条件?没问题$sql = "SELECT * FROM baz WHERE email=? AND activated > ?";$row = selectonerow($sql, [$email, $date]);等等。任何 SQL。任何条件。

小怪兽爱吃肉

我建议去掉这个函数或者用 YCS 建议的函数替换它。如果您真的想继续使用此功能,请考虑以下修复。您使里面的代码变得非常复杂,并且忘记将连接变量传递给函数。我已经简化了它:// open the DB connection properly inside Connections/connAsh.phpmysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);$connAsh = new mysqli('host', 'user', 'pass', $database_connAsh);$connAsh->set_charset('utf8mb4');// your function:function selectonerow(mysqli $connAsh, $fieldsarray, $table, $uniquefield, $uniquevalue): array{&nbsp; &nbsp; //The required fields can be passed as an array with the field names or as a comma separated value string&nbsp; &nbsp; if (is_array($fieldsarray)) {&nbsp; &nbsp; &nbsp; &nbsp; $fields = implode(", ", $fieldsarray);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $fields = $fieldsarray;&nbsp; &nbsp; }&nbsp; &nbsp; //performs the query&nbsp; &nbsp; $stmt = $connAsh->prepare("SELECT $fields FROM $table WHERE $uniquefield = ?");&nbsp; &nbsp; $stmt->bind_param('s', $uniquevalue);&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; return $stmt->get_result()->fetch_assoc();}这是您的功能,但消除了很多噪音。我$connAsh在函数的签名中添加了,所以你每次调用这个函数时都必须传入它。该函数将始终返回一个数组;如果没有获取记录,则数组将为空。这是推荐的方式。还要记住始终使用准备好的语句!
随时随地看视频慕课网APP
我要回答