-5

Hi there guys thanks for taking the time to give me a hand, I am struggling with two issues really but one can do for now. Basically I'm creating a blog connecting to my database MYSQL. The issue is the first error this..

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

What I am trying to achieve is I can have as many blogs and when a user click continue reading this blog, the other blogs will just dissapear as they have different page id's but it's not working. What I've done ti fix the error is add ($sql, connection). But I could be wrong? My code on that page..

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

THANKS A LOT :)!

Emma
  • 1
  • 1
    mysql_query($connection) ... why ? – Royal Bg Jul 24 '14 at 06:27
  • this error is not "weired"... you pass the connection resource to the mysql_query function and not your $sql. For further questions in SO, plese improve your asking style, e.g., do not post all the html stuff when you ask something about php/DB problems! – strauberry Jul 24 '14 at 06:27
  • 1
    [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Jul 24 '14 at 06:28
  • You know what this post needs? Even more identical answers – Phil Jul 24 '14 at 06:29

3 Answers3

1

You need to pass $sql, not $connection to mysql_query:

$sql = "SELECT * FROM blog"; //SQL query 
$result = mysql_query($sql) or die(mysql_error($connection)); //run the query 
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Try to change

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

with

    $sql = "SELECT * FROM blog"; //SQL query 
    $result = mysql_query($sql) or die(mysql_error($connection)); //run the query 
sensorario
  • 20,262
  • 30
  • 97
  • 159
0

You need the sql string and the connection:

  mysql_query($sql, $connection);

Thus you should have:

  $sql = "SELECT * FROM blog"; //SQL query 
  $result = mysql_query($sql,$connection) or die(mysql_error($connection)); //run the query 
Edper
  • 9,144
  • 1
  • 27
  • 46