Jump to content

[SOLVED] Simple MYSQL Search Problem


scott botkins

Recommended Posts

Hey Guys,

 

Here's my problem which I think may be simple to solve hopefully, I'm new to searching mysql databases.

 

I have this setup http://www.invisionblue.com/designs/gdogs which goes along with http://www.invisionblue.com/designs/gdogs/db.gif

 

It has over 100,000 listings. Right now i have it displaying the results by the SZNummer column. However I'm wanting it to be where I type in the dog name (Hundename) it will display the results instead of by SZNummer. You'll notice the tables and information isn't in english, that's because the client is from another country. Below is my code, should be simple I believe. Thanks in advance for the help!

 

<form action="index.php" method="post">
<p>ID For Hundename: <input type="text" name="id" /></p>
<p><input type="submit" /></p>
</form>


<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("host","user","pass"); //(host, username, password)

//select which database you want to edit
mysql_select_db("garydogs"); 

//select the table
$id = (int)$_POST['id'];
$result = mysql_query("select * from Hundestamm WHERE ( `Hundestamm` . `SZNummer` = $id ) limit 1");

//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["Hundename"];
   $zwing=$r["Zwingername"];
   $ges=$r["Geschlecht"];

   
   //display the row
   echo "<b>Hundename:</b><br>$title<br><br><b>Zwingername:</b><br>$zwing<br><br><b>Geschlecht:</b><br>$ges<br><br>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/59456-solved-simple-mysql-search-problem/
Share on other sites

Change the column to Hundename, escape it, make sure it's in quotes...

 

<?php

// ...

$result = mysql_query(sprintf('SELECT * FROM Hundestamm WHERE Hudename = "%s"', mysql_real_escape_string($_POST['id']));

// ... or a "starts with" variant

$result = mysql_query(sprintf('SELECT * FROM Hundestamm WHERE Hudename LIKE "%s%%"', mysql_real_escape_string($_POST['id']));

?>

Right, a missing closing parenthesis in both cases.

 

Corrected:

 

<?php

// ...

$result = mysql_query(sprintf('SELECT * FROM Hundestamm WHERE Hudename = "%s"', mysql_real_escape_string($_POST['id'])));

// ... or a "starts with" variant

$result = mysql_query(sprintf('SELECT * FROM Hundestamm WHERE Hudename LIKE "%s%%"', mysql_real_escape_string($_POST['id'])));

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.