0

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");
}
?>
  • you don't see errors when you don't check for them and you have one, as in your `WHERE username=$user";` and that's a string http://dev.mysql.com/doc/en/string-literals.html. Here, http://php.net/manual/en/mysqli.error.php use that on your query and http://php.net/manual/en/function.error-reporting.php while you're at it and set to display. – Funk Forty Niner Apr 29 '16 at 00:30
  • *with errors turned on ofc* - Doesn't look like it. Never seen a `while / else` block before. – Mike Apr 29 '16 at 00:30
  • possible duplicate of [When to use single quotes, double quotes, and backticks?](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) – Funk Forty Niner Apr 29 '16 at 00:31
  • possible duplicate of http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Mike Apr 29 '16 at 00:32
  • `if (mysqli_num_rows($result) > 0) {` missing a brace for that too. – Funk Forty Niner Apr 29 '16 at 00:33
  • See also: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Mike Apr 29 '16 at 00:36

1 Answers1

1

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";
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141