Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konrad Pilch
Konrad Pilch
2,435 Points

PHP After a line it crashes

HI,

Why it doesnt wokr? i mean after i write the line where i put the comment, it doesnt work. It works if i just echo soemthing but it wonrt if i write this code. WHy is that?

<?php

if(isset($_POST['username'])&&isset($_POST['password'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $password_hash = md5($password);

    if(!empty($username) &&!empty($password)) {

        $query = "SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password_hash'";
        if ($query_run = mysqli_query($con, $query)) {
            $query_num_rows = mysqli_num_rows($query_run);

            if ($query_num_rows==0) {
                echo 'Invalid username/password combination.';
            } else if ($query_num_rows==1) {
                echo $user_id = mysqli_result($query_run, 0, 'id'); // After this line it doenst work
            }
        }


    } else{
        echo 'You must supply a username and password.';
    }
}

?>
<form action="<?php echo $current_file; ?>" method="POST">
    Username: <input type="text" name="username">
    Password: <input type="password" name="password">
    <input type="submit" value="Log in">
</form>
Zachary Green
Zachary Green
16,359 Points

What do you mean not work?

Konrad Pilch
Konrad Pilch
2,435 Points

After i add the line where is comment, it wont echo the user id which shoudl echo the user id . When i put just echo " ok"; It does work. But i need this code. If i log in to user alex password1 , it should show user id 1 , if i log in to billi pass123 , it should echo user id 2 . It wont . The code it self is half highlighted.

1 Answer

Zachary Green
Zachary Green
16,359 Points

i think i found your problem. mysqli_result is not a method but a class. try mysqli_fetch_assoc

post to give more info: http://stackoverflow.com/questions/17707331/fatal-error-call-to-undefined-function-mysqli-result

mysqli docs: http://php.net/manual/en/book.mysqli.php

also as a side note have you thought of using pdo for db calls?

Konrad Pilch
Konrad Pilch
2,435 Points

Thank you but It still doesn't work .

Im using this

<?php

$mysqli_host = 'localhost';
$mysqli_user = 'root';
$mysqli_pass = 'root';

$mysqli_db = 'a_database';

error_reporting(E_ALL);  // reports all errors to make it easier to debug

$con = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_pass);

if (!$con) {
    die("Could not connect to the database server");
} elseif (!mysqli_select_db($con, $mysqli_db)) {
    echo "Could not select the database<br />";
    die(mysqli_error($con));
}

?>

But i did use pdo in other projects or rather learning .