Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I keep getting this error i don't understand what's going on mysqli_select_db() (new user)

I’m trying to get all the posts on the database to be listed in the home page

Fatal error: Uncaught TypeError: mysqli_select_db(): Argument #1 ($mysql) must be of type mysqli, string given in C:\xampp\htdocs\myblog\user_homepage.php:139 Stack trace: #0 C:\xampp\htdocs\myblog\user_homepage.php(139): mysqli_select_db(‘myblog’, Object(mysqli)) #1 {main} thrown in C:\xampp\htdocs\myblog\user_homepage.php on line 139

<?php
include 'auth.guard.php';
include 'db_connection.php'

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>regestration</title>
<style>
    /* padding on document's body to prevent the content
    to go underneath the header and footer */
    body{        
        padding-top: 60px;
        padding-bottom: 40px;
        box-sizing: border-box
    }
    /* Full-width input fields */
input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* Set a style for all buttons */
button {
  background-color: #04AA6D;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

button:hover {
  opacity:1;
}

/* Extra styles for the cancel button */
.cancelbtn {
  padding: 14px 20px;
  background-color: #f44336;
}

/* Float signup buttons and add an equal width */
.cancelbtn, .signupbtn {
  float: left;
  width: 50%;
}

    .fixed-header, .fixed-footer{
        width: 100%;
        position: fixed;        
        background: #333;
        padding: 10px 0;
        color: #fff;
    }
    .fixed-header{
        top: 0;
    }
    .fixed-footer{
        bottom: 0;
    }
    .container{
        width: 80%;
        margin: 0 auto; /* Center the DIV horizontally */
        padding: 16px;
    }
    nav a{
        color: #fff;
        text-decoration: none;
        padding: 7px 25px;
        display: inline-block;
    }

table {
  border-collapse: collapse;
  width: 100%;
}

th, td {
  text-align: left;
  padding: 8px;
  border: 2px solid #f2f2f2;
}

tr:nth-child(even){background-color: #f2f2f2}

th {
  background-color: #04AA6D;
  color: white;
}
/* Clear floats */
.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

/* styles for cancel button and signup button on extra small screens */
@media screen and (max-width: 300px) {
  .cancelbtn, .signupbtn {
     width: 100%;
  }
}
</style>
</head>
<body>
    <div class="fixed-header">
        <div class="container">
            <nav>
                <a href="user_homepage.php">Home</a>
                <a href="update_user_details_functionality.php">user profile</a>
                <a href= "logout.php">log out</a>
                
            </nav>
        </div>
    </div>
    
    <?php

$db = mysqli_select_db("myblog", $conn);

//finds the data stored in the database
$db = mysqli_select_db("myblog", $conn);
$query = mysqli_query("select * from posts", $conn);
$result = $conn->query($sql);
//making a table for the output
echo "<table border='1' <tr><th>ID</th> <th>post_date</th> <th>Price</th></tr>";
if ($result->posts> 0) {
    // output data of each row using a loop
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["post_id"]. "</td>" . "<td>" .$row["post_text"]. "</td>" . "<td>" . $row["post_date"] . "</td>" .  "<td>" . $row["category"] . "</td>" . "<td>" .  $row["price"] . "</td>" . "<td><img src='assets/" .   $row["post_id"]  . ".jpg' width='75'></td>";
        
echo "<input type='text' name='pcode' value='"  . $row["post_id"]. "'>";
echo "<input type='text' name='pname' value='"  . $row["post_text"]. "'>";
        
;
        
        for ($optionIndex=1; $optionIndex <= 15; $optionIndex++){
            echo '<option>' . $optionIndex . '</option>';
        }

    }
echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

</div>


    <div class="fixed-footer">
        <div class="container">Copyright &copy; 2021 Your Company</div>        
    </div>
</body>
</html>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have the arguments to mysqli_select_db flipped around (in both cases).

mysqli_select_db("myblog", $conn);

should be

mysqli_select_db($conn, "myblog");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading