gpt4 book ai didi

php - 重写代码 - 不起作用

转载 作者:行者123 更新时间:2023-11-29 08:15:34 24 4
gpt4 key购买 nike

我正在尝试使用 mysqli 函数(代码 2)重写现有代码代码 1。但这不起作用。有人可以帮助解决这个问题吗?

代码 1(旧代码 - 有效)

<?php
if(isset($_POST['thename']) === true && empty($_POST['thename']) === false) {
require'../db/connection.php';

$query = ("SELECT 'photos'.'theurl' FROM 'photos' WHERE 'photos'.'thename' = '" . mysql_real_escape_string(trim($_POST['thename'])) . "'");

echo(mysql_num_rows($query) !== 0) ? mysql_result($query, 0, 'theurl') : 'Not found';

代码 2(新 - 不起作用)

<?php
if(isset($_POST['thename']) === true && empty($_POST['thename']) === false) {
$getVal = mysqli_real_escape_string($Conn_db, trim($_POST['thename']));

require_once('../db/connection.php');
$query = ("SELECT 'photos'.'theurl' FROM 'photos' WHERE 'photos'.'thename' = '" . $getVal . "'");
$result = mysqli_query($Conn_db, $query);

$queryA = ("SELECT id FROM photos");
$resultA = mysqli_query($Conn_db, $queryA);
$row_cnt = $resultA->num_rows;

echo($row_cnt !== 0) ? mysqli_result($result, '0', 'theurl') : 'Not found.';
}

function mysqli_result($result, $ro, $field) {
$result->data_seek($ro);
$datarow = $result->fetch_array();
return $datarow[$field];
}

最佳答案

尝试更改以下行的顺序:

$getVal = mysqli_real_escape_string($Conn_db, trim($_POST['thename']));

require_once('../db/connection.php');

应该如下完成,因为 mysqli_real_escpae_string() 需要在调用之前创建数据库连接:

require_once('../db/connection.php');
$getVal = mysqli_real_escape_string($Conn_db, trim($_POST['thename']));

关于php - 重写代码 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20682047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com