ian89 Posted May 6, 2008 Share Posted May 6, 2008 I'm trying to create a search page that pulls data from a MySQL database with multiple tables and multiple columns. Connecting to the database and such are fine, but I'm at lost at how to make a workaround for my problem as I'm fairly new to this. My problem is, the search query only shows the the first column of one table. How do I modify this existing code as such that it will search through other tables and other columns? <?php $var = @$_GET['q'] ; $trimmed = trim($var); // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } mysql_connect("localhost","username","password"); mysql_select_db("database_name") or die("Unable to select database"); $query = "select * from table_name where field like \"%$trimmed%\" order by field"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); } I guess a drop down box at the search form to choose which table to search for would be convenient, but I dunno how to do that yet. Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/104391-search-script-for-mysql-database/ Share on other sites More sharing options...
smartin1017 Posted May 6, 2008 Share Posted May 6, 2008 <?php $var = @$_GET['q'] ; $trimmed = trim($var); // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } mysql_connect("localhost","username","password"); mysql_select_db("database_name") or die("Unable to select database"); //I dont think you gave enough info to solve this one but I have an idea to start.... //create an array with your tables $table[0] = name-of-first-table; $table[1] = name-of-first-table; $table[2] = name-of-first-table; $table[3] = name-of-first-table; $table[4] = name-of-first-table; //.....till you hit the number of table you have //set the '10' in $i < 10 to the amount of tables you have for ( $i = 0; $i <= 10; $i += 1) { $c_table= $table[$i]; $query = "select * from $c_table where field like \"%$trimmed%\" order by field"; // this is where I have no idea whereyou want to go with this. This will roughly get you started. //the for loop will run through the tables and search each one. then you can figure out if the search is a match, // do what you want with it then if you find a match you can either go through the rest of the loops or break out // by making $i= number_greater_then_tables. Hopefully this gets you started, Link to comment https://forums.phpfreaks.com/topic/104391-search-script-for-mysql-database/#findComment-534478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.