Jump to content

Get Data into an Array


pudge1

Recommended Posts

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.