Jump to content

Search MySQL database script problem.


Ruud Hermans

Recommended Posts

I am trying to edit the following script;

<?
//connect to mysql and the right database 
include 'config.php'; 

$search=$_POST["search"];

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM news WHERE message LIKE '%$search%'");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $title=$r["title"];
   $message=$r["message"];
   $who=$r["who"];
   $date=$r["date"];
   $time=$r["time"];
   $id=$r["id"];
   
   //display the row
   echo "$title <br> $message <br> $who <br> $date | $time <br>";
}
?>

I allready changed the part to connect to MySQL and the databse "test" but I do not understand this part;

//change whatevertable to the mysql table you're using

Does this refferes to the follwing line of code?

$result = mysql_query("SELECT * FROM news WHERE message LIKE '%$search%'");

If the table was called members and I want to change it so it searches trough the phonenumbers do I need to change it in this way then?

$result = mysql_query("SELECT * FROM members WHERE phonenumber LIKE '%$search%'");

And what does this part do exactly?

   $title=$r["title"];
   $message=$r["message"];
   $who=$r["who"];
   $date=$r["date"];
   $time=$r["time"];
   $id=$r["id"];

Note: I hope I posted in the right forum this time, I thought it reffered to MySQL so I presume it should be posted here.

Link to comment
Share on other sites

A rough translation of your $result query is this:

SELECT columns FROM table WHERE whatcolumnyouwant LIKE '%$whatyouaresearchingfor%'

So, "whatevertable" would be the table (news), and "whatevercolumn" would whatcolumnyouwant (message).

 

Your syntax for searching the phone number is correct, assuming you have a table named "members" and a column "phonenumber".

 

This here:

$title=$r["title"];
   $message=$r["message"];
   $who=$r["who"];
   $date=$r["date"];
   $time=$r["time"];
   $id=$r["id"];

Is the result of your mysql_fetch_array query, sent to the variable $r. These are the names of columns in your table (so in this case, you have a table named title, message, who, date, time, id.) In the next line, you asked for them to be printed/shown.

Link to comment
Share on other sites

Edited it like this;

<?
//connect to mysql and the right database 
include 'config.php'; 

$search=$_POST["search"];

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM members WHERE name LIKE '%$search%'");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $name=$r["name"];
   $email=$r["email"];
   $location=$r["location"];
   
   //display the row
   echo "$name <br> $email <br> $location <br>";
}
?>

The problem is that it spits the following out when I run it knowing the edited ino is correct.

$email

$location

"; } ?>

Link to comment
Share on other sites

What are you trying to do???

I am trying to make a search form to search trough a MySQL database. THe other part of it is;

Search:

<form method="post" action="search.php">
<input type="text" name="search" size=25 maxlength=25>
<input type="Submit" name="Submit" value="Submit">
</form>

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.