PDA

View Full Version : MySQL



Timer
11-19-2008, 02:58 AM
<?php
mysql_connect("localhost", "Usrnme", "pswrd") or
die("Could not connect: " . mysql_error());
mysql_select_db("dtabse");

$result = mysql_query("SELECT Username, Password FROM SleepAccounts");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("Name: %s Pass: %s", $row["Username"], $row["Password"]);
}

mysql_free_result($result);
?>

It wont return any INSERTed data into the DataBase. (Made this to check if the data was really being inserted or not). But i think it is, there's no errors, but this wont return them, plus ill need it for login, can anyone help?

nielsie95
11-19-2008, 07:54 AM
Username and Password are case sensitive in the $row. Other than that, I think it should work (you could try to lowercase SleepAccounts).

Wizzup?
11-19-2008, 10:06 AM
<?php
mysql_connect("localhost", "Usrnme", "pswrd") or
die("Could not connect: " . mysql_error());
mysql_select_db("dtabse");

$result = mysql_query("SELECT Username, Password FROM SleepAccounts");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("Name: %s Pass: %s", $row["Username"], $row["Password"]);
}

mysql_free_result($result);
?>

It wont return any INSERTed data into the DataBase. (Made this to check if the data was really being inserted or not). But i think it is, there's no errors, but this wont return them, plus ill need it for login, can anyone help?

The actual MySQL Log in is case sensitive. If you are going to use strings in PHP, use ' instead of ". Case sensitivity should also be watched out in $row.
Anyway, what you are doing is quite a weird way of getting the vars.



while((list($user, $pass) = mysql_fetch_row($result)) {
printf('Name: %s Pass: %s', $user, $pass);
}


That should work.

EDIT: Also, I take it you md5'ed the passwords?

MylesMadness
11-19-2008, 12:12 PM
Yeah, were going to

Timer
11-19-2008, 12:30 PM
The actual MySQL Log in is case sensitive. If you are going to use strings in PHP, use ' instead of ". Case sensitivity should also be watched out in $row.
Anyway, what you are doing is quite a weird way of getting the vars.



while((list($user, $pass) = mysql_fetch_row($result)) {
printf('Name: %s Pass: %s', $user, $pass);
}


That should work.

EDIT: Also, I take it you md5'ed the passwords?
Yeah, now..


Parse error: syntax error, unexpected '{' on line 8
Thanks btw

nielsie95
11-19-2008, 12:51 PM
((list($user, $pass) > (list($user, $pass)

Wizzup?
11-19-2008, 05:22 PM
Yea, sorry. I wrote it without actually testing it. It should indeed start with only one (.

Timer
11-19-2008, 08:17 PM
Hmm, somthings not working right here, eather, its not entering them in the database, or its not reading them right.

Register:

<title>Register - Sleepwalker</title>

<?php
$register = '<form id="form1" name="form1" method="post" action="register.php">
<table width="241" border="0">
<tr>
<td width="231"><label>Username</label>
<input type="text" name="Username" id="Username" /></td>
</tr>
<tr>
<td><label>Password </label>
<input type="password" name="Password" id="Password" /></td>
</tr>
<tr>
<td><label>Email</label>
<input type="text" name="Email" id="Email" /></td>
</tr>
<tr>
<td><input type="submit" name="Register" id="Register" value="Register" /></td>
</tr>
</table>
<label></label>
<p>
<label></label>
</p>
</form>';
If (!isset($_POST["Username"])){
echo $register;
Die();
}
else
{
$con = mysql_connect("localhost","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
$result = mysql_query("SELECT * FROM SleepAccounts");

while($row = mysql_fetch_array($result)) {
if ($row["Username"] = $_POST["Username"]) {
$takennoob = "99";
}
}
if ($takennoob == "99") {
echo "Username taken, please pick a new one <br><br>".$register;
$takennoob = "0";
Die();
}
else{
$ppppoooffffppppdd = $_POST["Username"];
$gOooooooooff844d = $_POST["Password"];
$ilovepiehowaboutyou = $_POST["Email"];
mysql_query("INSERT INTO SleepAccounts (Username, Password, Email)
VALUES ($ppppoooffffppppdd, $gOooooooooff844d, $ilovepiehowaboutyou)");
echo "Account created, thank you for using sleepwalker 2.0!";
Die();
}
mysql_close($con);
}
?>

The start to Login.php (used to see if its really entering in the DB)

<?php
mysql_connect("localhost", "", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("");

$result = mysql_query("SELECT * FROM SleepAccounts");

while (list($user, $pass) = mysql_fetch_row($result)) {
printf('Name: %s Pass: %s', $user, $pass);
}

mysql_free_result($result);
?>

nielsie95
11-19-2008, 09:40 PM
You need to insert them as strings:

VALUES ('$ppppoooffffppppdd', '$gOooooooooff844d', '$ilovepiehowaboutyou')"

Also, you should protect those strings: http://nl2.php.net/function.mysql-real-escape-string

I also think your way of checking if there's a username taken doesn't work.
($row["Username"] = $_POST["Username"]) just assigns $row, it doesn't check if they're equal(you need ==).. A better way is to just search for the new username (WHERE (Username = 'newusername')). If there are 0 rows in the result, the name's not picked.

Wizzup?
11-19-2008, 09:48 PM
mysql_select_db("", $con);

You should at least select a database...
To find out if a user already exists:



//psuedo

$result = mysql_query('SELECT USERNAME FROM MYTABLE WHERE USERNAME = \'' . $theUserName . '\'');
if(mysql_numrows($result) == 0) $noUser = true;

Timer
11-19-2008, 10:28 PM
i just took the table usrname and pass out. i got em.

Timer
11-19-2008, 10:47 PM
Hey Wizzy?

How can i get like...
Print out every username in the SleepAccounts table?

Wizzup?
11-19-2008, 11:03 PM
Hey Wizzy?

How can i get like...
Print out every username in the SleepAccounts table?

The following is untested, but should work:


$result = mysql_query('SELECT USERNAME FROM SLEEPACCOUNTS');
while (list($username) = mysql_fetch_row($result)) {
echo 'User: ' . $username;
}

Timer
11-20-2008, 12:36 AM
Thanks!

*Loves Wizzup? forever*

Floor66
11-30-2008, 08:48 AM
Don't wanna smartass but:


$result = mysql_query('SELECT `username` FROM `sleepaccounts`'); //Best to use backsticks: ``; just plain security
while ($row = mysql_fetch_array($result)) {
echo 'User: ' . $row['username'];
} //Thisll just fetch an array of it
//Is also possible