1

MY html form :

<form action="" method="post" > 
<tr> 
<td><select name="bs_edu">
<option value="" selected="selected">Select Basic Education</option>   
<option value="BA">B.A</option>
<option value="BARCH">B.ARCH</option>
<option value="BCA">BCA</option>
<option value="BBA">BBA</option> 
</select>
<td><input type="text" name="desg"/> </td>
<td><input type="email" name="email"/></td>
</tr>
</form>

My Query:

<?php
    $stmt = $con->prepare("SELECT * FROM user_tbl WHERE bs_edu = ? AND desg = ? AND exp = ? AND pr_sal = ? AND plc = ? AND email = ?  ");
    $stmt->bind_param('ssssss', $_POST['bs_edu'], $_POST['desg'], $_POST['exp'], $_POST['prsal'], $_POST['place'], $_POST['email']);
    $stmt->execute();
    $result = $stmt->get_result();
    if ($result->num_rows > "0")
    {
        while($row = $result->fetch_assoc());
        {
            echo 'first name: ' .$row['fname'];  
        }
    }
    $stmt->close(); 
?>

I know this the wrong way to show the data. Going through this manual http://php.net/manual/en/mysqli-stmt.fetch.php n really having bad luck, show how can i show the fetched data .. any help here.. thanks

PRashant PUrohit
  • 380
  • 1
  • 3
  • 21
Pooojaaaa
  • 183
  • 1
  • 4
  • 16
  • why do you think it's wrong? – DevMan Mar 14 '16 at 11:47
  • bcz i dont think its the proper way n m not getting any result from this one – Pooojaaaa Mar 14 '16 at 12:08
  • the problem is the ; just after while loop; – itzmukeshy7 Mar 14 '16 at 12:49
  • Two form fields ($_POST['exp'], $_POST['prsal']) are also missing in form; – itzmukeshy7 Mar 14 '16 at 12:57
  • 1
    @itzmukeshy7 Your pending edit http://stackoverflow.com/review/suggested-edits/11623946 you are modifying OP's "code" here from `if ($result->num_rows > "0")` to `if ($result->num_rows){`, *why??* Please DON'T do that! You're not supposed to modify "code". I rejected the edit. If it does pass, I will do a rollback to it. – Funk Forty Niner Mar 14 '16 at 12:58
  • @MS. why did you approve this edit? http://stackoverflow.com/review/suggested-edits/11623946 can't you see this guy is **modifying** OP's code from `if ($result->num_rows > "0")` to `if ($result->num_rows){`?? FFS. I don't get you reviewers, really. – Funk Forty Niner Mar 14 '16 at 13:07
  • @itzmukeshy7 You can be sure that I will be flagging this question with both your names in there ([MS.](http://stackoverflow.com/users/1323519/ms)). You'll think twice the next time you guys decide to change code. – Funk Forty Niner Mar 14 '16 at 13:09
  • @Fred-ii- tell me how to revert change? – itzmukeshy7 Mar 14 '16 at 13:11
  • @itzmukeshy7 Simple, you can't and it needs to go through the rest of edit reviews. Lovely. – Funk Forty Niner Mar 14 '16 at 13:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106241/discussion-between-itzmukeshy7-and-fred-ii). – itzmukeshy7 Mar 14 '16 at 13:13
  • guys i ve been trying everything but no ans.. its only triggering the elese part `0 records found` – Pooojaaaa Mar 15 '16 at 05:27

1 Answers1

0

The semi-colon in the following is making your loop stop and is an end of statement character.

while($row = $result->fetch_assoc());
                                    ^ end of statement

Remove it.

while($row = $result->fetch_assoc())

You wouldn't have gotten an error about it, since it's considered as valid syntax.

Also the quoted zero is interpreted as a string, so you need to remove it.

if ($result->num_rows > 0)

Sidenote about get_result().

The manual http://php.net/manual/en/mysqli-stmt.get-result.php states:

MySQL Native Driver Only

Available only with mysqlnd.

Example pulled from this answer https://stackoverflow.com/a/24985955/

if ($stmt = $mysqli->prepare("SELECT id, community, map, image FROM `googleMaps`")) {
    $stmt->execute();
    $stmt->bind_result($id, $community, $map, $image);
    while ($stmt->fetch()) {
            printf("%s %s\n", $title);
    }
    $stmt->close();
}

Additional notes.

<form> cannot be child of <table> since I suspect you may have <table></table> tags hidden somewhere in code you did not show.

You also don't have form elements to match your POST arrays.

Add error reporting to the top of your file(s) right after your opening PHP tag for example
<?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything, as well as or die(mysqli_error($con)) to mysqli_query().

Or another method would be to change:

$stmt->execute();

to:

if(!$stmt->execute()){
   trigger_error("There was an error.... ".$con->error, E_USER_WARNING);
}

Edit:

Borrowed from this answer https://stackoverflow.com/a/22899155/

$stmt = $con->prepare("SELECT * FROM user_tbl ... ");

$result = $stmt->execute(); 
$stmt->store_result();

if ($stmt->num_rows > 1) {

  while($row = $result->fetch_assoc()){ 

  echo 'First name: ' .$row['fname'];

  }

}else{

   echo "0 records found";
}

Plus, you also seem to be using your HTML form and PHP/MySQL in the same file.

You need to use isset() or !empty() against your POST arrays.

You also didn't include a submit button and is unknown if you are using one.

Same for what I already stated about missing form elements with matching name attributes for:

  • $_POST['exp']
  • $_POST['prsal']
  • $_POST['place']
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • but i m not getting any output.. i removed the semi colon – Pooojaaaa Mar 14 '16 at 12:05
  • @StillLearning `get_result()` as I stated in my answer is a MySQL native driver only and you may not have it installed on your server. This answer is an alternative http://stackoverflow.com/a/24985955/ (see the 2nd method). – Funk Forty Niner Mar 14 '16 at 12:09
  • @StillLearning You did also see the removed quotes `if ($result->num_rows > 0)` right? – Funk Forty Niner Mar 14 '16 at 12:12
  • @StillLearning Ok... well, your HTML form does not have name attributes for the POST arrays, so I don't know where you plan on using those. Use error reporting http://php.net/manual/en/function.error-reporting.php and check for errors on the query http://php.net/manual/en/mysqli.error.php as added to my answer – Funk Forty Niner Mar 14 '16 at 12:15
  • @StillLearning Reload my answer as I've made a few edits. See also under **Edit:** which has another slightly different method. I also added methods to check for errors, so you need to check for that as stated about potentially missing form elements/name attributes and that `
    ` cannot be "child" of `` and is unshown in your question. That's the best I can do here.
    – Funk Forty Niner Mar 14 '16 at 12:39
  • @StillLearning It's been a while now and haven't heard from you. Any progress? Errors? – Funk Forty Niner Mar 14 '16 at 13:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106309/discussion-between-stilllearning-and-fred-ii). – Pooojaaaa Mar 15 '16 at 05:06