Jump to content

[SOLVED] will my array search the database yes no?


redarrow

Recommended Posts

I made this all up and want to no would it be possable to use this kind of

search with the arrary values?

 

<?php

$db=mysql_connect("localhost","username","password");

mysql_select_db("my_database",$db);

  if($_POST['submit']){

   $key=($_POST['key']);

    for ($i=0; $i<count($key); $i++){

       $query="select * from users where name like "%'".key[0]."'%" and age like "%'".key[1]."'%" ";
      
      $result=mysql_query($query);


while($x=mysql_fetch_assoc($result)){


  echo " Username: ".$x['name']."  Age: ".$rec['age']." ";
   
}
   }
     }

$x=array("name","age");


  echo"<form action='test_array.php' method='POST'>";

   foreach($x as $key){

     echo"<input type='text' name='key[]' >$key<br>";
    
      }

     echo"<br>";

         echo"<input type='submit' name='submit' value='Search'>";

            echo"</form>";

?>

Link to comment
Share on other sites

please give a textual example ok cheers.

 

 

<?php

$db=mysql_connect("localhost","username","password");

mysql_select_db("my_database",$db);

  if($_POST['submit']){

   $key=($_POST['key']);

    for ($i=0; $i<count($key); $i++){

$query="select * from users where name like '%$key[0]%' and age like '%$key[1]%' ";
      
      $result=mysql_query($query);


while($x=mysql_fetch_assoc($result)){


  echo " Username: ".$x['name']."  Age: ".$rec['age']." ";
   
}
   }
     }

$x=array("name","age");


  echo"<form action='test_array.php' method='POST'>";

   foreach($x as $key){

     echo"<input type='text' name='key[]' >$key<br>";
    
      }

     echo"<br>";

         echo"<input type='submit' name='submit' value='Search'>";

            echo"</form>";

?>

Link to comment
Share on other sites

this is the correct way becouse you dont have the data or querys in the loop but cheers?

<?php

$db=mysql_connect("localhost","username","password");

mysql_select_db("my_database",$db);

if($_POST['submit']){

$key=($_POST['key']);

for($i=0; $i<count($key); $i++){
 
$a=$key[0];
 
$b=$key[1];

}

$query="select * from users where name like '%$a%' and age like '%$b%' ";
      
$result=mysql_query($query);

while($x=mysql_fetch_assoc($result)){

echo " Username: ".$x['name']."  Age: ".$rec['age']." ";
   
}

}
   
$p=array("name","age");

echo"<form action='test_array.php' method='POST'>";

foreach($p as $key){

echo"<input type='text' name='key[]' >$key<br>";
    
}

echo"<br>";

echo"<input type='submit' name='submit' value='Search'>";

echo"</form>";

?>

Link to comment
Share on other sites

i got this now but becouse i am assigning the array key singlely is that why i dont need a loop?

 

 

Ok i understand but brushing up so any help is good for coding cheers.

 

<?php

$db=mysql_connect("localhost","username","password");

mysql_select_db("my_database",$db);

if($_POST['submit']){

$key=($_POST['a']);
$key=($_POST['b']);

$a=$key[0];
$b=$key[1];

$query="select * from users where name like '%$a%' and age like '%$b%' ";
     
$result=mysql_query($query);


while($x=mysql_fetch_assoc($result)){


echo " Username: ".$x['name']."  Age: ".$rec['age']." ";
  }
}

$x=array("name","age");

echo"<form action='test_array.php' method='POST'>";

foreach($x as $key){

echo"<input type='text' name='key[]' >$key<br>";
   
}

echo"<br>";

echo"<input type='submit' name='submit' value='Search'>";

echo"</form>";

?>

Link to comment
Share on other sites

i got this now but becouse i am assigning the array key singlely is that why i dont need a loop?

 

Yes.

 

But this code won't work. The assignment of $_POST['b'] to $key overwrites the previous assignment. The second 2 lines of code won't work because $key isn't an array.

<?php
$key=($_POST['a']);
$key=($_POST['b']);

$a=$key[0];
$b=$key[1];
?>

 

You would need this (note the "[]")

 

<?php
$key[]=($_POST['a']);
$key[]=($_POST['b']);

$a=$key[0];
$b=$key[1];
?>

 

but you might just as well do

 

<?php
$a=($_POST['a']);
$b=($_POST['b']);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.