PHP抛出奇怪的错误

时间:2014-02-13 08:30:57

标签: php html5

执行此代码时,我不断收到以下错误消息:
 注意:尝试在第1178行的C:\ wamp \ www \ HoneysProject \ function.php中获取非对象的属性

第1178行是指以下代码行:
$ number_photos = $ photo_exists-> num_rows;

不确定它为什么会抛出这个错误,因为它是我在这个项目中经常使用的一行代码的副本。因为我继续成功输出,所以对运行完成的代码似乎没有任何实际影响。就像错误信息停止弹出一样。

以下是完整的源代码:

function create_album()
{
    try
    {
        if( isset( $_SESSION['session_id'] ) && $_SESSION['permissions'] ==  0  )
        {
            if( isset( $_POST['album_name'] ) && ( file_exists( $_FILES['cover_photo']['tmp_name'] ) || is_uploaded_file($_FILES['cover_photo']['tmp_name'] ) ) )
            {
                $db = honneyconnect( ) ;
                if( mysqli_connect_error() )
                {
                    throw new Exception( "Could not connect to the database") ;  
                }
                else
                {
                    $unique = false ;
                    while( $unique == false )
                    {
                        $key = rand( )  ;
                        $query = "select * from albums where album_id = '".$key."'";  
                        $album_exists = $db->query( $query ) ;
                        $number_albums = $album_exists->num_rows ;
                        if( $number_albums == 0 )
                        {
                            $unique = true ;
                        }
                    }
                if( !mkdir( "c:\\wamp\\www\\honeysproject\\".$_POST['album_name']  ) )
                {
                    throw new Exception( "Failed to create the album.  Please try again." ) ;
                }
                else
                {
                    $file_name = $_FILES["cover_photo"]["name"] ;
                    if( !move_uploaded_file($_FILES["cover_photo"]["tmp_name"], "C:/wamp/www/HoneysProject/".$_POST['album_name']."/" . $_FILES["cover_photo"]["name"]) )
                    {
                        throw new Exception( "There was a problem uploading the file" ) ;
                    }
                    else
                    {
                        $query = 'insert into albums values ("'.$key.'","'.$_POST['album_name'].'", "'.$_FILES['cover_photo']['name'].'")';
                        $album = $db->query( $query ) ;
                        if( !$album )
                        {
                            throw new Exception( "Failed to create the ".$_POST['album_name']." album.  Please check your input and try again." ) ;
                        }
                        else
                        {
                            $unique = false ;
                            while( $unique == false )
                            {
                                $picture_key = rand();
                                $query = "select * from photos where photo_id = '".$picture_key."'" ;
                                $photo_exists = $db->query( $query ) ;
                                $number_photos = $photo_exists->num_rows ;
                                if( $number_photos == 0 ) 
                                {
                                    $unique = true ;
                                }


                            }
                            $query = "insert into photos values ('".$key."', '".$picture_key."', '".$file_name."')" ;
                            $picture_query = $db->query( $query ) ;
                            if( !$picture_query ) 
                            {

                                throw new Exception( "Failed to add photo to the photos table.  Please check your syntax and try again." ) ;

                            }   
                            else
                            {
                                echo "<table><tr><td><img src='/HoneysProject/".$_POST['album_name']."/".$_FILES['cover_photo']['name']."'></td><td><a class ='button' href='/HoneysProject/uploadphotos.php?album_id=".$key."'>Upload Photos</a><br><a class='button' href='/HoneysProject/albumedit.php?album_id=".$key."'>Edit Album</a></td></tr></table>" ;
                            }
                        }
                    }

                }


            }
        }
        else
        {
            echo '<div class="data_entry">
                        <form id="new_album" method="post" action="createalbum.php" enctype="multipart/form-data" />
                        <input type="hidden" name="MAX_FILE_SIZE" value="50000000" />
                        <table>
                        <tr><td>Album Name:</td><td><input type="text" size="10" name="album_name" /></td></tr>
                        <tr><td>Choose a Default Photo:</td><td><input type="file" name="cover_photo" id="photo" /></td></tr>
                        <tr><td><input type="submit" value="Submit Data" /></td></tr>
                        </table>

                        </form></div>' ;
        }
    }
}
catch( Exception $error ) 
{
    echo "<div class='error'>".$error."</div>" ;
    echo '<div class="data_entry">
                        <form id="new_album" method="post" action="createalbum.php" enctype="multipart/form-data" />
                        <input type="hidden" name="MAX_FILE_SIZE" value="50000000" />
                        <table>
                        <tr><td>Album Name:</td><td><input type="text" size="10" name="album_name" value="'.$_POST['album_name'].'"/></td></tr>
                        <tr><td>Choose a Default Photo:</td><td><input type="file" name="cover_photo" id="photo" /></td></tr>
                        <tr><td><input type="submit" value="Submit Data" /></td></tr>
                        </table>

                        </form></div>' ;

}
}

3 个答案:

答案 0 :(得分:1)

错误是由于查询失败造成的。

我想知道你为什么要手动生成实体密钥?您的DBMS不支持自动递增的密钥或序列吗?

答案 1 :(得分:0)

显然查询失败,而$photo_exists不是nullfalse的对象。根据文档mysqli::query

  • 失败时返回FALSE。对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回一个mysqli_result对象。对于其他成功的查询,mysqli_query()将返回TRUE。

在尝试使用mysqli函数返回的对象之前,应检查错误。

$photo_exists = $db->query($query) ;
if($number_photos) {
    $number_photos = $photo_exists->num_rows ;
}

甚至:

if($photo_exists = $db->query($query)) {
    $number_photos = $photo_exists->num_rows ;
}

答案 2 :(得分:0)

$ photo_exists不是对象,很可能是null。仔细检查初始化的位置,以确保大写和拼写正确。

此外,您应该在尝试使用之前添加一些调试打印语句以检查$ photo_exists的状态,您很可能会惊讶于它不包含您想要的内容。

您可以使用var_dump($ photo_exists)来尝试帮助您输出对象。