-3

I am complete newbie to PHP/SQL and all this stuff, also not really skilled with any kind of programming. My problem is that I am currently trying to pull out data from MySQL table on to website, but after finishing the code it pulls out all of the data from my table. I would love to somehow get to pull out only data from specific table row, based on it's primary key. My current code looks like this.

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rainbow";

$link = mysqli_connect($servername, $username, $password, $dbname);

if($link === false){
  die("ERROR: COuld not connect." . mysqli_connect_error());
}

$sql = "
SELECT name
     , nick
     , surname
     , team
     , country
     , birthdate
     , mouse
     , dpi
     , keyboard
     , headset 
  FROM players
";
if($result = mysqli_query($link, $sql)){
  if(mysqli_num_rows($result) > 0){
    echo "<table>";
      echo "<tr>";
        echo "<th>name</th>";
        echo "<th>nick</th>";
        echo "<th>surname</th>";
        echo "<th>team</th>";
        echo "<th>country</th>";
        echo "<th>birthdate</th>";
        echo "<th>mouse</th>";
        echo "<th>dpi</th>";
        echo "<th>keyboard</th>";
        echo "<th>headset</th>";
      echo "</tr>";
    while ($row = mysqli_fetch_array($result)){
        echo "<tr>";
          echo "<td>" . $row['name'] . "</td>";
          echo "<td>" . $row['nick'] . "</td>";
          echo "<td>" . $row['surname'] . "</td>";
          echo "<td>" . $row['team'] . "</td>";
          echo "<td>" . $row['country'] . "</td>";
          echo "<td>" . $row['birthdate'] . "</td>";
          echo "<td>" . $row['mouse'] . "</td>";
          echo "<td>" . $row['dpi'] . "</td>";
          echo "<td>" . $row['keyboard'] . "</td>";
          echo "<td>" . $row['headset'] . "</td>";
        echo "</tr>";
    }
    echo "</table";
    mysqli_free_result($result);
    }
    else {
      echo "Ziadny vysledok a nic nefunguje";
  }
}

mysqli_close($link);
?>

To be honest, I am not even sure if this is the right way to do it, but it works and it pulls the data into a HTML table which is not necessary for me, I just wanted to try it. Thanks for answers!

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Jakub Varga
  • 15
  • 1
  • 5
  • If you know the id, you can add a condition to your SQL query: `WHERE id = $id` -- but to keep it safe from possible SQL injections you should switch from `mysqli_query()` to using a [prepared statement](https://stackoverflow.com/a/60496/1941241) – rickdenhaan Mar 16 '19 at 22:19
  • 1
    You should work through some PHP tutorials:) they will cover everything you need because this question shows that you're missing some fundamental basics. But as a hint: google "sql where statement". – LLJ97 Mar 16 '19 at 22:20
  • 1
    1. The **first** thing to correct a problem, look for your **problem**, why the **resulting** program is not appropriate or does not appear? 2. Second, is there an error *message* on the program output? 3. Third, what query commands will be ***generated*** on the ***web server application?*** like Heidi SQL, MySQL, etc. Please try the query of the: **SELECT * FROM players** in my opinion this shortens writing! Good Luck :) – Ogi Setiawan Mar 16 '19 at 22:26

2 Answers2

0

Let's suppose you have a page with this code:

<form action='page2.php' method='post'>
Inform a number:
<input type='text' name='number'>
<input type='submit' value='Send'>
</form>

Save the form above as page1.php

The file page2.php will contain the code that select one register from your table and show the result.

<?php 
$id = $_POST["number"];

//The sql command will look like this:
$sql = "SELECT name, nick, surname, team, country, birthdate, mouse, dpi, keyboard, headset FROM players WHERE id = $id";
?>
0

A couple of things. Firstly, You would typically keep some of your confidential stuff (database name, userid, password and server name in a separate file (eg. config.php) and you would "include" that file in this file... include ("config.php"); Secondly, in your $sql line you are selecting all of your columns individually and later in the 10 lines after your "while" statement you are selecting them again (for display). I would be calling the whole table in your $sql line with SELECT * from players and I would follow this with a WHERE. As a newbie, your basic retrive from batabase has 3 main words, SELECT (means go and get what you want and in most cases, grab it all with a *) FROM (the table you want to get it from, in your case the table is players) and WHERE (this is your selection criteria... id > 50...colour = "blue"... whatever you want). When pushing data to the DB you would use SET and UPDATE but when retrieving... SELECT, FROM, WHERE. Your "while" statement will simply do your $sql statement (select, from, where) and return results until it runs out of records
Good luck Newbie gri2a

gri2a
  • 59
  • 2