mysqli_query does not return result
I code php to get returned from mysql.
My query works in mysql workbench, however it doesn't work in php.
sql below is returned well in mysql workbench.
"select * from players_info where name = XXXX and sex = X and season = xxxx and tName = XXXX"
My php is below
function player_info($conn, $name, $season, $sex, $tName){
$output = '';
$sql = "SELECT *
FROM players_info
WHERE name ='".$name."' and sex ='".$sex."'
and season = '".$season."' and tName = '".$tName."'";
$result = mysqli_query($conn, $sql);
if($result -> num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
}
} else {
echo "no result!";
}
$conn -> close();
return $output;
}
HTML is below.
<div id = "player_page3">
<?php
echo player_info($conn, $_GET["name"], $_GET["season"], $_GET["sex"], $_GET["tName"]);
?>
</div>
I think my code returns 0 result even though sql is correct.
What is the problem on my code?
UPDATE :
I added code shown below after $sql.
I think it returns nothing.
result is "Error description : no result!"
'no result!' came from the else statement in my code.
if (!mysqli_query($conn, $sql)){
echo("Error description : ". mysqli_error($conn));
}
php mysql function get
|
show 3 more comments
I code php to get returned from mysql.
My query works in mysql workbench, however it doesn't work in php.
sql below is returned well in mysql workbench.
"select * from players_info where name = XXXX and sex = X and season = xxxx and tName = XXXX"
My php is below
function player_info($conn, $name, $season, $sex, $tName){
$output = '';
$sql = "SELECT *
FROM players_info
WHERE name ='".$name."' and sex ='".$sex."'
and season = '".$season."' and tName = '".$tName."'";
$result = mysqli_query($conn, $sql);
if($result -> num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
}
} else {
echo "no result!";
}
$conn -> close();
return $output;
}
HTML is below.
<div id = "player_page3">
<?php
echo player_info($conn, $_GET["name"], $_GET["season"], $_GET["sex"], $_GET["tName"]);
?>
</div>
I think my code returns 0 result even though sql is correct.
What is the problem on my code?
UPDATE :
I added code shown below after $sql.
I think it returns nothing.
result is "Error description : no result!"
'no result!' came from the else statement in my code.
if (!mysqli_query($conn, $sql)){
echo("Error description : ". mysqli_error($conn));
}
php mysql function get
You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Usemysqli_error()
to get a detailed error message from the database.
– John Conde
Jan 1 at 15:30
5
Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared parameterized statements.
– John Conde
Jan 1 at 15:30
why dont you use the mysqli_error like this . $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); you wil know the error.
– Anupam Rekha
Jan 1 at 15:33
Where do you create the$conn
? and how?
– dWinder
Jan 1 at 15:35
No idea where and how you're using the GET arrays and the return.
– Funk Forty Niner
Jan 1 at 15:38
|
show 3 more comments
I code php to get returned from mysql.
My query works in mysql workbench, however it doesn't work in php.
sql below is returned well in mysql workbench.
"select * from players_info where name = XXXX and sex = X and season = xxxx and tName = XXXX"
My php is below
function player_info($conn, $name, $season, $sex, $tName){
$output = '';
$sql = "SELECT *
FROM players_info
WHERE name ='".$name."' and sex ='".$sex."'
and season = '".$season."' and tName = '".$tName."'";
$result = mysqli_query($conn, $sql);
if($result -> num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
}
} else {
echo "no result!";
}
$conn -> close();
return $output;
}
HTML is below.
<div id = "player_page3">
<?php
echo player_info($conn, $_GET["name"], $_GET["season"], $_GET["sex"], $_GET["tName"]);
?>
</div>
I think my code returns 0 result even though sql is correct.
What is the problem on my code?
UPDATE :
I added code shown below after $sql.
I think it returns nothing.
result is "Error description : no result!"
'no result!' came from the else statement in my code.
if (!mysqli_query($conn, $sql)){
echo("Error description : ". mysqli_error($conn));
}
php mysql function get
I code php to get returned from mysql.
My query works in mysql workbench, however it doesn't work in php.
sql below is returned well in mysql workbench.
"select * from players_info where name = XXXX and sex = X and season = xxxx and tName = XXXX"
My php is below
function player_info($conn, $name, $season, $sex, $tName){
$output = '';
$sql = "SELECT *
FROM players_info
WHERE name ='".$name."' and sex ='".$sex."'
and season = '".$season."' and tName = '".$tName."'";
$result = mysqli_query($conn, $sql);
if($result -> num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
}
} else {
echo "no result!";
}
$conn -> close();
return $output;
}
HTML is below.
<div id = "player_page3">
<?php
echo player_info($conn, $_GET["name"], $_GET["season"], $_GET["sex"], $_GET["tName"]);
?>
</div>
I think my code returns 0 result even though sql is correct.
What is the problem on my code?
UPDATE :
I added code shown below after $sql.
I think it returns nothing.
result is "Error description : no result!"
'no result!' came from the else statement in my code.
if (!mysqli_query($conn, $sql)){
echo("Error description : ". mysqli_error($conn));
}
php mysql function get
php mysql function get
edited Jan 1 at 16:29
sk Jin
asked Jan 1 at 15:27
sk Jinsk Jin
205
205
You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Usemysqli_error()
to get a detailed error message from the database.
– John Conde
Jan 1 at 15:30
5
Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared parameterized statements.
– John Conde
Jan 1 at 15:30
why dont you use the mysqli_error like this . $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); you wil know the error.
– Anupam Rekha
Jan 1 at 15:33
Where do you create the$conn
? and how?
– dWinder
Jan 1 at 15:35
No idea where and how you're using the GET arrays and the return.
– Funk Forty Niner
Jan 1 at 15:38
|
show 3 more comments
You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Usemysqli_error()
to get a detailed error message from the database.
– John Conde
Jan 1 at 15:30
5
Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared parameterized statements.
– John Conde
Jan 1 at 15:30
why dont you use the mysqli_error like this . $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); you wil know the error.
– Anupam Rekha
Jan 1 at 15:33
Where do you create the$conn
? and how?
– dWinder
Jan 1 at 15:35
No idea where and how you're using the GET arrays and the return.
– Funk Forty Niner
Jan 1 at 15:38
You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use
mysqli_error()
to get a detailed error message from the database.– John Conde
Jan 1 at 15:30
You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use
mysqli_error()
to get a detailed error message from the database.– John Conde
Jan 1 at 15:30
5
5
Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared parameterized statements.
– John Conde
Jan 1 at 15:30
Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared parameterized statements.
– John Conde
Jan 1 at 15:30
why dont you use the mysqli_error like this . $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); you wil know the error.
– Anupam Rekha
Jan 1 at 15:33
why dont you use the mysqli_error like this . $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); you wil know the error.
– Anupam Rekha
Jan 1 at 15:33
Where do you create the
$conn
? and how?– dWinder
Jan 1 at 15:35
Where do you create the
$conn
? and how?– dWinder
Jan 1 at 15:35
No idea where and how you're using the GET arrays and the return.
– Funk Forty Niner
Jan 1 at 15:38
No idea where and how you're using the GET arrays and the return.
– Funk Forty Niner
Jan 1 at 15:38
|
show 3 more comments
1 Answer
1
active
oldest
votes
First of all - as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects.
Here are some points to note:
Your $output
variable is not collecting the result because it is overwriting itself over the loop. You need to change that to .=
$output .= $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
.=
concatenates new text to the end.
UPDATE
If you want a single row you need to remove the while loop
if($result -> num_rows > 0){
$row = mysqli_fetch_assoc($result)
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
} else {
echo "no result!";
}
Actually, the result is one row. however I don't know how to return just one row. So I used like that. I mean that is not a big problem so far. How can I change if I hope to result just one row?
– sk Jin
Jan 1 at 15:55
Check the update
– nazim
Jan 1 at 15:59
and BTW as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects
– nazim
Jan 1 at 16:00
add a comment |
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%2f53996653%2fmysqli-query-does-not-return-result%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
First of all - as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects.
Here are some points to note:
Your $output
variable is not collecting the result because it is overwriting itself over the loop. You need to change that to .=
$output .= $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
.=
concatenates new text to the end.
UPDATE
If you want a single row you need to remove the while loop
if($result -> num_rows > 0){
$row = mysqli_fetch_assoc($result)
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
} else {
echo "no result!";
}
Actually, the result is one row. however I don't know how to return just one row. So I used like that. I mean that is not a big problem so far. How can I change if I hope to result just one row?
– sk Jin
Jan 1 at 15:55
Check the update
– nazim
Jan 1 at 15:59
and BTW as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects
– nazim
Jan 1 at 16:00
add a comment |
First of all - as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects.
Here are some points to note:
Your $output
variable is not collecting the result because it is overwriting itself over the loop. You need to change that to .=
$output .= $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
.=
concatenates new text to the end.
UPDATE
If you want a single row you need to remove the while loop
if($result -> num_rows > 0){
$row = mysqli_fetch_assoc($result)
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
} else {
echo "no result!";
}
Actually, the result is one row. however I don't know how to return just one row. So I used like that. I mean that is not a big problem so far. How can I change if I hope to result just one row?
– sk Jin
Jan 1 at 15:55
Check the update
– nazim
Jan 1 at 15:59
and BTW as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects
– nazim
Jan 1 at 16:00
add a comment |
First of all - as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects.
Here are some points to note:
Your $output
variable is not collecting the result because it is overwriting itself over the loop. You need to change that to .=
$output .= $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
.=
concatenates new text to the end.
UPDATE
If you want a single row you need to remove the while loop
if($result -> num_rows > 0){
$row = mysqli_fetch_assoc($result)
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
} else {
echo "no result!";
}
First of all - as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects.
Here are some points to note:
Your $output
variable is not collecting the result because it is overwriting itself over the loop. You need to change that to .=
$output .= $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
.=
concatenates new text to the end.
UPDATE
If you want a single row you need to remove the while loop
if($result -> num_rows > 0){
$row = mysqli_fetch_assoc($result)
$output = $row['name']."n"
."No : ".$row['No']."n"
."Pos : ".$row['pos']."n"
."Height(m) / Weight(kg) : ".$row['height']. " / ".$row['weight']."n"
."Born : ".$row['month']." ".$row['day'].", ".$row['year']."n"
."Shoots / Catches : ".$row['shoots']."n"
."Club : ".$row['team'];
} else {
echo "no result!";
}
edited Jan 1 at 16:04
answered Jan 1 at 15:40
nazimnazim
8831023
8831023
Actually, the result is one row. however I don't know how to return just one row. So I used like that. I mean that is not a big problem so far. How can I change if I hope to result just one row?
– sk Jin
Jan 1 at 15:55
Check the update
– nazim
Jan 1 at 15:59
and BTW as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects
– nazim
Jan 1 at 16:00
add a comment |
Actually, the result is one row. however I don't know how to return just one row. So I used like that. I mean that is not a big problem so far. How can I change if I hope to result just one row?
– sk Jin
Jan 1 at 15:55
Check the update
– nazim
Jan 1 at 15:59
and BTW as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects
– nazim
Jan 1 at 16:00
Actually, the result is one row. however I don't know how to return just one row. So I used like that. I mean that is not a big problem so far. How can I change if I hope to result just one row?
– sk Jin
Jan 1 at 15:55
Actually, the result is one row. however I don't know how to return just one row. So I used like that. I mean that is not a big problem so far. How can I change if I hope to result just one row?
– sk Jin
Jan 1 at 15:55
Check the update
– nazim
Jan 1 at 15:59
Check the update
– nazim
Jan 1 at 15:59
and BTW as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects
– nazim
Jan 1 at 16:00
and BTW as alerted in the comments you need to learn to prepare your sql statements. Use PDO - Your current script IS VULNERABLE to sql injects
– nazim
Jan 1 at 16:00
add a comment |
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%2f53996653%2fmysqli-query-does-not-return-result%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
You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use
mysqli_error()
to get a detailed error message from the database.– John Conde
Jan 1 at 15:30
5
Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared parameterized statements.
– John Conde
Jan 1 at 15:30
why dont you use the mysqli_error like this . $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); you wil know the error.
– Anupam Rekha
Jan 1 at 15:33
Where do you create the
$conn
? and how?– dWinder
Jan 1 at 15:35
No idea where and how you're using the GET arrays and the return.
– Funk Forty Niner
Jan 1 at 15:38