使用PHP

时间:2017-09-03 13:44:58

标签: php mysql mysqli

每次我尝试删除表中删除的条目时,我的PHP中都存在mySQL的问题但是会输出错误。错误如下:

  

警告:mysqli_query()[function.mysqli-query]:无法获取mysqli

     

警告:mysqli_num_rows()期望参数1为mysqli_result,   null给出

     

警告:mysqli_close()[function.mysqli-close]:无法获取mysqli

我似乎无法弄清楚为什么会发生这种情况,它可能很简单,但我对mySQL很新,所以希望有更高级的人可以告诉我哪里出错了。

<?
if(isset($_GET['deleteTweet'])) {
    $deleteTweet = $_GET['deleteTweet'];

    $sql = "DELETE FROM bestTweets WHERE id=".$deleteTweet;

    if (mysqli_query($conn, $sql)) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . mysqli_error($conn);
    }
    mysqli_close($conn);
}

$sql = "SELECT id, tName, tUsername, tTimeDate, tBody FROM bestTweets";
$result = mysqli_query($conn, $sql);

$onMouseOver = 'onmouseover="this.src='."'IMAGES/binBtn_Hover.svg'".'" ';
$onMouseOut = 'onmouseout="this.src='."'IMAGES/binBtn.svg'".'"';

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo 
        '<div id="tweetContainer" class="tweetContainer">
            <div id="tweetMain">
                <div id="tweetName">
                    <p>'.$row['tName'].'</p>
                </div>'.

                '<div id="tweetUsername">
                    <p>'.$row['tUsername'].'</p>
                </div>'.

                '<div id="tweetDateTime">
                    <p>' .$row['tTimeDate'].'</p>
                </div>'.

                '<div id="tweetBody">
                    <p>'.$row["tBody"].'</p>
                </div>
                <div id="tweetSave">
                    <form action="Saved.php?isset=true&deleteTweet='.$row["id"].'" method="post">
                        <button type="submit"  name="binBtn1" class="saveBtn"><img src="IMAGES/binBtn.svg"'.$onMouseOver.$onMouseOut.' width="50px" height="50px"></button>
                    </form>
                </div>
            </div>
        </div>';}
    }
    else {
        echo "0 results";
    }
mysqli_close($conn);
?>

2 个答案:

答案 0 :(得分:2)

如果你删除一行,你做的最后一件事就是关闭连接!

更改为...

<?
if(isset($_GET['deleteTweet'])) {
    $deleteTweet = $_GET['deleteTweet'];

    $sql = "DELETE FROM bestTweets WHERE id=".$deleteTweet;

    if (mysqli_query($conn, $sql)) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . mysqli_error($conn);
    }
    //mysqli_close($conn);
}

下一个查询是尝试使用已关闭的连接。

答案 1 :(得分:0)

两个建议:

  1. 检查您是否已成功建立连接(因为我在您的代码中找不到$con)。
  2. 确保您没有过早关闭连接。