2 Replies - 207 Views - Last Post: 24 June 2020 - 05:30 AM Rate Topic: -----

#1 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 20-January 19

MYSQLI Prepared SELECT query no results

Posted 21 June 2020 - 08:01 AM

After hours of trying to find a solution myself, I resulted in posting here. The query returns no errors nor displays any data which exists in the database. Upon submitting a form with a ref number, it should display the record matching it. Am I missing something very silly?!


$servername = "xxx";
$username = "xxxx";
$password = "xxx";
$dbname = "xxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$ref = trim(($_POST['ref']));

$count=1;


$sel_query="SELECT ref, sname, fname, B1 FROM Enrol where ref =?";
$stmt = $conn->prepare($sel_query); 



$stmt->bind_param("s", $ref);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$user = $result->fetch_assoc(); // fetch data   
if($result->num_rows === 0) exit('No results!');
while($row = $result->fetch_assoc()) {  ?>

<tr>
<td><?php echo $count; ?></td>
<td><?php echo $row["ref"]; ?></td>
<td><?php echo $row["sname"]; ?></td>
<td><?php echo $row["fname"]; ?></td>
<td><?php echo $row["B1l"]; ?></td>


</tr>

<?php $count++; } ?>


</table>

</body>

<?php
$stmt->close();
?>



Is This A Good Question/Topic? 0
  • +

Replies To: MYSQLI Prepared SELECT query no results

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: MYSQLI Prepared SELECT query no results

Posted 21 June 2020 - 10:01 AM

Well, start from the top. Make sure that $_POST['ref'] has a value. If it doesn't, then the query is going to be ref = '' which probably gives you no results. If that looks good, var_dump() your $result variable and see if it has a result. This will tell you if anything came back from the SELECT. If nothing did, are you able to look at the database through another tool like MySQL workbench? See if there are any records in there and what their values are for that table. Maybe you are assuming something is there, but it is empty or simply doesn't match.

After running those two tests, you should have a better idea of what is going on. You can post your results here and we can help further if needed.
Was This Post Helpful? 1
  • +
  • -

#3 nicephotog   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 36
  • Joined: 06-June 20

Re: MYSQLI Prepared SELECT query no results

Posted 24 June 2020 - 05:30 AM

drop $dbname from it in the call.
use mysqli_select_db($conn,$dbname) toselect the database.
then
$resultset = mysqli_query($conn,$sel_query) ;

However using object syntax for $conn using dash greater than object referencing creation should look more like $conn = new mysqli($servername, $username, $password)
then its
$conn->

// the following should be replaced with procedural calls not object notation calls
$stmt = $conn->prepare($sel_query);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1