PHP MySQL select all matching rows ans echo another table rows












1















I have 2 table message and answer. where message id = answer id. I want select using message. when message = hi then expected answer hello, hey.



Message Table:
| id | message |
| 1 | hi |
| 2 | hi |
| 3 | Hi |


and answer table:



| id | answer |
| 1 | hello |
| 2 | hey |
| 3 | Hello |


I already tried:
$inp = "hi";



SELECT * FROM message JOIN answer ON message.message = answer.answer WHERE message='$inp'"


any suggestions?










share|improve this question




















  • 1





    Please share your latest code attempt; this would help us in pointing you to right direction.

    – Madhur Bhaiya
    Nov 23 '18 at 5:18











  • now may you can understand!

    – Rabib
    Nov 23 '18 at 5:25






  • 2





    SELECT * FROM answer JOIN message ON message.id= answer.id WHERE message='$inp'" Any luck with this.

    – Prashant Deshmukh.....
    Nov 23 '18 at 5:25








  • 1





    Always join using the appropriate relationships. As specified in previous comment, you should using id not message/answer

    – Madhur Bhaiya
    Nov 23 '18 at 5:28






  • 1





    Also, Your code is open to SQL injection related attacks. Please learn to use Prepared Statements

    – Madhur Bhaiya
    Nov 23 '18 at 5:28
















1















I have 2 table message and answer. where message id = answer id. I want select using message. when message = hi then expected answer hello, hey.



Message Table:
| id | message |
| 1 | hi |
| 2 | hi |
| 3 | Hi |


and answer table:



| id | answer |
| 1 | hello |
| 2 | hey |
| 3 | Hello |


I already tried:
$inp = "hi";



SELECT * FROM message JOIN answer ON message.message = answer.answer WHERE message='$inp'"


any suggestions?










share|improve this question




















  • 1





    Please share your latest code attempt; this would help us in pointing you to right direction.

    – Madhur Bhaiya
    Nov 23 '18 at 5:18











  • now may you can understand!

    – Rabib
    Nov 23 '18 at 5:25






  • 2





    SELECT * FROM answer JOIN message ON message.id= answer.id WHERE message='$inp'" Any luck with this.

    – Prashant Deshmukh.....
    Nov 23 '18 at 5:25








  • 1





    Always join using the appropriate relationships. As specified in previous comment, you should using id not message/answer

    – Madhur Bhaiya
    Nov 23 '18 at 5:28






  • 1





    Also, Your code is open to SQL injection related attacks. Please learn to use Prepared Statements

    – Madhur Bhaiya
    Nov 23 '18 at 5:28














1












1








1


1






I have 2 table message and answer. where message id = answer id. I want select using message. when message = hi then expected answer hello, hey.



Message Table:
| id | message |
| 1 | hi |
| 2 | hi |
| 3 | Hi |


and answer table:



| id | answer |
| 1 | hello |
| 2 | hey |
| 3 | Hello |


I already tried:
$inp = "hi";



SELECT * FROM message JOIN answer ON message.message = answer.answer WHERE message='$inp'"


any suggestions?










share|improve this question
















I have 2 table message and answer. where message id = answer id. I want select using message. when message = hi then expected answer hello, hey.



Message Table:
| id | message |
| 1 | hi |
| 2 | hi |
| 3 | Hi |


and answer table:



| id | answer |
| 1 | hello |
| 2 | hey |
| 3 | Hello |


I already tried:
$inp = "hi";



SELECT * FROM message JOIN answer ON message.message = answer.answer WHERE message='$inp'"


any suggestions?







php mysql mysqli






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:24







Rabib

















asked Nov 23 '18 at 5:16









RabibRabib

188




188








  • 1





    Please share your latest code attempt; this would help us in pointing you to right direction.

    – Madhur Bhaiya
    Nov 23 '18 at 5:18











  • now may you can understand!

    – Rabib
    Nov 23 '18 at 5:25






  • 2





    SELECT * FROM answer JOIN message ON message.id= answer.id WHERE message='$inp'" Any luck with this.

    – Prashant Deshmukh.....
    Nov 23 '18 at 5:25








  • 1





    Always join using the appropriate relationships. As specified in previous comment, you should using id not message/answer

    – Madhur Bhaiya
    Nov 23 '18 at 5:28






  • 1





    Also, Your code is open to SQL injection related attacks. Please learn to use Prepared Statements

    – Madhur Bhaiya
    Nov 23 '18 at 5:28














  • 1





    Please share your latest code attempt; this would help us in pointing you to right direction.

    – Madhur Bhaiya
    Nov 23 '18 at 5:18











  • now may you can understand!

    – Rabib
    Nov 23 '18 at 5:25






  • 2





    SELECT * FROM answer JOIN message ON message.id= answer.id WHERE message='$inp'" Any luck with this.

    – Prashant Deshmukh.....
    Nov 23 '18 at 5:25








  • 1





    Always join using the appropriate relationships. As specified in previous comment, you should using id not message/answer

    – Madhur Bhaiya
    Nov 23 '18 at 5:28






  • 1





    Also, Your code is open to SQL injection related attacks. Please learn to use Prepared Statements

    – Madhur Bhaiya
    Nov 23 '18 at 5:28








1




1





Please share your latest code attempt; this would help us in pointing you to right direction.

– Madhur Bhaiya
Nov 23 '18 at 5:18





Please share your latest code attempt; this would help us in pointing you to right direction.

– Madhur Bhaiya
Nov 23 '18 at 5:18













now may you can understand!

– Rabib
Nov 23 '18 at 5:25





now may you can understand!

– Rabib
Nov 23 '18 at 5:25




2




2





SELECT * FROM answer JOIN message ON message.id= answer.id WHERE message='$inp'" Any luck with this.

– Prashant Deshmukh.....
Nov 23 '18 at 5:25







SELECT * FROM answer JOIN message ON message.id= answer.id WHERE message='$inp'" Any luck with this.

– Prashant Deshmukh.....
Nov 23 '18 at 5:25






1




1





Always join using the appropriate relationships. As specified in previous comment, you should using id not message/answer

– Madhur Bhaiya
Nov 23 '18 at 5:28





Always join using the appropriate relationships. As specified in previous comment, you should using id not message/answer

– Madhur Bhaiya
Nov 23 '18 at 5:28




1




1





Also, Your code is open to SQL injection related attacks. Please learn to use Prepared Statements

– Madhur Bhaiya
Nov 23 '18 at 5:28





Also, Your code is open to SQL injection related attacks. Please learn to use Prepared Statements

– Madhur Bhaiya
Nov 23 '18 at 5:28












2 Answers
2






active

oldest

votes


















3














Try this code :



"SELECT * FROM message JOIN answer ON message.id= answer.id WHERE message.message='".$inp."'";





share|improve this answer
























  • same output just 1 row. I want 2 row . thank you

    – Rabib
    Nov 23 '18 at 8:52



















1














use common b/w each table in your case id so query should be..



SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = 'hi';



rest in php you should use different ways to run query like prepared statements in order to avoid sql injection or concatenate the variables



like



'SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = "'.$input.'"';



or mysqli_prepare() bind_params()



or learn using PDO which is good to avoid sql injections as well as supports multiple db drivers which make your work more dynamic and flexible.






share|improve this answer


























  • thank you for your answer. but my expected output single time 2 results hello and hey. how can do this? any suggestions?

    – Rabib
    Nov 23 '18 at 8:58






  • 1





    it is giving you two answers for the message 'hi' isn't it ?? so all you need to do is loop through it using php.

    – Akhilesh
    Nov 23 '18 at 9:06











  • haha my code is working. but I forgotten to add while loop. thank you again

    – Rabib
    Nov 23 '18 at 10:08











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440969%2fphp-mysql-select-all-matching-rows-ans-echo-another-table-rows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Try this code :



"SELECT * FROM message JOIN answer ON message.id= answer.id WHERE message.message='".$inp."'";





share|improve this answer
























  • same output just 1 row. I want 2 row . thank you

    – Rabib
    Nov 23 '18 at 8:52
















3














Try this code :



"SELECT * FROM message JOIN answer ON message.id= answer.id WHERE message.message='".$inp."'";





share|improve this answer
























  • same output just 1 row. I want 2 row . thank you

    – Rabib
    Nov 23 '18 at 8:52














3












3








3







Try this code :



"SELECT * FROM message JOIN answer ON message.id= answer.id WHERE message.message='".$inp."'";





share|improve this answer













Try this code :



"SELECT * FROM message JOIN answer ON message.id= answer.id WHERE message.message='".$inp."'";






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 6:48









Bhoomi patelBhoomi patel

515220




515220













  • same output just 1 row. I want 2 row . thank you

    – Rabib
    Nov 23 '18 at 8:52



















  • same output just 1 row. I want 2 row . thank you

    – Rabib
    Nov 23 '18 at 8:52

















same output just 1 row. I want 2 row . thank you

– Rabib
Nov 23 '18 at 8:52





same output just 1 row. I want 2 row . thank you

– Rabib
Nov 23 '18 at 8:52













1














use common b/w each table in your case id so query should be..



SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = 'hi';



rest in php you should use different ways to run query like prepared statements in order to avoid sql injection or concatenate the variables



like



'SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = "'.$input.'"';



or mysqli_prepare() bind_params()



or learn using PDO which is good to avoid sql injections as well as supports multiple db drivers which make your work more dynamic and flexible.






share|improve this answer


























  • thank you for your answer. but my expected output single time 2 results hello and hey. how can do this? any suggestions?

    – Rabib
    Nov 23 '18 at 8:58






  • 1





    it is giving you two answers for the message 'hi' isn't it ?? so all you need to do is loop through it using php.

    – Akhilesh
    Nov 23 '18 at 9:06











  • haha my code is working. but I forgotten to add while loop. thank you again

    – Rabib
    Nov 23 '18 at 10:08
















1














use common b/w each table in your case id so query should be..



SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = 'hi';



rest in php you should use different ways to run query like prepared statements in order to avoid sql injection or concatenate the variables



like



'SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = "'.$input.'"';



or mysqli_prepare() bind_params()



or learn using PDO which is good to avoid sql injections as well as supports multiple db drivers which make your work more dynamic and flexible.






share|improve this answer


























  • thank you for your answer. but my expected output single time 2 results hello and hey. how can do this? any suggestions?

    – Rabib
    Nov 23 '18 at 8:58






  • 1





    it is giving you two answers for the message 'hi' isn't it ?? so all you need to do is loop through it using php.

    – Akhilesh
    Nov 23 '18 at 9:06











  • haha my code is working. but I forgotten to add while loop. thank you again

    – Rabib
    Nov 23 '18 at 10:08














1












1








1







use common b/w each table in your case id so query should be..



SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = 'hi';



rest in php you should use different ways to run query like prepared statements in order to avoid sql injection or concatenate the variables



like



'SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = "'.$input.'"';



or mysqli_prepare() bind_params()



or learn using PDO which is good to avoid sql injections as well as supports multiple db drivers which make your work more dynamic and flexible.






share|improve this answer















use common b/w each table in your case id so query should be..



SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = 'hi';



rest in php you should use different ways to run query like prepared statements in order to avoid sql injection or concatenate the variables



like



'SELECT * FROM message JOIN answer ON answer.id = message.id WHERE message = "'.$input.'"';



or mysqli_prepare() bind_params()



or learn using PDO which is good to avoid sql injections as well as supports multiple db drivers which make your work more dynamic and flexible.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 6:47

























answered Nov 23 '18 at 6:42









AkhileshAkhilesh

626




626













  • thank you for your answer. but my expected output single time 2 results hello and hey. how can do this? any suggestions?

    – Rabib
    Nov 23 '18 at 8:58






  • 1





    it is giving you two answers for the message 'hi' isn't it ?? so all you need to do is loop through it using php.

    – Akhilesh
    Nov 23 '18 at 9:06











  • haha my code is working. but I forgotten to add while loop. thank you again

    – Rabib
    Nov 23 '18 at 10:08



















  • thank you for your answer. but my expected output single time 2 results hello and hey. how can do this? any suggestions?

    – Rabib
    Nov 23 '18 at 8:58






  • 1





    it is giving you two answers for the message 'hi' isn't it ?? so all you need to do is loop through it using php.

    – Akhilesh
    Nov 23 '18 at 9:06











  • haha my code is working. but I forgotten to add while loop. thank you again

    – Rabib
    Nov 23 '18 at 10:08

















thank you for your answer. but my expected output single time 2 results hello and hey. how can do this? any suggestions?

– Rabib
Nov 23 '18 at 8:58





thank you for your answer. but my expected output single time 2 results hello and hey. how can do this? any suggestions?

– Rabib
Nov 23 '18 at 8:58




1




1





it is giving you two answers for the message 'hi' isn't it ?? so all you need to do is loop through it using php.

– Akhilesh
Nov 23 '18 at 9:06





it is giving you two answers for the message 'hi' isn't it ?? so all you need to do is loop through it using php.

– Akhilesh
Nov 23 '18 at 9:06













haha my code is working. but I forgotten to add while loop. thank you again

– Rabib
Nov 23 '18 at 10:08





haha my code is working. but I forgotten to add while loop. thank you again

– Rabib
Nov 23 '18 at 10:08


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440969%2fphp-mysql-select-all-matching-rows-ans-echo-another-table-rows%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud