Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Query skips out of IF and go to else

Junior Spellweaver
Joined
Dec 29, 2015
Messages
111
Reaction score
75
Hey guys. It's me again.
I'm currently working on some new stuff for myself. And I got a really big problem.

I look if the table have an value, if the table don't have a value, then echo "n0 p0s7s h3r3".
But he skips all the time the if-loop and go into the else-loop. But the text 'n0 [...]' will display.

My Code:

Code:
    public function getLastPostFromCat($subID) {
        $subID = $this->Database()->escape(htmlspecialchars($subID));


        $threadStmt = $this->Database()->getCon()->query("SELECT * FROM forum_threads WHERE subID = ".$subID." ORDER BY forumID DESC LIMIT 1");


        if($threadStmt->rowCount() == 0) {
            echo "No posts here";
        } else {
            $thread = $threadStmt->fetchObject();
            $postStmt = $this->Database()->getCon()->query("SELECT userID, forumID FROM forum_posts WHERE forumID = ".$thread->forumID);


            return $postStmt->fetchObject();
        }
    }

Code:
  public function getThreadByID($id) {
        $id = $this->Database()->escape($id);
        $stmt = $this->Database()->select('*')->from('forum_threads')->where("forumID = ".$id);


    if($stmt->row() == 0) {
      //return nothing
    } else {
      return $stmt->get();
    }
    }

Code:
<a href="/thread/december-and-january-update-495/"><?= htmlspecialchars($this->ForumThreads->getThreadByID($this->ForumPosts->getLastPostFromCat($sub['subID'])->forumID)->forumTitle); ?></a>

Error:
Code:
[URL="http://localhost/thread/december-and-january-update-495/"]No posts hereError: 8
Message: Trying to get property of non-object 
File: C:\xampp\htdocs\application\views\Home\Home.tpl
Line: 21[/URL]
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
What does $threadStmt->$rowCount() return?

Side note, it would be better to rather than concatenating it inside of the query string.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
strange.. 0 is 0 is 0. Investigate your problem a little deeper, but saying in code .. if(0 == 0) you will get through that condition.. The computer doesn't make mistakes ;)
 
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
It might be possible that your SQL query does not return what you expect it to. You should try to separate your SQL code into a real SQL query and run it against your database directly. See if the results you expect are returned, and so on. Maybe it does return rows you do not expect from it because your query produces faulty output out of a logic error.
 
Unknown Place
Joined
Mar 7, 2013
Messages
580
Reaction score
87
You could try printing the results and checking what is resulted, then make the proper changes.
 
git bisect -m
Loyal Member
Joined
Sep 2, 2011
Messages
2,171
Reaction score
916
PHP:
$this->Database()->getCon()->query
Kylon this path is huge. Try implement a cleanest Design Pattern. Haha for few seconds i was thinking in Jinja for PHP (But Jinja is a Template engine, and yes, a lot of people uses it for "Native SQL Queries being Dynamic" don't do that Ky...) Try Implement a Design Pattern following Factories.

Here is live Example:
You can see other example here: (Go ahead Chapter 4 > App.Ado)

In Java is DAO but in PHP is ADO. wut.
 
Junior Spellweaver
Joined
Dec 29, 2015
Messages
111
Reaction score
75
PHP:
$this->Database()->getCon()->query
@Kylon this path is huge. Try implement a cleanest Design Pattern. Haha for few seconds i was thinking in Jinja for PHP (But Jinja is a Template engine, and yes, a lot of people uses it for "Native SQL Queries being Dynamic" don't do that Ky...) Try Implement a Design Pattern following Factories.

Here is live Example:
You can see other example here: (Go ahead Chapter 4 > App.Ado)

In Java is DAO but in PHP is ADO. wut.
bruuh, that's an old thread. :'(
<- updated version <3
 
Back
Top