“试图获得非对象属性”是什么意思?

时间:2018-02-16 18:03:36

标签: php mysql

我收到此错误消息

  

注意:尝试在第20行的C:\ wamp64 \ www \ MatchManagement \ MatchPopulate.php中获取非对象的属性

有人能说出代码有什么问题吗?

<?php 
/* Need to correct this code*/
require '../../configure.php';
$uOpponentName = $_POST['Opponents'];
$uVenue = $_POST['Venue'];
$Already = False;

$db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
$database = "matchmanagementdb";
$conn = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
// check to see if Match (Opponents + Venue)already in the database, if so, retrieve data or add match to database
$SQL = "SELECT * FROM teamselect WHERE Opponents = $uOpponentName AND Venue = $uVenue";
$result = $conn->query($SQL);
//if $result->num_rows >0 then retrieve data  ELSE add match to database
    if ($result->num_rows >0) {
        while($row = $result->fetch_assoc()) {
            $OpponentName = $row['Opponents'];
            $selected = 'selected="selected"';    
        } 
    } else { 
        $sql = "INSERT INTO teamselect (Opponents, Venue) VALUES ('$uOpponentName', '$uVenue')";
     }
$conn->close();
?>

2 个答案:

答案 0 :(得分:0)

您在查询中缺少单引号。因此$result为假,$result->num_rows失败。

$SQL = "SELECT * FROM teamselect WHERE Opponents = '$uOpponentName' AND Venue = '$uVenue'";

NB:您的代码容易受SQL injections攻击,并可能危及数据库的安全性。你应该看一下parameterized queries函数。

最后,您应该在使用之前测试函数的结果。例如:

$result = $conn->query($SQL);
if (!$result) {
    // error
} else {
    if ($result->num_rows >0) {
       // ...
    }
}

答案 1 :(得分:0)

当您尝试访问不是对象的对象的属性时,会发生此错误。在这种情况下,您无法访问该属性,因为$ result不是对象。它是一个布尔= false。有一件事是使用PHP的is_object检查变量是否为对象。这不会回答您查询的问题,而是回答有关错误本身的问题。

例如,使用is_object提前失败,

if ($result !== false && $result->num_rows >0) {}

你可以这样做,

import re

s = "i would like to exlucde :HOkd but not JI:jklj " 
words = re.findall(r'(?:^|\s)(\w+(?::\w+)?)', s)

print(words)