+ 2
How can i make an sql search query which can return more than one result? Like searching a name on facebook?
Hello Sirs. I have a database with names and email. I run scripts from a php page to echo results from SQL to the browser. this is the code $sql="select* from login where username like '%$username%'"; When I run it through php I get only one result using mysqli_fetch_array. But there are over two names that begins with "ca" in the database. How can I fix that?
24 Réponses
+ 5
Given a table named login with columns named username and email using MySQL with PHP:
$db = mysqli_connect($servername, $username, $password, $database);
if (!$db) {
die('Could not connect to MySQL: ' . mysqli_connect_error());
}
$sql = 'SELECT * FROM login WHERE username LIKE "%'.$username.'%"';
$result = mysqli_query($db, $sql);
$num_rows = mysqli_num_rows($result);
for($i = 0; $i < $num_rows; ++$i) {
$row = mysqli_fetch_array($result);
$username = $row['username'];
$email = $row['email'];
// code to work with current iteration of username and email
}
+ 7
So useful, thx 👍
+ 4
You should be able to just use php and a form submit using post method. I'll try to get an example together for you within the next couple of hours. Busy working on a car at the moment.
+ 1
Use mysqli_fetch_all() for functional programing or Mysqli::fetch_all() for OOP
Both return multidimentional array.
+ 1
Haha.
I prefer JavaScript though I wish it was possible with php
+ 1
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
// Place the code you want only to run when the form has been submitted here
// Maybe the code from my previous post
}
?>
<html>
<body>
<form action="" method="post">
<input type="text" name="query"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Note this won't run in the code playground properly due to page being resubmitted.
+ 1
Most likely yes, but I haven't seen all of your page, so it's a bit subjective.
+ 1
okay sir @Chaotic.
Thank you very much.
lemme try it
+ 1
So, here you go. Comment in code if you have questions.
https://code.sololearn.com/wuYve604LnMb/?ref=app
+ 1
Thanks very much Sirs.
It worked like charm.
I'm very happy and thankful.
This problem has been disturbing my sleep for about 2days until now
0
I'll explain if you want.
0
Thanks very much for the help sir @Chaotic and sir @Freeze.
it really helped.
0
Guys I need help..
When I have done as you all advised but when I launch the php page, it automatically outputs everything even when I have not yet pressed the search button.
though when I've pressed, it then only outputs the search result.
I wish to stop it from running when the search button has not been pressed.
Thanks very much
0
There are many answers for this situation:
Custom API, JavaScript's functions, sleep(), ob_implicit_start(), etc.
0
hmm thanks @Freeze.
Seems like I still have a lot to learn..😀
0
Thanks @ChaoticDawg.
I have written the sibmit form.
The form action is set with $_SERVER['PHP_SELF'].
But I don't know, when I open the page, results of the entire SQL in this case, I used $sql =" select* from login where username LIKE %username%"; shows. It is only after I input my search word the click the submit button that the result of the search is returned to me without the whole login table.
0
Thanks @Freeze.
I will be waiting.
0
Thanks @chaotic.
should I place the while loop inside the if isset condition?
0
@Freeze, I prefer the functional one.
And I wish the page reloads after submission
0
*/This is the code*/
<?php
require('auth.php');
require_once('connect.php');
$username=$_POST['search'];
$sql1 ="select* from login where username LIKE '%$username%'";
$show=mysqli_query($connection,$sql1);
while($show_result=mysqli_fetch_array($show)) { ?>
<div class="alert alert-success" role="alert">
<caption>Results</caption>
<table class="table table-responsive">
<tr>
<td><?php echo $show_result['username']."<br>";?></td>
<td><?php echo $show_result['email']."<br>";?></td>
<td><?php echo $show_result['password']."<br>";?></td>
</tr>
</table>
</div>
<?php }?>