note 102339 added to function.mysql-fetch-field
liveonaware
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
<?php
/* Convert your Table fields / columns to variables and use them in your input tags to search the same Table...
Just copy & paste code to see for yourself
*/
//1. connect to database
$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database');
//2. get all table fields in MySQL and store them in an array
$table_fields = array();
$query = "SHOW COLUMNS FROM customers ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$table_fields[] = $row["Field"];
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<?php
//3. make table fields to input tags inside a form
foreach($table_fields as $field_input) { ?>
<tr><td><?php echo $field_input.": "; ?></td><td><input type="text" name="<?php echo $field_input; ?>" /></td></tr>
<?php } ?>
<tr><td></td><td><input type="submit" name="submit" value="Go" style="width: 100px" /></td></tr>
</table>
</form>
<?php
//4. finally, search the same table
if(isset($_POST["submit"])) {
$query2 = "SELECT * FROM customers WHERE id IS NOT NULL ";
//exclude the SUBMIT KEY on the POST Array, add as necessary
//note: MySQL Text Blob, cannot be searched in my kind of Query below because of LIKE % so I added it in excluded list
$excluded_keys_from_post = array('submit', 'sometextarea');
foreach($_POST as $key => $value) {
if(in_array($key, $excluded_keys_from_post)) {
//don't search the POST KEYS in the exclusion array
continue;
}
$query2 .= " AND $key LIKE '" .mysql_real_escape_string($value). "%' ";
}
echo "<hr />";
$result2 = mysql_query($query2);
echo $query2; //just to show what the query looks like
while($row2 = mysql_fetch_array($result2)) {
echo $row2["last_name"]."<br />";
}
$numrows = mysql_num_rows($result2);
if($numrows <= 0) {
echo "No results found.";
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 114.108.192.11 (proxied: 111.68.42.183)
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-field.php
Edit -- https://master.php.net/note/edit/102339
Del: integrated -- https://master.php.net/note/delete/102339/integrated
Del: useless -- https://master.php.net/note/delete/102339/useless
Del: bad code -- https://master.php.net/note/delete/102339/bad+code
Del: spam -- https://master.php.net/note/delete/102339/spam
Del: non-english -- https://master.php.net/note/delete/102339/non-english
Del: in docs -- https://master.php.net/note/delete/102339/in+docs
Del: other reasons-- https://master.php.net/note/delete/102339
Reject -- https://master.php.net/note/reject/102339
Search -- https://master.php.net/manage/user-notes.php