/    Sign up×
Community /Pin to ProfileBookmark

PHP Parse error: syntax error, unexpected ‘>’

Hey there, first post here.

This is a simple code (I’m a beginner) that I’ve been writing with a video tutorial:

<?php

$con = mysqli_connect(“localhost”,”username”,”blabla”,”db”);

//getting the categories

function getCats(){

global $con;

$get_cats = “select * from categories”;

$run_cats = “mysqli_query($connection, $get_cats);

while ($row_cats = mysqli_fetch_array($run_cats)){

$cat_id = $row_cats [‘cat_id];
$cat_title = $row_cats [‘cat_title];

echo “<li><a href=”#”>cat_title</a></li>”;

}

}

?>

The error points up to “echo “<li><a href=”#”>cat_title</a></li>”;” saying PHP Parse error: syntax error, unexpected ‘>’ . The thing is, this is exactly written in the same way as the video tutorial. This keeps giving me this error and I believe it’s the reason when I try to include this function on my html page all the content disappears.

Thanks in advance.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@KrauserauthorJun 30.2015 — My first thread on the forums

[CODE]<?php

$con = mysqli_connect("localhost","myusername","blabla","db");


//getting the categories

function getCats(){

global $con;

$get_cats = "select * from categories";

$run_cats = "mysqli_query($connection, $get_cats);

while (row_cats = mysqli_fetch_array($run_cats)){

$cat_id = $row_cats ['cat_id];
$cat_title = $row_cats ['cat_title];




[B]echo "<li><a href="#">$cat_title</a></li>";[/B]

}
}


?>
[/CODE]


The bold code line gives me this error: PHP Parse error: syntax error, unexpected '>' in functions.php on line 24. I've tried some changes but nothing.


Thanks in advance.
Copy linkTweet thisAlerts:
@ginerjmJun 30.2015 — You have a double quote where none should be.

$run_cats = "mysqli_query($connection, $get_cats);

You're missing a $ on a var name.

while (row_cats = mysqli_fetch_array($run_cats))

You're missing a single quote where one should be in two similar places.

$cat_id = $row_cats ['cat_id];

$cat_title = $row_cats ['cat_title];

You're using double quotes for two purposes in your last statement which will be a conflict. echo "<li><a href="#">$cat_title</a></li>";

Alternate single and double ones in this case.

If you are using a php aware editor it should be highlighted or the color of the text might be off. In any case, as part of your practice on becoming a talented programmer look for these errors.

(I had mercy at the last moment and included the error lines for you. Call me soft.)

TURN ON php error checking too.
Copy linkTweet thisAlerts:
@NogDogJun 30.2015 — You must be very careful with your quoting: computers are stupid and don't think for themselves. I made several adjustments, adding comments where I did:
[code=php]
<?php

$con = mysqli_connect("localhost", "username", "blabla", "db");

//getting the categories

function getCats()
{

global $con; // UGH!!! Pass that in as a parameter!!!

$get_cats = "select * from categories";

$run_cats = mysqli_query($connection, $get_cats); // don't quote this

while ($row_cats = mysqli_fetch_array($run_cats)) {

$cat_id = $row_cats ['cat_id']; // missing closing quote
$cat_title = $row_cats ['cat_title']; // another missing closing quote

echo "<li><a href="#">cat_title</a></li>"; // must escape quotes within string

}

}
[/code]
Copy linkTweet thisAlerts:
@NogDogJun 30.2015 — Merged the duplicate threads.
Copy linkTweet thisAlerts:
@KrauserauthorJul 01.2015 — Oh, sorry about the double threads. I thought my first one didn't work for some reason so I created a second one.

Man, pure lack of attention there from me, about the code. Thanks a lot guys.
Copy linkTweet thisAlerts:
@soo92Sep 17.2015 — do you know how to add subcategories?
Copy linkTweet thisAlerts:
@NogDogSep 17.2015 — do you know how to add subcategories?[/QUOTE]

Please ask a new question in a new thread, as opposed to doing it in a reply to an old thread. (And if/when you do, that question is probably way too vague to elicit a useful answer.)

PS: This might be a good time to follow the link in my signature. ?
×

Success!

Help @Krauser spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...