Reputation: 2964
I have problem in printing the database records on the screen. It has no problem on inserting the values. It also tells us the number of rows.
Can you please tell me that what is the problem in fetching and printing records
<!DOCTYPE html>
<html>
<body>
<form action="zain.php" method="post">
Topic: <input type="text" name="topic"><br />
<br />
Name: <input type="text" name="name"><br /><br />
Attendance: <input type="text" name="attendance"><br />
<br />
<input type="reset" name="reset">
<input type="submit" name="submit" value="Go">
</form>
<?php
$user = 'root';
$password = 'zz224466';
$db = 'Zain';
// Create connection
$conn = mysqli_connect('localhost', $user, $password, $db);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "";
mysqli_select_db($conn, "zain");
//$sql = "CREATE TABLE Lectures(Topic varchar(20), Name varchar(20), Attendence int)";
$sqli = "INSERT INTO lectures(Topic , Name , Attendence) VALUES('$_POST[topic]','$_POST[name]','$_POST[attendance]')";
mysqli_query($conn, $sqli);
////////////////////////// For print DATABASE on the screen ////////////////////////////////////////
if(isset($_POST['submit'])) {
mysqli_select_db($conn, "zain");
$dataBase = "SELECT * FROM lectures";
$print = mysqli_query($conn, $dataBase);
while ($record = mysqli_fetch_array($print)) ;
{
echo $record['Topic'];
echo "</br>";
}
}
mysqli_close($conn);
/////// It tells the number of records in database///////
$countRow = mysqli_num_rows($print);
echo "<br>" . $countRow;
?>
</body>
</html>
Upvotes: 0
Views: 344
Reputation: 74217
Do you see the semi-colon at the end of your while
loop?
while ($record = mysqli_fetch_array($print)) ;
^ right there
It's actually considered as valid syntax and won't throw an error. So checking for errors for it, won't help.
References:
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.
Furthermore,
Your present code is open to SQL injection. Use mysqli_*
with prepared statements, or PDO with prepared statements.
Upvotes: 2