0

So had this code for course catalog search field:

 if (isset($_POST['submit']))
    $search = $_POST['search'];
    $byNameOrCode = mysqli_query($link, "SELECT course_name, course_code, ects_credits FROM courses WHERE course_name LIKE '%$search%' OR course_code LIKE '%$search%'");

it worked perfectly but had to change to parameter binding. so I switched last line with:

$byNameOrCode = mysqli_stmt_prepare($link, "SELECT  course_name, course_code, ects_credits FROM courses WHERE course_name LIKE ? OR course_code LIKE ?");
mysqli_stmt_bind_param($byNameOrCode, "ss", "%" . $search . "%", "%" . $search . "%");
    mysqli_stmt_execute($byNameOrCode);
    mysqli_stmt_bind_result($byNameOrCode, $course_code, $course_name, $ects_credits, $semester_name);

It crashed the whole page. Then I did:

if (isset($_POST['submit']))
    $search = '%' . $_POST['search'] . '%';
    mysqli_stmt_bind_param($byNameOrCode, "ss", $search, $search);
    mysqli_stmt_execute($byNameOrCode);
    mysqli_stmt_bind_result($byNameOrCode, $course_code, $course_name, $ects_credits, $semester_name);

the page is okay but it does not seem to search anything. anyone has any idea how to fix it?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • You need to make your question more clear. The problem you are seeing is solved by the answer I gave. If you have a different problem then please describe it properly – Dharman Apr 26 '21 at 13:38

1 Answers1

-1

Check the working code below.

$con = mysqli_connect("localhost", "root", "", "test");
$link = mysqli_stmt_init($con);

$search = '%ab%';

mysqli_stmt_prepare($link, "SELECT  course_name, course_code, ects_credits FROM courses WHERE course_name LIKE ? OR course_code LIKE ?");
mysqli_stmt_bind_param($link, "ss", $search, $search);
mysqli_stmt_execute($link);
mysqli_stmt_bind_result($link, $course_code, $course_name, $ects_credits);

while (mysqli_stmt_fetch($link)) {
 print("course_code: ".$course_code."\n");
 print("course_name: ".$course_name."\n");
 print("ects_credits: ".$ects_credits."\n");
 print("\n");
}
  • does not search anything :( – peterthegreat1853 Apr 26 '21 at 12:09
  • It worked perfectly fine at my end. Could you share screenshot what response you are getting? Also, hope you would have changed the search value basis the data available in you db table - courses. Otherwise it will definitely not search anything :) – Rajesh Kumar May 04 '21 at 18:35