user4483652
user4483652

Reputation:

PHP MySQLi Procedural get results

I'm trying to fetch last_ip, last_ua, ui_walks, ui_lastwalk and ui_walkstotal from MySQL using PHP.
I want to assign each result (row) to a PHP variable.

Weird thing is that I dont get any error, just a blank page, with errors turned on ofc.
Tried on my localserver and with a public server (One.com) but nothing happens.

What am I doing wrong?

index.php

<?php
session_start();
include_once 'includes/db.config.php';
$_SESSION['user'] = "Dennis";
$user = mysqli_real_escape_string($connection, $_SESSION['user']);
$sql = "SELECT last_ip, last_ua, ui_walks, ui_lastwalk, ui_walkstotal FROM MyLog WHERE username=$user";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$lastip = $row['last_ip'];
$lastua = $row['last_ua'];
$uiwalks = $row['ui_walks'];
$uilastwalk = $row['ui_lastwalk'];
$uiwalkstotal = $row['ui_walkstotal'];
echo $lastip;
} else {
// No results
echo "No results";
}
?>

db.config.php

<?php
define ("HOST", "localhost");
define ("USER", "user");
define ("PASS", "pass");
define ("DB", "my_db");
$connection = mysqli_connect(HOST, USER, PASS, DB);
if (mysqli_connect_errno()) {
die("No database connection");
}
?>

Upvotes: 0

Views: 292

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

I've already outlined what was wrong in comments so I don't see why I should be repeating it here.

Here's your fixed code and making it a community wiki at the same time, I want no rep from this:

Sidenote: Indenting helps (wink)

<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();

include_once 'includes/db.config.php';
$_SESSION['user'] = "Dennis";
$user = mysqli_real_escape_string($connection, $_SESSION['user']);

$sql = "SELECT last_ip, last_ua, ui_walks, ui_lastwalk, ui_walkstotal 
        FROM MyLog WHERE username='$user'";

$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));

if (mysqli_num_rows($result) > 0) {

    while ($row = mysqli_fetch_assoc($result)) {
    $lastip = $row['last_ip'];
    $lastua = $row['last_ua'];
    $uiwalks = $row['ui_walks'];
    $uilastwalk = $row['ui_lastwalk'];
    $uiwalkstotal = $row['ui_walkstotal'];
    echo $lastip;
    }

} else {
// No results
echo "No results";
}
?>

Upvotes: 1

Related Questions