Page 5 of 6

Re: Code Issue

Posted: Fri Apr 13, 2012 12:34 pm
by Celauran
TheHappyPeanut wrote:Any books you'd recommend?
Already recommended a couple.

Re: Code Issue

Posted: Fri Apr 13, 2012 2:03 pm
by TheHappyPeanut
I completely forgot about that. Thanks for the reminder. There is one line of code giving me an error and that's the $result. I've Googled it and tried a fix, but it didn't seem to work, so I removed it. Here's the error I receive with that bit of code you gave me. Is it something I simply need to change?

Code: Select all

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\Tracker\login.php on line 10
Line 10:

Code: Select all

$result   = $mysqli->query($query)->fetch_assoc();

Re: Code Issue

Posted: Fri Apr 13, 2012 2:15 pm
by Celauran
Undo the method chaining.

Code: Select all

$result = $mysqli->query($query);
$user = $result->fetch_assoc();

Re: Code Issue

Posted: Fri Apr 13, 2012 2:24 pm
by TheHappyPeanut
Celauran wrote:Undo the method chaining.

Code: Select all

$result = $mysqli->query($query);
$user = $result->fetch_assoc();
The good news is that line 10 no longer has that issue. The bad news is that now line 11 has it. :P

Code: Select all

<?php

if (!empty($_POST))
{
    if (isset($_POST['username']) && isset($_POST['password']))
    {
        $mysqli   = new mysqli('localhost', 'root', '', 'tracker');
        $username = $mysqli->real_escape_string($_POST['username']);
        $query    = "SELECT id, password FROM 'tracker' WHERE username='$username'";
        $result   = $mysqli->query($query);
        $user = $result->fetch_assoc();
    }
    else
        die('Unable to proceed');
}
   
?>

Re: Code Issue

Posted: Fri Apr 13, 2012 2:26 pm
by Celauran
That means the query is returning false and is therefore no good.

EDIT: Remove the quotes around the table name.

Re: Code Issue

Posted: Fri Apr 13, 2012 2:29 pm
by TheHappyPeanut
Heh... full of issues today, it seems.

Code: Select all

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\Tracker\login.php on line 9

Code: Select all

<?php

if (!empty($_POST))
{
    if (isset($_POST['username']) && isset($_POST['password']))
    {
        $mysqli   = new mysqli('localhost', 'root', '', 'tracker');
        $username = $mysqli->real_escape_string($_POST['username']);
        $query    = SELECT id, password FROM 'tracker' WHERE username='$username';
        $result   = $mysqli->query($query);
        $user = $result->fetch_assoc();
    }
    else
        die('Unable to proceed');
}
   
?>

Re: Code Issue

Posted: Fri Apr 13, 2012 2:36 pm
by Celauran
Not around the query string, around the table name.

Code: Select all

        $query    = "SELECT id, password FROM tracker WHERE username='$username'";

Re: Code Issue

Posted: Fri Apr 13, 2012 2:51 pm
by TheHappyPeanut
Still has the issue. :(

Code: Select all

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\Tracker\login.php on line 11
and here's my code:

Code: Select all

<?php

if (!empty($_POST))
{
    if (isset($_POST['username']) && isset($_POST['password']))
    {
        $mysqli   = new mysqli('localhost', 'root', '', 'tracker');
        $username = $mysqli->real_escape_string($_POST['username']);
        $query    = "SELECT id, password FROM tracker WHERE username='$username'";
        $result   = $mysqli->query($query);
        $user = $result->fetch_assoc();
    }
    else
        die('Unable to proceed');
}
   
?>

Re: Code Issue

Posted: Fri Apr 13, 2012 2:53 pm
by Celauran
What happens when you run the query manually?

Re: Code Issue

Posted: Fri Apr 13, 2012 2:57 pm
by TheHappyPeanut
I'm not really sure how to run a manual query in phpMyAdmin... I've never done it before :/

Re: Code Issue

Posted: Fri Apr 13, 2012 2:59 pm
by Celauran
Log in, choose your database on the left, click the SQL tab at the top, write your query. You could also just do it from the console.

Re: Code Issue

Posted: Fri Apr 13, 2012 3:13 pm
by TheHappyPeanut
[text]#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query = "SELECT id, password FROM tracker WHERE username='$username'"' at line 1[/text]

Re: Code Issue

Posted: Fri Apr 13, 2012 3:15 pm
by Celauran
No, you can't paste PHP code into MySQL.

Code: Select all

SELECT id, password FROM tracker WHERE username = 'replace_this_with_username'

Re: Code Issue

Posted: Fri Apr 13, 2012 3:17 pm
by TheHappyPeanut
I fixed it. I was using "trackers" when I should have been using "users". Thanks for the help with that. Also, what exactly does this query do?

Re: Code Issue

Posted: Fri Apr 13, 2012 3:20 pm
by TheHappyPeanut
Sent you a PM by the way.