postbil.com Posted February 10, 2010 Share Posted February 10, 2010 Hello everybody. I had a small problem with a search function on my site. The idea was to create a function there make it possible for the user to search for friends on the site. I had written the code but of some reason it doesn’t work, now I have spend a lot of time to find the error but I have no more ideas.. Somebody please help me, tell me what I do wrong.. Here is my code: <?php include '../DB/db.connect.inc.php'; ?> <html> <head> <title>WWW.POSTBIL.COM - Projekt arbejde.</title> <link rel="stylesheet" type="text/css" href="../style/style.css"> <style type="text/css"> th { background-color: #999;} .odd_row { background-color: #EEE;} .even_row { background-color: #FFF;} </style> </head> <body> <h2>Search for frinds</h2> <form method="post" action="add_contact.php" > <table> <tr> <td><input type="text" name="search" id="search"></td> <td><input type="submit" name="submit" id="submit" value="search"></td> </tr> </table> <table style="width="100%"> <tr><th>Email</th><th>Fifstnavn</th><th>Lastnavn</th><th>Adresse</th><th>City</th><th>contry</th><th>Phone</th><th>Homepage</th><th>Work</th><th>Company</th><th>Work adress</th><th>Work city</th><th>Work phone</th><th>Direct work phone</th><th>Work homepage</th></tr> <?php if(isset($_POST['submit']) && $_POST['submit'] == 'search'){ $search = (isset($_POST['search'])) ? trim($_POST['search']) : ''; // Here I had my Mysql statement $query = 'SELECT firstName, lastName, email, adress, city, contry, phone, homepage, work, workCompany, workAdress, workCity, workPhone, workDirectPhone, workHomepage FROM site_user WHERE firstName LIKE '%$search%''; $result = mysql_query($query, $conn) or die(mysql_error($conn)); $odd = true; while ($row = mysql_fetch_array($result)){ echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">'; $odd = !$odd; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['email'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['firstName'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['lastName'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['adress'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['city'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['contry'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['phone'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['homepage'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['work'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['workCompany'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['workAdress'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['workCity'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['workPhone'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['workDirectPhone'] . '</a></td>'; echo '<td><a href="user.php?id=' . $row['user_id'] . '">' . $row['workHomepage'] . '</a></td>'; echo '</tr>'; } mysql_free_result($result); mysql_close($conn); } ?> </table> </form> </body> </html> Postbil.com Quote Link to comment https://forums.phpfreaks.com/topic/191603-problem-with-search-function/ Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 uset mysql_fetch_assoc instead of mysql_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/191603-problem-with-search-function/#findComment-1010021 Share on other sites More sharing options...
postbil.com Posted February 10, 2010 Author Share Posted February 10, 2010 Okay .. but there is still nothing when I try to look at some of the values I have in the database. and if I dont type something into the search box and try to search, so do I view all records in the database .. how? Quote Link to comment https://forums.phpfreaks.com/topic/191603-problem-with-search-function/#findComment-1010075 Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 Keynames in result row are lowercased so $row['userWebsite'] = null but $row['userwebsite'] should have some values And btw if you selecting all (or almost all) columns from table u can use "SELECT * FROM blab blab bla" instead "SELECT col1,col2,col3, ..., col100 FROM ..." and yes LIKE '%%' should return all rows Quote Link to comment https://forums.phpfreaks.com/topic/191603-problem-with-search-function/#findComment-1010083 Share on other sites More sharing options...
postbil.com Posted February 10, 2010 Author Share Posted February 10, 2010 sorry but I do not entirely understand what you mean by "KeyName in result row are lowercased" I can get you to fix it in code so I can see what you mean? I am quite new in php .. Quote Link to comment https://forums.phpfreaks.com/topic/191603-problem-with-search-function/#findComment-1010106 Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 After query result is fetched php automaticlly makes index name of array lowercased for example $row['workHomepage'] doesn't exists what u realy want to echo is $row['workhomepage'] simply u can var_dump $row and u will see while($row = ...) { var_dump($row); //rest of the code } Quote Link to comment https://forums.phpfreaks.com/topic/191603-problem-with-search-function/#findComment-1010121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.