How to get value from a table column

Asked

Viewed 1,076 times

0

I wanted to take the value of logged in user points and put on my page. This is the table "users" inserir a descrição da imagem aqui

For example: "You have x points"

Below page login.php

  <?php 
  $login = $_POST['login'];
  $entrar = $_POST['entrar'];
  $senha = md5($_POST['senha']);
  $connect = mysql_connect('xxxx','xxxxx','xxxxxxx');
  $db = mysql_select_db('xxxxxxxxx');
    if (isset($entrar)) {

      $verifica = mysql_query("SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'") or die("erro ao selecionar");
        if (mysql_num_rows($verifica)<=0){
          echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
          die();
        }else{
          setcookie("login",$login);
          header("Location:index.php");
        }
    }
?>
  • Put the login code in the question so we can help you.

  • Which code? From the entire login page?

  • You want to show this score on the index.php page, correct?

  • That, exactly.

  • If you just want to get the value of points just reuse the query you did. $fetch = mysql_fetch_object($verifies); , and then display the value of points $fetch->points; , or store in a Session. I also state that using Mysqli or PDO as connection drive mysql_* no longer receives support.

  • How would you do that, Mauro? I’m pretty layabout in PHP....

  • If any answer solves your problem mark it as accepted. See how in https://i.stack.Imgur.com/jx7Ts.png and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

  • Enjoy a tour in https://answall.com/tour

Show 3 more comments

2 answers

0


I edited the response and updated it to MySQLi because the version you are using MySQL is discontinued

$connect = new mysqli ("localhost", "USUARIO", "SENHA", "Nome_DB");

$sql="SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'";
$buscar=mysqli_query($connect,$sql);
$dados=mysqli_fetch_array($buscar);

$result=mysqli_num_rows($buscar);

if ($result==1) {

    $pontos = $dados["pontos"];

    setcookie("login",$login);
    header("Location:index.php?pontos=".$pontos);

}else|{

    echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
    die();

}

On the index page recover via GET

$pontos = $_GET['pontos'];

then you put it where you want it

within PHP

 echo $pontos;

within HTML

<?php echo $pontos ?>

0

There are already threads explaining how to do this, not specifically your problem, but as you reported that is layman in language I will explain in response.

This is your code (apparently it’s working)

  <?php 
  $login = $_POST['login'];
  $entrar = $_POST['entrar'];
  $senha = md5($_POST['senha']);
  $connect = mysql_connect('xxxx','xxxxx','xxxxxxx');
  $db = mysql_select_db('xxxxxxxxx');
    if (isset($entrar)) {

      $verifica = mysql_query("SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'") or die("erro ao selecionar");
        if (mysql_num_rows($verifica)<=0){
          echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
          die();
        }else{
          setcookie("login",$login);
          header("Location:index.php");
        }
    }
?>

Along those lines $verifica = mysql_query("SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'") or die("erro ao selecionar"); you are selecting everything from the table usuarios where credentials are in agreement.

This query is bringing all the data from the table, as long as the entered data is ok.

Along those lines if (mysql_num_rows($verifica)<=0) you are checking if the user does not exist on else you are saving the login in a session, the logic is the same, just in the block of the else add $fetch = mysql_fetch_object($verifica); and access the value of points $fetch->pontos;

at the end your code would be

  <?php 
  $login = $_POST['login'];
  $entrar = $_POST['entrar'];
  $senha = md5($_POST['senha']);
  $connect = mysql_connect('xxxx','xxxxx','xxxxxxx');
  $db = mysql_select_db('xxxxxxxxx');
    if (isset($entrar)) {

      $verifica = mysql_query("SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'") or die("erro ao selecionar");
        if (mysql_num_rows($verifica)<=0){
          echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
          die();
        }else{
          setcookie("login",$login);

          $fetch = mysql_fetch_object($verifica);
          $_SESSION["pontos"] = $fetch->pontos;
          header("Location:index.php");
        }
    }
?>

To access the session value in index.php just sign in to the file and get the value.

index php.

session_start();

echo "you have " . $_SESSION["points"] . " points";

  • And how would you put in index.php type "You have x points"?

  • I updated the answer.

  • Didn’t pull the amount of stitches :/

  • What error does it display? open your PHP error log and see what it generates. Usually this should work, unless your initial code is having problems.

  • I put it like this: <div class="alert"> <p> Hey, <?php echo $login_cookie; ? > . <?php session_start(); echo "you have " . $_SESSION["points"] . "points"; ? > E appeared in index.php "Hey, test . you have points "

Browser other questions tagged

You are not signed in. Login or sign up in order to post.