an issue with php pdo mysql
I want to print last post from specific category
Could you please help me with the code?
I want to put on $record
manual, for example: I put "design"
, and just show the last post in design category.
And one thing: table blog
it's separate from table record
.
thanks
<?php $category = $record ['record']; { ?>
<?php foreach($db->query("select * from blog where category = '$category' order by id desc") as $row){ ?>
<li>
<a href="<?php echo $row['image']; ?>">
<div class="gallery-item"><img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>"></div>
</a>
</li>
<?php } } ?>
php mysql database pdo
add a comment |
I want to print last post from specific category
Could you please help me with the code?
I want to put on $record
manual, for example: I put "design"
, and just show the last post in design category.
And one thing: table blog
it's separate from table record
.
thanks
<?php $category = $record ['record']; { ?>
<?php foreach($db->query("select * from blog where category = '$category' order by id desc") as $row){ ?>
<li>
<a href="<?php echo $row['image']; ?>">
<div class="gallery-item"><img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>"></div>
</a>
</li>
<?php } } ?>
php mysql database pdo
1
What is an issue getting now and what your expected output?
– Sadikhasan
Nov 14 '18 at 12:55
Try this queryselect * from blog where category = '$category' order by id desc LIMIT 1
– Sadikhasan
Nov 14 '18 at 12:57
You have been asked what results you're getting now as opposed to the desired results. Can you interact with people in comments please? So that you can clarify your question. @Ebi Your question is unclear.
– Funk Forty Niner
Nov 14 '18 at 13:09
add a comment |
I want to print last post from specific category
Could you please help me with the code?
I want to put on $record
manual, for example: I put "design"
, and just show the last post in design category.
And one thing: table blog
it's separate from table record
.
thanks
<?php $category = $record ['record']; { ?>
<?php foreach($db->query("select * from blog where category = '$category' order by id desc") as $row){ ?>
<li>
<a href="<?php echo $row['image']; ?>">
<div class="gallery-item"><img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>"></div>
</a>
</li>
<?php } } ?>
php mysql database pdo
I want to print last post from specific category
Could you please help me with the code?
I want to put on $record
manual, for example: I put "design"
, and just show the last post in design category.
And one thing: table blog
it's separate from table record
.
thanks
<?php $category = $record ['record']; { ?>
<?php foreach($db->query("select * from blog where category = '$category' order by id desc") as $row){ ?>
<li>
<a href="<?php echo $row['image']; ?>">
<div class="gallery-item"><img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>"></div>
</a>
</li>
<?php } } ?>
php mysql database pdo
php mysql database pdo
edited Nov 16 '18 at 13:19
barbsan
2,43721223
2,43721223
asked Nov 14 '18 at 12:50
EbiEbi
11
11
1
What is an issue getting now and what your expected output?
– Sadikhasan
Nov 14 '18 at 12:55
Try this queryselect * from blog where category = '$category' order by id desc LIMIT 1
– Sadikhasan
Nov 14 '18 at 12:57
You have been asked what results you're getting now as opposed to the desired results. Can you interact with people in comments please? So that you can clarify your question. @Ebi Your question is unclear.
– Funk Forty Niner
Nov 14 '18 at 13:09
add a comment |
1
What is an issue getting now and what your expected output?
– Sadikhasan
Nov 14 '18 at 12:55
Try this queryselect * from blog where category = '$category' order by id desc LIMIT 1
– Sadikhasan
Nov 14 '18 at 12:57
You have been asked what results you're getting now as opposed to the desired results. Can you interact with people in comments please? So that you can clarify your question. @Ebi Your question is unclear.
– Funk Forty Niner
Nov 14 '18 at 13:09
1
1
What is an issue getting now and what your expected output?
– Sadikhasan
Nov 14 '18 at 12:55
What is an issue getting now and what your expected output?
– Sadikhasan
Nov 14 '18 at 12:55
Try this query
select * from blog where category = '$category' order by id desc LIMIT 1
– Sadikhasan
Nov 14 '18 at 12:57
Try this query
select * from blog where category = '$category' order by id desc LIMIT 1
– Sadikhasan
Nov 14 '18 at 12:57
You have been asked what results you're getting now as opposed to the desired results. Can you interact with people in comments please? So that you can clarify your question. @Ebi Your question is unclear.
– Funk Forty Niner
Nov 14 '18 at 13:09
You have been asked what results you're getting now as opposed to the desired results. Can you interact with people in comments please? So that you can clarify your question. @Ebi Your question is unclear.
– Funk Forty Niner
Nov 14 '18 at 13:09
add a comment |
1 Answer
1
active
oldest
votes
If you want the most recent post, you could change your SQL to select it.
Try something like this:
select * from blog where category = '$category' order by {DATE_FIELD} desc limit 1
You need to exchange the string {DATE_FIELD} with the actual date field in your table. This select would return the most recent dataset and only that one.
EDIT: You can also sort by youre id if the date isn't changed or the changed date is stored in another field.
select * from blog where category = '$category' order by id desc limit 1
could you please explain with an example
– Ebi
Nov 14 '18 at 13:04
just load the return of your select in a variable like this: $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1"). After that you can access all information with $postData['FIELDNAME']. For example $postData['image'] or $postData['title'] etc.
– Marcus Rommel
Nov 14 '18 at 13:08
$postData['image'] or $postData['title'] will only work if this fields exists. I just assumed that because of the code you provided. If you want to know what properties are available do a var_dump <?php var_dump($postData) ?>
– Marcus Rommel
Nov 14 '18 at 13:12
i have problem with this code <?php $category = $record ['record']; { ?> the one you said it's working, i have some record on the the different i want to get that record.. for example <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") but this code doesn't work..
– Ebi
Nov 14 '18 at 13:19
in this line: " <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") " youre closing the php right after " $category = $record ['design']; { " with the "?>" amd the is a wrong closing bracket. You need to remove the closing bracket and move the "?>" at the end of your php statement like this: <?php $category = $record ['design']; $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") ?>
– Marcus Rommel
Nov 14 '18 at 13:29
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300670%2fan-issue-with-php-pdo-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want the most recent post, you could change your SQL to select it.
Try something like this:
select * from blog where category = '$category' order by {DATE_FIELD} desc limit 1
You need to exchange the string {DATE_FIELD} with the actual date field in your table. This select would return the most recent dataset and only that one.
EDIT: You can also sort by youre id if the date isn't changed or the changed date is stored in another field.
select * from blog where category = '$category' order by id desc limit 1
could you please explain with an example
– Ebi
Nov 14 '18 at 13:04
just load the return of your select in a variable like this: $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1"). After that you can access all information with $postData['FIELDNAME']. For example $postData['image'] or $postData['title'] etc.
– Marcus Rommel
Nov 14 '18 at 13:08
$postData['image'] or $postData['title'] will only work if this fields exists. I just assumed that because of the code you provided. If you want to know what properties are available do a var_dump <?php var_dump($postData) ?>
– Marcus Rommel
Nov 14 '18 at 13:12
i have problem with this code <?php $category = $record ['record']; { ?> the one you said it's working, i have some record on the the different i want to get that record.. for example <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") but this code doesn't work..
– Ebi
Nov 14 '18 at 13:19
in this line: " <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") " youre closing the php right after " $category = $record ['design']; { " with the "?>" amd the is a wrong closing bracket. You need to remove the closing bracket and move the "?>" at the end of your php statement like this: <?php $category = $record ['design']; $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") ?>
– Marcus Rommel
Nov 14 '18 at 13:29
|
show 4 more comments
If you want the most recent post, you could change your SQL to select it.
Try something like this:
select * from blog where category = '$category' order by {DATE_FIELD} desc limit 1
You need to exchange the string {DATE_FIELD} with the actual date field in your table. This select would return the most recent dataset and only that one.
EDIT: You can also sort by youre id if the date isn't changed or the changed date is stored in another field.
select * from blog where category = '$category' order by id desc limit 1
could you please explain with an example
– Ebi
Nov 14 '18 at 13:04
just load the return of your select in a variable like this: $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1"). After that you can access all information with $postData['FIELDNAME']. For example $postData['image'] or $postData['title'] etc.
– Marcus Rommel
Nov 14 '18 at 13:08
$postData['image'] or $postData['title'] will only work if this fields exists. I just assumed that because of the code you provided. If you want to know what properties are available do a var_dump <?php var_dump($postData) ?>
– Marcus Rommel
Nov 14 '18 at 13:12
i have problem with this code <?php $category = $record ['record']; { ?> the one you said it's working, i have some record on the the different i want to get that record.. for example <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") but this code doesn't work..
– Ebi
Nov 14 '18 at 13:19
in this line: " <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") " youre closing the php right after " $category = $record ['design']; { " with the "?>" amd the is a wrong closing bracket. You need to remove the closing bracket and move the "?>" at the end of your php statement like this: <?php $category = $record ['design']; $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") ?>
– Marcus Rommel
Nov 14 '18 at 13:29
|
show 4 more comments
If you want the most recent post, you could change your SQL to select it.
Try something like this:
select * from blog where category = '$category' order by {DATE_FIELD} desc limit 1
You need to exchange the string {DATE_FIELD} with the actual date field in your table. This select would return the most recent dataset and only that one.
EDIT: You can also sort by youre id if the date isn't changed or the changed date is stored in another field.
select * from blog where category = '$category' order by id desc limit 1
If you want the most recent post, you could change your SQL to select it.
Try something like this:
select * from blog where category = '$category' order by {DATE_FIELD} desc limit 1
You need to exchange the string {DATE_FIELD} with the actual date field in your table. This select would return the most recent dataset and only that one.
EDIT: You can also sort by youre id if the date isn't changed or the changed date is stored in another field.
select * from blog where category = '$category' order by id desc limit 1
answered Nov 14 '18 at 13:01
Marcus RommelMarcus Rommel
88011017
88011017
could you please explain with an example
– Ebi
Nov 14 '18 at 13:04
just load the return of your select in a variable like this: $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1"). After that you can access all information with $postData['FIELDNAME']. For example $postData['image'] or $postData['title'] etc.
– Marcus Rommel
Nov 14 '18 at 13:08
$postData['image'] or $postData['title'] will only work if this fields exists. I just assumed that because of the code you provided. If you want to know what properties are available do a var_dump <?php var_dump($postData) ?>
– Marcus Rommel
Nov 14 '18 at 13:12
i have problem with this code <?php $category = $record ['record']; { ?> the one you said it's working, i have some record on the the different i want to get that record.. for example <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") but this code doesn't work..
– Ebi
Nov 14 '18 at 13:19
in this line: " <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") " youre closing the php right after " $category = $record ['design']; { " with the "?>" amd the is a wrong closing bracket. You need to remove the closing bracket and move the "?>" at the end of your php statement like this: <?php $category = $record ['design']; $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") ?>
– Marcus Rommel
Nov 14 '18 at 13:29
|
show 4 more comments
could you please explain with an example
– Ebi
Nov 14 '18 at 13:04
just load the return of your select in a variable like this: $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1"). After that you can access all information with $postData['FIELDNAME']. For example $postData['image'] or $postData['title'] etc.
– Marcus Rommel
Nov 14 '18 at 13:08
$postData['image'] or $postData['title'] will only work if this fields exists. I just assumed that because of the code you provided. If you want to know what properties are available do a var_dump <?php var_dump($postData) ?>
– Marcus Rommel
Nov 14 '18 at 13:12
i have problem with this code <?php $category = $record ['record']; { ?> the one you said it's working, i have some record on the the different i want to get that record.. for example <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") but this code doesn't work..
– Ebi
Nov 14 '18 at 13:19
in this line: " <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") " youre closing the php right after " $category = $record ['design']; { " with the "?>" amd the is a wrong closing bracket. You need to remove the closing bracket and move the "?>" at the end of your php statement like this: <?php $category = $record ['design']; $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") ?>
– Marcus Rommel
Nov 14 '18 at 13:29
could you please explain with an example
– Ebi
Nov 14 '18 at 13:04
could you please explain with an example
– Ebi
Nov 14 '18 at 13:04
just load the return of your select in a variable like this: $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1"). After that you can access all information with $postData['FIELDNAME']. For example $postData['image'] or $postData['title'] etc.
– Marcus Rommel
Nov 14 '18 at 13:08
just load the return of your select in a variable like this: $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1"). After that you can access all information with $postData['FIELDNAME']. For example $postData['image'] or $postData['title'] etc.
– Marcus Rommel
Nov 14 '18 at 13:08
$postData['image'] or $postData['title'] will only work if this fields exists. I just assumed that because of the code you provided. If you want to know what properties are available do a var_dump <?php var_dump($postData) ?>
– Marcus Rommel
Nov 14 '18 at 13:12
$postData['image'] or $postData['title'] will only work if this fields exists. I just assumed that because of the code you provided. If you want to know what properties are available do a var_dump <?php var_dump($postData) ?>
– Marcus Rommel
Nov 14 '18 at 13:12
i have problem with this code <?php $category = $record ['record']; { ?> the one you said it's working, i have some record on the the different i want to get that record.. for example <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") but this code doesn't work..
– Ebi
Nov 14 '18 at 13:19
i have problem with this code <?php $category = $record ['record']; { ?> the one you said it's working, i have some record on the the different i want to get that record.. for example <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") but this code doesn't work..
– Ebi
Nov 14 '18 at 13:19
in this line: " <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") " youre closing the php right after " $category = $record ['design']; { " with the "?>" amd the is a wrong closing bracket. You need to remove the closing bracket and move the "?>" at the end of your php statement like this: <?php $category = $record ['design']; $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") ?>
– Marcus Rommel
Nov 14 '18 at 13:29
in this line: " <?php $category = $record ['design']; { ?> $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") " youre closing the php right after " $category = $record ['design']; { " with the "?>" amd the is a wrong closing bracket. You need to remove the closing bracket and move the "?>" at the end of your php statement like this: <?php $category = $record ['design']; $postData = $db->query("select * from blog where category = '$category' order by id desc limit 1") ?>
– Marcus Rommel
Nov 14 '18 at 13:29
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300670%2fan-issue-with-php-pdo-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What is an issue getting now and what your expected output?
– Sadikhasan
Nov 14 '18 at 12:55
Try this query
select * from blog where category = '$category' order by id desc LIMIT 1
– Sadikhasan
Nov 14 '18 at 12:57
You have been asked what results you're getting now as opposed to the desired results. Can you interact with people in comments please? So that you can clarify your question. @Ebi Your question is unclear.
– Funk Forty Niner
Nov 14 '18 at 13:09