skip to Main Content

I am trying to get a variable from a mySQL table into my PHP code. I am using a mysqli_query command. When I try to echo the result, it echos nothing, not even other echo statements.

I have ensured that the $conn is correct and working, and that the Sql query is correct by running it directly in PHPmyAdmin.

<?php
session_start(); 

include_once("includes/dbh.inc.php");

    $UID = $_POST['userIdVariable'];

    $UID = $_SESSION['u_uid'];

    $balance = "SELECT account_balance FROM `users` WHERE user_uid = "$UID"";//this line probably works

$result = mysqli_query($conn, $balance);

echo "$result";

When I run this code it would output nothing. The right side of the $balance line is a good sql query according to PHPmyadmin. The $UID variable is read in correctly.

3

Answers


  1. You need to use mysqli_fetch function to get result columns out of $result object.

    while ($data = mysqli_fetch_assoc($result)) {
        printf("%s n", $data["account_balance"]);
    }
    
    Login or Signup to reply.
  2. Don’t use quotation in a field name or table name inside the query.

    $balance = "SELECT account_balance FROM users WHERE user_uid = '$UID'";
    
    Login or Signup to reply.
  3. please check your database connectivity.there could be something missing like database name.

    and print your $UID. may be there is no id getting.

    to print data use print_r();

    and try this:

    session_start(); 
    $servername = "localhost";
    $username = "username";
    $password = "password";
    // Create connection
    $conn = new mysqli($servername, $username, $password,"databasename");
         $UID=1;
         $balance = "SELECT account_balance FROM users WHERE user_uid =$UID"; 
        $result = mysqli_query($conn, $balance); 
        if (mysqli_num_rows($result)) { 
            while ($row = mysqli_fetch_assoc($result)) { 
                echo $row['account_balance'];
            } 
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search