我该如何解决这个奇怪的PHP问题?

时间:2014-07-24 06:25:43

标签: php mysql

大家好,感谢你抽出时间帮助我,我正在努力解决两个问题,但现在可以做到。基本上我创建了一个连接到我的数据库MYSQL的博客。问题是第一个错误..

Warning: mysql_query() expects parameter 1 to be string, resource given in D:\xampp\htdocs\wd1_osmanovic_0100348514\pages\blog.php on line 19

我想要实现的是我可以拥有尽可能多的博客,当用户点击继续阅读此博客时,其他博客将会消失,因为他们有不同的页面ID,但它不起作用。我已经完成的修复错误的是add($ sql,connection)。但我可能错了?我在该页面上的代码..

<?php 
//--- Authenticate code begins here ---
session_start();
//checks if the login session is true
if(!isset($_SESSION['username'])){
header("location:index.php");
}
$username = $_SESSION['username'];

// --- Authenticate code ends here ---


 include ('header.php');

 function displayAllBlog(){
        global $connection; 

        $sql = "SELECT * FROM blog"; //SQL query 
        $result = mysql_query($connection) or die(mysql_error($connection)); //run the query 
            while($row = mysql_fetch_array($result)){ //use a while loop to display all the rows in the database 
                echo "<h1>" . $row['blogTitle'] . "</h1>";
                echo "<h3>" . $row['dateTime'] . "&nbsp; &nbsp; &nbsp;" . $row['authorID'] . "&nbsp; &nbsp; &nbsp;" . $row['catID'] . "</h3>";          
                echo "<p>" . $row['blogContent'] . "</p>"; 
                echo "<p>" . (substr(($row['authorID']),0,100)) . "</p>";
                echo "<a href='blog.php?id=" .$row['blogID']. "'>Go to blog.</a>";  
            }
    }

 ?> 
<link rel="stylesheet" type="text/css" href="../css/style1.css">




       <div style="float:right">  <a class="btn btn-danger logout" href="logout.php" > Logout</a> </div>

        <div id="menu">
    <ul id="nav">
        <li><a href="home.php" target="_self" >Home</a></li>
        <li><a href="session1.php" target="_self" >Sessions</a>
            <ul>
                <li><a href="session1.php" target="_self" >Session 1</a></li>
                <li><a href="session2.php" target="_self" >Session 2</a></li>
                <li><a href="session3.php" target="_self" >Session 3</a></li>
                <li><a href="session4.php" target="_self" >Session 4</a></li>
                <li><a href="session5.php" target="_self" >Session 5</a></li>
                <li><a href="session6.php" target="_self" >Session 6</a></li>
                <li><a href="session7.php" target="_self" >Session 7</a></li>
                <li><a href="session8.php" target="_self" >Session 8</a></li>
                <li><a href="session9.php" target="_self" >Session 9</a></li>
                <li><a href="session10.php" target="_self" >Session 10</a></li>
                <li><a href="session11.php" target="_self" >Session 11</a></li>
                <li><a href="session12.php" target="_self" >Session 12</a></li>
                <li><a href="session13.php" target="_self" >Session 13</a></li>
                <li><a href="session14.php" target="_self" >Session 14</a></li>


            </ul>
            <li><a href="blog.php" target="_self" >Blog</a></li>


    </ul>
    </div>



                    <?php
                                $blogID = 0;
                                if(!empty($_GET['ID']))$blogID = mysql_real_escape_string($connection, $_GET['ID']); //Grabs blog ID from get 

                                if($blogID >= 1){
                                    //echo blog based on ID, else fall back on displaying all.
                                    $sql = "SELECT * FROM blog WHERE blogID='$blogID'"; //SQL query 
                                    $result = mysql_query($sql, $connection) or die(mysql_error($connection)); //run the query 
                                    $count = mysql_num_rows($result); //Number of rows

                                    if($count == 1){
                                        //Echo Single blog
                                        $row = mysql_fetch_array($result);
                                        echo "<h1>" . $row['blogTitle'] . "</h1>";
                                        echo "<h3>" . $row['dateTime'] . "&nbsp; &nbsp; &nbsp;" . $row['authorID'] . "&nbsp; &nbsp; &nbsp;" . $row['catID'] . "</h3>";          
                                        echo "<p>" . $row['blogContent'] . "</p>"; 
                                        echo "<p>" . (substr(($row['authorID']),0,100)) . "</p>";                 
                                    }else{
                                        //IF NO BLOG MATCHES WITH ID GIVEN, DISPLAY ALL
                                        displayAllBlog();  
                                    }
                                }else{
                                    //IF NO $GET DISPLAY ALL
                                    displayAllBlog();
                                }
                            ?> 




<?php include ('footer.php'); ?> 

感谢很多:)!

3 个答案:

答案 0 :(得分:1)

您需要将$sql而不是$connection传递给mysql_query

$sql = "SELECT * FROM blog"; //SQL query 
$result = mysql_query($sql) or die(mysql_error($connection)); //run the query 

答案 1 :(得分:0)

尝试更改

    $sql = "SELECT * FROM blog"; //SQL query 
    $result = mysql_query($connection) or die(mysql_error($connection)); //run the query 

    $sql = "SELECT * FROM blog"; //SQL query 
    $result = mysql_query($sql) or die(mysql_error($connection)); //run the query 

答案 2 :(得分:0)

您需要sql字符串和连接:

  mysql_query($sql, $connection);

因此你应该:

  $sql = "SELECT * FROM blog"; //SQL query 
  $result = mysql_query($sql,$connection) or die(mysql_error($connection)); //run the query