The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Unexpected 'Select' Y_STRING
masonh928
post Mar 27 2016, 06:20 PM
Post #1


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



CODE

<?php

Class Blog{

private $Connect;

public function __construct(){

$this->Connect = new PDO(*******);

}

public function displayPosts($Category){

$Category = (empty($Category)) ? 1 : 2;

if($Category == 1){

$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts");

}
if($Category == 2){

$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE Category = ". $Category .");

}

$Query->execute();

return $Query->fetchAll();
}


public function viewPost($PostID){

$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE ID = :PostID");

$Query->bindValue(":PostID", $PostID);
$Query->execute();

return $Query->fetchAll();
}

}
?>


Here's the line:

CODE

$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE ID = :PostID");



Whenever I include it, it says "Unexpected End of File"

This post has been edited by masonh928: Mar 27 2016, 06:23 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 27 2016, 07:35 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



This line of code looks wrong to me:
CODE
$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE Category = ". $Category .");
It should be:
CODE
$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE Category = $Category");
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Mar 28 2016, 03:39 PM
Post #3


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



hmm... I too was iffy of that; however, it threw no error, but I will change it just in case.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 28 2016, 04:31 PM
Post #4


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Of course it threw an error: "Unexpected 'Select' Y_STRING" according to your post. I don't understand why you aren't consistent with your coding style. This function:
CODE
viewPost($PostID)
uses placeholders and bindValue while this function:
CODE
displayPosts($Category)
doesn't. Maybe because the value is supplied by your code and not the user? I would still stick to 1 style in the class.

As for the code I pointed out. You use concat for this line:
CODE
$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE Category = ". $Category .");
which means you need a closing double quote to close the string. You could have done it this way:
CODE
$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE Category = ". $Category .'"');
or this way:
CODE
$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE Category = ". $Category ."""");
but way mess with either? Since you use double quotes around the string all you really need is:
CODE
$Query = $this->Connect->prepare("SELECT * FROM Blog_Posts WHERE Category = $Category");
and let PHP substitute the value.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Mar 28 2016, 04:50 PM
Post #5


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



The error in the blog script is remedied.

However this still yields a 'Unexpected End of File' error.

CODE

<?php  
    
       include_once("Blog.class.php");

                 $Blog = new Blog();
  
//$BlogDP = $Blog->displayPosts();

foreach($Blog->displayPosts() as $Post){
  
echo <<<HTML
            <pre>
                 <fieldset>
                       <a href="/viewPost.php?PostID={$Post['ID']}"><h3>{$Post['Subject']}</h3></a>
                 </fieldset>
            </pre>
HTML;  

}


include("INC/footer.php");
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Mar 28 2016, 05:06 PM
Post #6


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



My Blog Class is fine now though. It gives no errors or warnings. I don't understand how you say my style is different. One of them must be supplied by a $_GET value for $Category, so does $PostID

This post has been edited by masonh928: Mar 28 2016, 05:08 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 28 2016, 05:36 PM
Post #7


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(masonh928 @ Mar 28 2016, 04:50 PM) *

The error in the blog script is remedied.

However this still yields a 'Unexpected End of File' error.

CODE

<?php  
    
       include_once("Blog.class.php");

                 $Blog = new Blog();
  
//$BlogDP = $Blog->displayPosts();

foreach($Blog->displayPosts() as $Post){
  
echo <<<HTML
            <pre>
                 <fieldset>
                       <a href="/viewPost.php?PostID={$Post['ID']}"><h3>{$Post['Subject']}</h3></a>
                 </fieldset>
            </pre>
HTML;  

}


include("INC/footer.php");
?>


I don't see anything that pops out at me. Is this the complete code? One thing to check is that there is no space or tab after the closing heredoc id (HTML;). There must be a newline character after the semi-colon.

This post has been edited by CharlesEF: Mar 28 2016, 05:40 PM
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 28 2016, 05:39 PM
Post #8


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(masonh928 @ Mar 28 2016, 05:06 PM) *

My Blog Class is fine now though. It gives no errors or warnings. I don't understand how you say my style is different. One of them must be supplied by a $_GET value for $Category, so does $PostID

I don;t see any reference to $_GET in your code. What I was trying to say is that if you use placeholders and bindValue in 1 function then why not use it in all functions?
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Mar 28 2016, 05:52 PM
Post #9


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



I missed a whitespace after the closing, I was under the impression that you could have whitespace, but not any regular characters, guess not. Thanks!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 28 2016, 05:58 PM
Post #10


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(masonh928 @ Mar 28 2016, 05:52 PM) *

I missed a whitespace after the closing, I was under the impression that you could have whitespace, but not any regular characters, guess not. Thanks!

Glad you got it fixed. The only thing allowed after the closing heredoc id is a newline.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 05:03 PM