pulling data from a sql database

Codes here !

Moderators: egami, macek, gesf

Post Reply
resilo
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Aug 05, 2017 9:36 am

Sat Nov 11, 2017 3:27 pm

So i am using it in conjunction with unity. it is a script to pull player stats from the database i just wanna know from if my php programming is sound?

Code: Select all

<?php
	require_once("config.php");

	$auth_host = $GLOBALS['auth_host'];
	$auth_user = $GLOBALS['auth_user'];
	$auth_pass = $GLOBALS['auth_pass'];
	$auth_dbase = $GLOBALS['auth_dbase'];
	
	
	$db = mysqli_connect($auth_host, $auth_user, $auth_pass,$auth_dbase) or die("Error " . mysqli_error($db));

	$user_name = mysqli_real_escape_string($db,$_POST['user']);
 
$sql = "SELECT level, xp, hp FROM account WHERE user = '$user_name'";
$result = $db->query($sql);

$row = mysqli_fetch_row($result);
if ($result->num_rows > 0) {
    while($row = mysqli_fetch_assoc($result)){
echo "level: ".$row['level']. "xp: ".$row['xp']. "hp: ".$row['hp']."<br>";
        }
    }

?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Sun Nov 12, 2017 3:06 am

Does it work, or do you get errors / corrupt data.

resilo
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Aug 05, 2017 9:36 am

Sun Nov 12, 2017 10:14 am

the return i get is from it is always null.
so i just want to make sure the scripts way of receiving the username and choosing the one that matches it from the database
is correct mostly
it is suppose to be exactly like my script for retrieving the monster data but
take into account the username and match it before retrieving the data
i have an additional login script that uses a similar method
so i know for sure that the variable user is being sent to the script correctly

here is the monster list script for refrence

Code: Select all

<?PHP
 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "accounts";
 
 
 
$con = new mysqli($servername, $username, $password, $dbname);
if (!$con){
        die('Could not connect: ' . mysqli_error());
        }
       
mysqli_select_db($con, "accounts") or die ("could not load the database" . mysqli_error());
 
$sql = "SELECT ID, Monster, XP FROM creature";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = mysqli_fetch_assoc($result)){
    
    echo "ID: ".$row['ID']. "Monster: ".$row['Monster']. "XP: ".$row['XP']."<br>";
        }
       }
?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Sun Nov 12, 2017 11:12 am

O.K. you want to do this the long and hard way:

The following is wrong:

Code: Select all

	$auth_host = $GLOBALS['auth_host'];
	$auth_user = $GLOBALS['auth_user'];
	$auth_pass = $GLOBALS['auth_pass'];
	$auth_dbase = $GLOBALS['auth_dbase'];
If you had error reporting set to display, you would have received a message to that effect.

Post Reply