Code Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

TheHappyPeanut wrote:Any books you'd recommend?
Already recommended a couple.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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();
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

Undo the method chaining.

Code: Select all

$result = $mysqli->query($query);
$user = $result->fetch_assoc();
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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');
}
   
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

That means the query is returning false and is therefore no good.

EDIT: Remove the quotes around the table name.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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');
}
   
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

Not around the query string, around the table name.

Code: Select all

        $query    = "SELECT id, password FROM tracker WHERE username='$username'";
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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');
}
   
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

What happens when you run the query manually?
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

I'm not really sure how to run a manual query in phpMyAdmin... I've never done it before :/
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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]
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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'
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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?
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

Sent you a PM by the way.
Post Reply