Trying to access array offset on value of type null
- Cupara
- Member | 90
I'm struggling to get a specific news article to load after I click on it from the news page.
Here is everything so far.
The errors:
news.php file:
news_detail.latte file:
And the corresponding php file from the temp folder:
This is the last thing I need to get fixed then I can start working on the color scheme and get my site launched.
Thanks
Last edited by Cupara (2023-12-22 14:30)
- David Grudl
- Nette Core | 8249
It tells you that $news2
is null,
$result->fetch_assoc()
returns null. This has nothing to do
with Latte.
- nightfish
- Member | 525
Cupara wrote:
So I did var_dump($news2); and it returns NULL but I'm not understanding why so I'm scouring through the docs for Latte to make sure I implemented the call right. I'm not used to using $result->fetch_assoc(); to fetch a single entry.
@Cupara
As David pointed out earlier, the problem is not in Latte. It is in these
two lines:
First problem: row 1 contains SQL injection vulnerability. Use prepared
statements. If I am correct in the assumption that you are using
mysqli
class, it would be something like:
Second problem – condition in your SQL query probably does not match any
rows – which is why $news2
is NULL.
- Cupara
- Member | 90
I don't see how it's not right as the slug is being passed otherwise the link wouldn't work properly. Maybe it's an issue in my .htaccess file and how it's parsing the link to prettify it.
Thanks for pointing out the prepared statement vulnerability. I haven't played with PHP in quite a few years and I'm forgetting so much stuff.
- nightfish
- Member | 525
@Cupara Well… you could dump your query with specific value for the
slug (“SELECT * FROM news WHERE slug = ‘foobar’”) and then execute it in
your database admin tool (Adminer, PHPStorm etc.) to see if it returns anything.
Also maybe dump $_GET['slug']
to verify that it does not contain
any trailing whitespace or other unwanted characters.
- Cupara
- Member | 90
@nightfish I am running out of ideas as well.
Here is the link to it online: https://serenitydev.xyz/…-is-a-test-2
- Cupara
- Member | 90
I'm honestly thinking about starting over from scratch for the 15th time. A lot of the startovers prior were because I was having way too many issues with specific template engine systems that I just didn't want to deal with but when I got to Latte, I liked it so much I wanted to deal with the issues I kept running into.
- nightfish
- Member | 525
@Cupara The db.php
file looks okay-ish, but I would
probably advise against using mysqli
. It is preferable to use
PDO
or rather another higher-level database abstraction layer
(nette/database, dibi/dibi, doctrine/dbal, nextras/dbal).
If you want to go full-Nette, I would advise using
nette/database
, as it can be tightly integrated with Nette.
And maybe base your project on nette/web-project
and if you are new
to Nette, go through the tutorial – https://doc.nette.org/en/quickstart.
It will show you how to create a simple blog website (not dissimilar to your
News/NewsDetail).