PHP MySQL search db hat contains using % Wildcard -
i have looked many responses on hear in regards searching using like % , concat not working me. trying find in database words % croyden , surrey below script works fine, want use % words contain croyden , surrey. have tried various ways nothing work.
$loctown = "croydon"; $loccounty = "surrey"; $query = 'select loctown, loccounty location loctown ? , loccounty ? '; if ($stmt = mysqli_prepare($link, $query)) { mysqli_stmt_bind_param($stmt, "ss", $loctown, $loccounty); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $loctown, $loccounty); /* fetch value */ mysqli_stmt_fetch($stmt); while (mysqli_stmt_fetch($stmt)) { } echo "location: $loctown - $loccounty\n"; /* close statement */ mysqli_stmt_close($stmt); } mysqli_close($link); just clarity below works correctly after updating sql , fething result.
$query = "select loctown, loccounty location loctown concat('%', ?, '%') , loccounty concat('%', ?, '%') "; if ($stmt = mysqli_prepare($link, $query)) { mysqli_stmt_bind_param($stmt, "ss", $loctown, $loccounty); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $loctown, $loccounty); /* fetch value */ mysqli_stmt_fetch($stmt); while (mysqli_stmt_fetch($stmt)) { echo "location: $loctown - $loccounty\n"; } /* close statement */ mysqli_stmt_close($stmt); } mysqli_close($link);
since concatenating % in query didn't work op, can try concatenating % when binding alternative:
mysqli_stmt_bind_param($stmt, "ss", '%'.$loctown, '%'.$loccounty);
Comments
Post a Comment