1

i need help for my php script:

This is my index.php:

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);

$db_hostname = '127.0.0.1';
$db_username = 'root';
$db_password = 'password';
$db_database = 'mydatabase';


    $connect = mysqli_connect("$db_hostname","$db_username","$db_password");

    if (!$connect){

           die('Could not connect: ' . mysqli_error());
        
        }

    
    mysqli_select_db($connect, $db_database) or die ("could not find db");


    $output ='';



    if (isset ($_POST['search'])){

     $search = $_POST['search'];

   }


    $query = mysqli_query($connect, "SELECT * FROM user WHERE email LIKE '%$searchq%'") or die("could not search");

    $count = mysqli_num_rows($query);

    
    if($count == 0){

     $output = 'There was no search results !';

   }

    
    else{

         while($row = mysqli_fetch_array($query)){
         $fname = $row['email'];
         $output .='<div> '.$fname.'</div>';}

        }  


?>

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset=utf-8" />
      <title>Lookup</title>
  </head>
  <body>

<form action="index.php" method="post">
<input type="text" placeholder="search" name="search">
<input type="submit" value=">>"/>

</form>



<?php

print("$output"); 

?>

  </body>
</html> 

  • So the script works and its return me email from email column where my string is found.

  • But i have another question, i have another column beside my email column named location, and every email correspond to a location value. So i want to see 2 columns on my OUTPUT.

This is example an of my columns:

| email | location |

| [email protected] | Chicago |

| [email protected] | Mexico |



Thanks for your help.

1
  • $row['location'] Commented Jul 11, 2017 at 17:52

4 Answers 4

3

You're actually pretty close to what you want already. The key is in your while loop.

while($row = mysqli_fetch_array($query)){
    $fname = $row['email'];
    $loc   = $row['location'];
    $output .='<div>' . $fname . ' ' . $loc . '</div>';
}  

The $row variable will contain an entry for every column you select in your query. You're using a select * in your query, so you may or may not realize all that's in there. If you want to see the contents, you might find var_dump($row); to be helpful for testing.

Sign up to request clarification or add additional context in comments.

1 Comment

use $row['location]; as $row['location']; see single quote missing
1

Your else part should be:-

else{

     while($row = mysqli_fetch_array($query)){
     $fname_location = $row['email']."  ".$row['location'];
     $output .='<div> '.$fname_location .'</div>';}

    } 

Comments

0

Change this part:

else{

     while($row = mysqli_fetch_array($query)){
     $fname = $row['email'];
     $output .='<div> '.$fname.'</div>';}

    }  

To this:

else{

     while($row = mysqli_fetch_array($query)){
     $fname = $row['email'];
     $ulocation = $row['location '];
     $output .='<div> '.$fname . ' ' . $ulocation.'</div>';}

    }  

1 Comment

missing dot . between php variables in output variable
0

Set another variable $location to receive the location data from db just like you set the $fname

 while($row = mysqli_fetch_array($query)){
     $fname = $row['email'];
     $location = $row['location'];
     $output .='<div> '.$fname . ' ' . $location.'</div>';
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.