pudge1 Posted March 30, 2009 Share Posted March 30, 2009 How would I get all the users in a database into an array for example if I had a fieldname called username and 3 users registered with the names tim tom and tommy how would I get tim tom and tommy all into an array from the database? Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/ Share on other sites More sharing options...
shlumph Posted March 30, 2009 Share Posted March 30, 2009 What do you have for code? You'd do something like this: <?php //Database Credentials $host = "localhost"; $user = "username"; $pass = "password"; $data = "database_name"; //Connect to server, and select database $con = mysql_connect($host, $user, $pass) or die("unable to connect"); mysql_select_db($data) or die("unable to select database"); //Get data into array $array_from_database = array(); $sql = "SELECT * FROM table_name"; $rs = mysql_query($sql) or die(mysql_error() . "<br />" . $sql); while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) { $array_from_database[] = $row; } print_r($array_from_database); Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-796601 Share on other sites More sharing options...
pudge1 Posted March 30, 2009 Author Share Posted March 30, 2009 For some reason it doesn't work. I'll look more into it. Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-797148 Share on other sites More sharing options...
pudge1 Posted March 30, 2009 Author Share Posted March 30, 2009 I made it echo it ($array_from_database) but all it said was "Array" Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-797151 Share on other sites More sharing options...
pudge1 Posted March 30, 2009 Author Share Posted March 30, 2009 It also echos all the data from the database... Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-797161 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 It echoes all of the data from the database because of the print_r(). echoing $array_from_database doesn't make sense. Why would you echo an array? Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-797398 Share on other sites More sharing options...
pudge1 Posted March 31, 2009 Author Share Posted March 31, 2009 To see why it isn't working. Anyway I used the in_array function to find something that WAS in the array (if would be working correctly) and it didn't work. Can someone please help me with this, I don't understand why it wouldn't work. Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-797409 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 in_array is not recursive, and mysql_fetch_array returns a multi dimensional array. Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-797422 Share on other sites More sharing options...
pudge1 Posted March 31, 2009 Author Share Posted March 31, 2009 So what should I do to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-797493 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 Do you want to check if something exists in the table? If so, you should use a WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-797501 Share on other sites More sharing options...
pudge1 Posted March 31, 2009 Author Share Posted March 31, 2009 So like $fun = blah blah blah WHERE Username="$username" if(!$fun) { echo "username doesn't exist"; } else { } that code minus syntax errors? Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-798108 Share on other sites More sharing options...
pudge1 Posted March 31, 2009 Author Share Posted March 31, 2009 hmm I did that but it still doesn't work. BTW this is a different code The signup page works now I need the signin page to work $my = mysql_connect("localhost","nintendo_pudge","*************"); if(!$my) { echo "Failed to Connect to MySQL Database<br />"; echo mysql_error(); exit; } else { } $con2 = mysql_select_db("nintendo_urlblurusers", $my); if(!$con2) { echo mysql_error(); exit; } else { } ?> <? $username = $_POST['username']; $password = $_POST['password']; $username = md5($username); $password = md5($password); $my = mysql_connect("localhost","nintendo_pudge","Franz5211-"); if(!$my) { echo "Failed to Connect to MySQL Database<br />"; echo mysql_error(); exit; } else { } $con2 = mysql_select_db("nintendo_urlblurusers", $my); if(!$con2) { echo mysql_error(); exit; } else { } $cuser = mysql_query("Select Username FROM Users2 Where Username='$username'"); if(!$cuser) { echo '<div id="redoutput"><br />Username Does Not Exist!<br /><br /></div>'; exit; } else { } $apassword = mysql_query("SELECT Password FROM Users2 WHERE Username='$username'"); if(!$apassword) { echo '<div id="redoutput"><br />Invalid Username/Password Combination<br /><br /></div>'; exit; } else { } if($password == $apassword) { echo '<div id="output"><br />Logged In!<br /><br /></div>'; } else { echo '<div id="redoutput"><br />Invalid Username/Password Combination (2)<br /><br /></div>'; } mysql_close($my); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-798111 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 mysql_query returns a resource, and you must extract the data from the resource using a function like mysql_fetch_assoc or mysql_result. Hopefully this doesn't offend you, but I think you need to read a couple PHP with MySQL tutorials. Quote Link to comment https://forums.phpfreaks.com/topic/151687-get-data-into-an-array/#findComment-798214 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.