2014-07-24 2 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 

무엇을 달성하려고하는 것은 나는 많은 블로그가있을 수 있으며, 사용자가 클릭이 블로그를 읽고 계속하면, 다른 블로그들이 다른 페이지를 바로 사라지는 것 같다 이드이지만 작동하지 않습니다. 내가 ti 수정 한 오류는 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'); ?> 

고맙습니다. :)! 당신은 SQL 문자열과 연결이 필요

$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 
+1

는 mysql_query ($ 연결) ... 왜? –

+0

이 오류는 "weired"가 아닙니다 ... 연결 리소스를 $ sql이 아닌 mysql_query 함수로 전달합니다. SO에 대한 추가 질문이 있으면 PHP/DB 문제에 대해 물어볼 때 plese가 묻는 스타일을 향상시킵니다 (예 : 모든 HTML 항목을 게시하지 마십시오). – strauberry

+1

[PHP에서 mysql_ * 함수를 사용하면 안되는 이유는 무엇입니까?] (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,$connection) or die(mysql_error($connection)); //run the query 
0

로 변경

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