czukoman20 Posted October 15, 2007 Share Posted October 15, 2007 I have a mysql database named userdata setup. like this Database = userdata Under userdata are the columns buddy and username what i want to do is make a page that has a textbox with a button on it that says view data. when you type one of the usernames in the textbox then you would hit view data. when view data is hit. it will search the database for every row in the column username that = textbox then display all of the rows it found I know how to connect to the database.. i just need help searching the database and displaying the data thanks Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/ Share on other sites More sharing options...
hvle Posted October 15, 2007 Share Posted October 15, 2007 $sql = "select `username` from `userdata` where username='$nameyouwanttosearch'"; Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/#findComment-369591 Share on other sites More sharing options...
czukoman20 Posted October 15, 2007 Author Share Posted October 15, 2007 Ok i will try that thanks.. i still have to setup the actual .php. but i'm pretty sure that will work Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/#findComment-369594 Share on other sites More sharing options...
hostfreak Posted October 15, 2007 Share Posted October 15, 2007 Pretty basic sql statement like hvle said, then to expand on it: <?php if (isset($_POST['submit'])) { $txt_box = $_POST['txt_box']; $sql = "SELECT username FROM userdata WHERE username = '" . $txt_box . "' ORDER BY username ASC"; $query = mysql_query($sql) OR DIE(mysql_error()); while ($row = mysql_fetch_assoc($query)) { $usr_name = $row['username']; echo $usr_name . '<br />'; } } else { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Basic</title> </head> <body> <form name="search" method="POST"> <input name="txt_box" type="text" /><br /> <input name="submit" type="submit" value="Find User" /> </form> </body> </html> <?php } ?> Of course you will also want to do your input validation to make sure what they enter is what you expect (a username) Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/#findComment-369607 Share on other sites More sharing options...
czukoman20 Posted October 15, 2007 Author Share Posted October 15, 2007 ok you just simplified my life a lot. thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/#findComment-369609 Share on other sites More sharing options...
czukoman20 Posted October 15, 2007 Author Share Posted October 15, 2007 Pretty basic sql statement like hvle said, then to expand on it: Code: <?php if (isset($_POST['submit'])) { $txt_box = $_POST['txt_box']; $sql = "SELECT username2 FROM userdata WHERE username2 = '" . $txt_box . "' ORDER BY username2 ASC"; $query = mysql_query($sql) OR DIE(mysql_error()); while ($row = mysql_fetch_assoc($query)) { $usr_name = $row['username2']; $usr_bud = $row['buddy]; echo $usr_name . $usr_con'<br />'; } } else { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Basic</title> </head> <body> <form name="search" method="POST"> <input name="txt_box" type="text" /><br /> <input name="submit" type="submit" value="Find User" /> </form> </body> </html> <?php } ?> ok well now the issue is. when i give the name of the username2. it finds it just right.. but doesnt look at the other column (buddy) so the username is guest and his buddies are buddy1 buddy2 when i type guest in the box all i see is guest guest when its supposed to say guest buddy1 guest buddy2 Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/#findComment-369647 Share on other sites More sharing options...
Barand Posted October 15, 2007 Share Posted October 15, 2007 $sql = "SELECT username2, buddy FROM userdata WHERE username2 = '" . $txt_box . "' ORDER BY buddy "; Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/#findComment-369783 Share on other sites More sharing options...
hostfreak Posted October 15, 2007 Share Posted October 15, 2007 Also, your going to get an error with: echo $usr_name . $usr_con'<br />'; //improper use of the concatenation operater See: //should be echo $usr_name . ' ' . $usr_con . '<br />'; //however, in this case it is just easier to enclose with double quotes echo "$user_name $usr_con<br/>"; By the way, where is $usr_con coming from? Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/#findComment-369811 Share on other sites More sharing options...
czukoman20 Posted October 17, 2007 Author Share Posted October 17, 2007 just an example.. its ok i figured it out thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/73259-solved-database-reading/#findComment-371277 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.