Takes multiple data from MySQL with PHP

Hi,

I’m not as good in the programming. I need to make a graduaters page where there are maybe 1920 and 1921 and 1922 etc people. The dates when they graduated the school.

When I make the databes 1920 and insert maybe one name and the id is situated with 1920, then I make a new entry 1921 and that id is 21, different than 1920 ID.

If I press on the link “1920” it shows me the data in 1920 table AND data on 1921. I do not want that multiple names from 1921 come to 1920…

People will go to index.php and there will be 1920. They press on it and it will go to nimekiri.php and that would show 1920 graduates. Right now it shows 1920 graduates AND 1921…What’s wrong in my code?

Here are my codes:

  1. index.php
<?php //include ('nimekiri.php'); $servername = "***********"; $username = "***********"; $password = "***********"; $dbname = "***********";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = “SELECT aasta, lend_id FROM vilist”;
$result = $conn->query($sql);
echo “

Vilistlased

”;
echo “

Aasta (lend)

”;
echo “”;

for ($x = 0; $x <= 8; $x++) {

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
        
         $aasta=$row["aasta"];
         $lend=$row["lend_id"];
     //echo "<td>". $row["aasta"]." (". $row["lend_id"].")". "</td>"; 
     echo "<td><a href=nimekiri.php>$aasta ($lend)</a>"."</td>";
   } 

} else {
echo “0 results”;
}
}
echo “

”;

$conn->close();
?>

</body>
  1. nimekiri.php
<?php //include ('index.php'); //echo $aasta; //echo $lend; $servername = "***********"; $username = "***********"; $password = "***********"; $dbname = "***********"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT aasta, lend_id, enimi, pnimi FROM vilist"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { //if($aasta === $row["aasta"]){ //echo "

". $row["aasta"]." ". $row["lend_id"]."". "

"; echo " ". $row["enimi"]." ". $row["pnimi"]."". "
"; } } else { echo "0 results"; } echo ""; $conn->close(); ?>

You are missing a WHERE condition in your query.

WHERE id = 1920

I thought that maybe WHERE is missing but I can’t find where to put it…:frowning: I think I should put it in nimekiri.php but where exactly, can you show me?

The suggestion will only help with id 1920, but I have 1920-2016. What should I use then?

The same exact thing. Just change the id number you are passing to the query.

Hi,

If I do it like this

$sql = “SELECT aasta, lend_id, enimi, pnimi FROM vilist WHERE lend_id = 1”;
$result = $conn->query($sql);

then it still shows the same answer on year 1920 and year 1980 and year 2015. Why is that so?

Post a SQL dump of your DB or PM it to me.

SELECT aasta, lend_id, enimi, pnimi FROM vilist WHERE ID = 1

Gives

2015	2015	Kalle	Sepp

[php]Id 1
Id 2

<?php $hostdb = 'localhost'; $dbname = 'w1060m05'; $username = 'root'; $password = ''; if (isset($_GET['id'])){ try { $pdo = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM vilist WHERE ID = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$_GET['id']]); $result = $stmt->fetchAll(); } catch (PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } if ($result) { foreach ($result as $row) { echo '
';
            print_r($row);
            echo '
'; } } else { echo "No rows returned."; } } ?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service