+ 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?

2nd Oct 2017, 5:31 AM
Ondape Valery
Ondape Valery - avatar
24 Antworten
+ 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 }
2nd Oct 2017, 6:26 AM
ChaoticDawg
ChaoticDawg - avatar
+ 7
So useful, thx 👍
2nd Oct 2017, 8:12 AM
Bohdan Sakovych
Bohdan Sakovych - avatar
+ 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.
5th Oct 2017, 7:13 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Use mysqli_fetch_all() for functional programing or Mysqli::fetch_all() for OOP Both return multidimentional array.
3rd Oct 2017, 3:23 PM
Freezemage
Freezemage - avatar
+ 1
Haha. I prefer JavaScript though I wish it was possible with php
5th Oct 2017, 6:37 PM
Ondape Valery
Ondape Valery - avatar
+ 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.
5th Oct 2017, 8:07 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Most likely yes, but I haven't seen all of your page, so it's a bit subjective.
5th Oct 2017, 8:42 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
okay sir @Chaotic. Thank you very much. lemme try it
5th Oct 2017, 8:45 PM
Ondape Valery
Ondape Valery - avatar
+ 1
So, here you go. Comment in code if you have questions. https://code.sololearn.com/wuYve604LnMb/?ref=app
5th Oct 2017, 8:56 PM
Freezemage
Freezemage - avatar
+ 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
5th Oct 2017, 8:58 PM
Ondape Valery
Ondape Valery - avatar
0
I'll explain if you want.
3rd Oct 2017, 3:31 PM
Freezemage
Freezemage - avatar
0
Thanks very much for the help sir @Chaotic and sir @Freeze. it really helped.
5th Oct 2017, 6:02 AM
Ondape Valery
Ondape Valery - avatar
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
5th Oct 2017, 5:45 PM
Ondape Valery
Ondape Valery - avatar
0
There are many answers for this situation: Custom API, JavaScript's functions, sleep(), ob_implicit_start(), etc.
5th Oct 2017, 6:30 PM
Freezemage
Freezemage - avatar
0
hmm thanks @Freeze. Seems like I still have a lot to learn..😀
5th Oct 2017, 6:34 PM
Ondape Valery
Ondape Valery - avatar
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.
5th Oct 2017, 7:19 PM
Ondape Valery
Ondape Valery - avatar
0
Thanks @Freeze. I will be waiting.
5th Oct 2017, 8:02 PM
Ondape Valery
Ondape Valery - avatar
0
Thanks @chaotic. should I place the while loop inside the if isset condition?
5th Oct 2017, 8:37 PM
Ondape Valery
Ondape Valery - avatar
0
@Freeze, I prefer the functional one. And I wish the page reloads after submission
5th Oct 2017, 8:37 PM
Ondape Valery
Ondape Valery - avatar
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 }?>
5th Oct 2017, 8:51 PM
Ondape Valery
Ondape Valery - avatar