I'm currently learning how to use PHP and SQL in tandem. I'm trying to use PHP to display data from my database straight onto my website. I know it's probably rudamentary, but I have scoured the internet for the last two days and nothing has worked for me. I have even tried asking chatGPT but that thing has only driven me more insane.
<?php include "head.php";
$currentPage = 'contact';
require 'db_class.php';
$db = new sql_class(); ?>
<header>
<div id="banner">
<?php include "header.php";
$result = $db->execute("SELECT subject_name FROM subjects WHERE id = 1");
// Fetch the row from the result
$row = mysqli_fetch_assoc($result);
// Store the subject name in a variable
$subject_name = $row['subject_name'];
?>
</div>
</header>
<body>
<h1>Contact Us</h1>
<form method="post">
<input type="text" id="firstname">
<label for="firstname">First name</label><br>
<input type="text" id="lastname">
<label for="lastname">Last Name</label><br>
<input type="text" id="email" required>
<label for="email">Email</label><br>
<input type="text" id="phone" required>
<label for="phone">Phone</label><br>
<select id="subject">
<option value="comment">Comment</option>
<option value="doctrine">Doctrine</option>
<option value="So"><?php echo $result;?></option>
</select><br>
<textarea name="" id="message" cols="30" rows="10">Your Message</textarea><br>
<input type="submit">
</form>
</body>
<?php include 'footer.php'?>
</html>
So, in conclusion, my main problem is; I want to display the word "comment" from my SQL database table named "subjects." If you're wondering why I'm not having the entire dropdown menu be displayed from my database it's because I'm just learning right now and if I can get this to work, then setting up a while loop to display the rest of the menu should not be too hard.
Thank you for any criticism or advice you may have for me.
I have went onto youtube and chatGPT. No responses from them have worked. The comments you see are actually from chatGPT's code.
<?php echo $result;?>
to show? I think you want<?php echo $subject_name; ?>
$result
isn't a string, it's amysqli_result
object. You can only use it inmysqli
functions likemysqli_fetch_assoc()
. If you're getting an empty string when you use$subject_name
, that's the value that's in the database, it's not a problem with the code.