更新到MySQLi并遇到一些麻烦

时间:2013-09-26 16:44:10

标签: php mysql mysqli

所以我花了一些时间看MySQLi,但我在使用新功能更新脚本时遇到了麻烦。此脚本用于动态下拉表单,使用JS发送给它的数据。您可以找到脚本 here 的实时版本,以查看我正在拍摄的内容。我查看了我的代码并将其与其他MySQLi示例进行了比较,我只是不确定我哪里出错了。

现在,第一个下拉列表甚至没有启动查询,所有PHP都会返回预定义的结果,因为它对第一个选项来说更简单。对我而言,奇怪的是,即使第一次下拉,当它完全不依赖于MySQLi连接时,它现在也无法正常工作。这一切在更新之前都有效,仅供参考。

这是我的剧本:

$db = new mysqli($dbHost, $dbUser, $dbPass, $dbDatabase);

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
isset($_GET['source'])?$source = $db->real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = $db->real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = $db->real_escape_string(urldecode($_GET['setpoint'])):"";

//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts 
if (isset($_GET['setpoint'])) {
    $query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
    $query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
    $query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
    $query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}

//creates a result array from query results
isset($query)?$result = $db->query($query):"";

//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
    while ($row = $result->fetch_assoc()) {
        echo "<option value='" . $row['stp'] . "'>" . $row['stp'] . "</option>";
    $result->free();
    }
} elseif (isset($_GET['power'])) {
    echo "<option>Please Choose Setpoint Options</option>";
    while ($row = $result->fetch_assoc()) {
        $row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
        echo "<option value='" . $row['stp'] . "'>" . $row['stp'] . "</option>";
    $result->free();
    }
} elseif (isset($_GET['source'])) {
    echo "<option>Please Choose Input Range</option>";
    while ($row = $result->fetch_assoc()) {
        echo "<option value='" . $row['sir'] . "'>" . $row['sir'] . "</option>";
    $result->free();
    }
} elseif (isset($_GET['type']) && $_GET['type'] == "Digital") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
    $result->free();
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
    $result->free();
}

编辑:这是我使用弃用方法的旧代码。

$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());


//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
//$type = mysql_real_escape_string(urldecode($_GET['type']));
isset($_GET['source'])?$source = mysql_real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = mysql_real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = mysql_real_escape_string(urldecode($_GET['setpoint'])):"";

//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts 
if (isset($_GET['setpoint'])) {
    $query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
    $query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
    $query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
    $query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}

//creates a result array from query results
isset($query)?$result = mysql_query($query):"";

//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
    }
} elseif (isset($_GET['power'])) {
    echo "<option>Please Choose Setpoint Options</option>";
    while ($row = mysql_fetch_array($result)) {
        $row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
        echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
    }
} elseif (isset($_GET['source'])) {
    echo "<option>Please Choose Input Range</option>";
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
    }
} elseif (isset($_GET['type']) && $_GET['type'] == "Digital") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}

1 个答案:

答案 0 :(得分:1)

您正在释放while循环中的$result。这将导致循环在第二次迭代时失败。

由于你在所有的ifs中释放了结果,为什么不在结束时只执行一次?

...
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}

$result->free();

然而,这并不能解释为什么模拟和数字仍无法正常工作..