2014-07-24 34 views
-5

嗨,那裏的傢伙感謝您抽出寶貴時間來幫助我,我正在努力解決兩個問題,但現在可以做到。基本上我創建了一個連接到我的數據庫MYSQL的博客。問題是第一個錯誤的..如何解決這個奇怪的PHP問題?

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

我所試圖實現的是我可以有很多博客和當用戶點擊繼續閱讀這篇博客中,其他博客只會dissapear,因爲他們有不同的頁面身份證,但它不起作用。我所做的修復錯誤是添加($ sql,連接)。但我可能錯了?我在該頁上的代碼..

<?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'); ?> 

感謝一個很好的:)!

+1

的mysql_query($連接)...爲什麼? –

+0

這個錯誤不是「weired」的......你將連接資源傳遞給mysql_query函數,而不是你的$ sql。對於SO中的其他問題,請改進您的提問風格,例如,當您詢問有關php/DB問題的內容時,請勿發佈所有html內容! – strauberry

+1

[爲什麼不應該使用mysql_ *函數在PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil

回答

1

你需要傳遞$sql,不$connectionmysql_query

$sql = "SELECT * FROM blog"; //SQL query 
$result = mysql_query($sql) or die(mysql_error($connection)); //run the query 
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 
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