Jump to content

retrieving data from database


Alfraganus

Recommended Posts

Dear programmers,

 

I am trying to retrieve all ma data from database, but my php is bringing me just my last data only, how can I bring them all?

 

and one more problem, when I am trying to uplaod data to database, sometimes it is uplaoding and sometimes it is just igroning, what can it be result of?

 

here is my tiny source code::

 

upload.php

 

 

 

<!DOCTYPE html>
<html>
<head>
<title>uploading a file</title>
</head>
<body bgcolor="blue">
<form accept="upload.php" method="post" enctype="multipart/form-data">
<table align="center" width="700" border="2" bgcolor="green">
<tr align="center">
<td align="center">
<td colspan="8"><h2>upload whatever you want mr Alfraganus</h2></td>
</td>
<tr>
<td colspan="8" align="center"><input type="text" name="title" size="60"><b>Title</b></td>
</tr>
<tr>
<td colspan="8"><textarea name="article" cols="80" rows="20"></textarea></td>
</tr>
<tr>
<td colspan="8"><center><input type="submit" name="upload" value="upload the text"> </center></td>
</tr>
</table>
</body>
</html>
<?php
/*$servername="localhost";
$username="root";
$password="";
$dbname="testdb";*/
$conn=new mysqli ("localhost", "root","","testdb"); //checking connection
if ($conn) {
echo "success";
}
if (isset($_POST['upload'])) {
$title=$_POST['title'];
$body=$_POST['article'];
$insert="INSERT INTO cms
(title, body) VALUES ('$title', '$body')";
$checking=mysqli_query($conn, $insert);
}
if($checking) {
echo "<script>alert('success')</script>";
}
?>
display.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="style.css" media="all" />
</head>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<div id="header">ajshcoljwq qwpdjpqwkdqokdwpqodk'pqw </div>
<div id="sidebar"> </div>
<center>
<div id="content">
<?php
$con=new mysqli("localhost","root","", "testdb");
$sql="SELECT * FROM cms";
$run=mysqli_query($con, $sql);
while($row=mysqli_fetch_array($run)) {
$title=$row['title'];
$body=$row['body'];
}
echo "
<h1> $title</h1
<p>$body</p>
";
?>
<div id="footer">
</center>
</body>
</html>

 

Link to comment
Share on other sites

you need to put the " Echo" part inside the while loop .

 

 

<?php

$con=new mysqli("localhost","root","", "testdb");
$sql="SELECT * FROM cms";
$run=mysqli_query($con, $sql);
while($row=mysqli_fetch_array($run)) {
$title=$row['title'];
$body=$row['body'];
}
echo "
<h1> $title</h1
<p>$body</p>
";
?>

replace it with this :

<?php 
$con=new mysqli("localhost","root","", "testdb");
$sql="SELECT * FROM cms";
 
$run=mysqli_query($con, $sql);
while($row=mysqli_fetch_array($run)) {
$title=$row['title'];
$body=$row['body'];
echo "
<h1> $title</h1
<p>$body</p>
 
";

}


?>
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...