Mythic Fr0st Posted February 26, 2007 Share Posted February 26, 2007 This stores all my info into those variables, however I need to loop through my database, and check if there is any info in "user" that matches the entered username (to signup)... if ($user == $row['user']) will return the first bit of information stored in my row 'user', wont it? if so how do I return ALL of it, and could you give me an example please, thanks in advance <?php //req files require("C:\Program Files\EasyPHP1-8\www\Main Files\Require Files\Notice.php"); require("C:\Program Files\EasyPHP1-8\www\Main Files\Require Files\Banner.php"); require("C:\Program Files\EasyPHP1-8\www\Main Files\Require Files\mysqlconnect.php"); require("C:\Program Files\EasyPHP1-8\www\Main Files\Require Files\buttons.css"); //begin login check $user = $_POST['user']; $pw1 = $_POST['pw1']; $pw2 = $_POST['pw2']; $em1 = $_POST['em1']; $em2 = $_POST['em2']; $hph = $_POST['hph']; $mph = $_POST['mph']; $loc = $_POST['locn']; $postcode = $_POST['pc']; $heard = $_POST['heard']; $con = $_POST['con']; $city = $_POST['city']; $result = mysql_query("SELECT * FROM `signup`")or die(return_error (__FILE__,__LINE__,mysql_error())); $row = mysql_fetch_array($result); //add Where $id = 'id' $ident = array(); if ($user == mysql_query("INSERT INTO signup(`user`,`pw1`,`pw2`) VALUES('$user','$pw1','$pw2')")or die (return_error(__FILE__,__LINE__,mysql_error())); ?> <html> <head> <title> Girl Cafe </title> </head> <body bgcolor="pink"> </body> </html> Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/ Share on other sites More sharing options...
monk.e.boy Posted February 26, 2007 Share Posted February 26, 2007 Not sure if I understand exactly what you want. $sql = "SELECT id,name,address,telephone FROM users WHERE name LIKE '%john%'"; this will select all users where name is LIKE (look this up in google) 'john'. $res = mysql_query( $sql ); while( $row = mysql_fetch_array( $res ) ) { echo row['name']; echo '<br>'; echo row['address']; } I guess you need to put your logic inside the loop. Hope that helps. monk.e.boy Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/#findComment-194207 Share on other sites More sharing options...
Mythic Fr0st Posted February 26, 2007 Author Share Posted February 26, 2007 Thanks, but not quite what I meant I mean, like I wanna check if the data in $user, is in the database, so you can't signup with someone elses name / Email address How do I do that? E.G if ($user = any data from the row "user" So Like For ($i = 0; $i < number of registered users in my db; $i++) { if ($user == $row.1.['user']; } I wanna check to make sure $user has not been used Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/#findComment-194823 Share on other sites More sharing options...
boo_lolly Posted February 26, 2007 Share Posted February 26, 2007 you're close. use while instead of for: <?php $db = mysql_connect("xxx", "xxx", "xxx"); mysql_select_db("your_db"); $sql = "SELECT * FROM your_table"; $query = mysql_query($sql); while($row = mysql_fetch_array($query)){ if($row['user_name'] == $_POST['user_name']){ /*print error*/ }else{ /*continue to register new user*/ } } ?> alternatively, if memory serves this will work in PHP, but you may be able to use a foreach loop: <?php $db = mysql_connect("xxx", "xxx", "xxx"); mysql_select_db("your_db"); $sql = "SELECT * FROM your_table"; $query = mysql_query($sql); $row = mysql_fetch_array($query); foreach($row['user_name'] as $key => $val){ if($val == $_POST['user_name']){ /*print error*/ }else{ /*continue to register new user*/ } } ?> that may be faster (if it works, that is). Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/#findComment-194827 Share on other sites More sharing options...
jcbarr Posted February 26, 2007 Share Posted February 26, 2007 Seems awful complicated... Why not just do a SELECT query with the $user in the WHERE clause and if it comes back with any rows then print a message saying that the username has already been registered by someone else. Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/#findComment-194836 Share on other sites More sharing options...
Mythic Fr0st Posted February 27, 2007 Author Share Posted February 27, 2007 >> Seems awful complicated... Why not just do a SELECT query with the $user in the WHERE clause and if it comes back with any rows then print a message saying that the username has already been registered by someone else. Thats what im trying to do, just check if "any" of them in that row are matching the data in $user I can do what boo_lolly said (PS Thanks for the code boo_lolly) easily enough, but its quite large lol, I dont wanna have alot of those in my code Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/#findComment-194934 Share on other sites More sharing options...
corbin Posted February 27, 2007 Share Posted February 27, 2007 Are you trying to pull one row and see if the user name is in that one row, or pull all the rows and see if the username is in the table? If its the latter: //mysql stuff $q = mysql_query("SELECT username FROM users WHERE username = '{$uname}'"); $n = mysql_num_rows($q); if($n > 0) { // it exists } else { //it doesnt exist } Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/#findComment-194939 Share on other sites More sharing options...
Mythic Fr0st Posted February 27, 2007 Author Share Posted February 27, 2007 Im trying to see if username is in that "one" row, should I use that code? Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/#findComment-195021 Share on other sites More sharing options...
boo_lolly Posted February 27, 2007 Share Posted February 27, 2007 Im trying to see if username is in that "one" row, should I use that code? OH, i thought you wanted to check to see if that user is located in any row in the database. i don't see why you wouldn't want to tho. your welcome for the code. but my code will search the entire table to see if the username has already been taken. if you want to check a specific row, you need to narrow the query down to one single row, you do this by adding a WHERE clause, like so: <?php $db = mysql_connect("xxx", "xxx", "xxx"); mysql_select_db("your_db"); $sql = "SELECT * FROM your_table WHERE something = 'something_else'"; $query = mysql_query($sql); $numRows = mysql_num_rows($query); if($numRows < 1){ /*continue to register new user*/ }else{ /*print error*/ } ?> Link to comment https://forums.phpfreaks.com/topic/40131-looping-through-my-database-and-checking-if-that-info-is-identical-to-a-variable/#findComment-195251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.