SRL Archive - Web Development

MySQL


Timer

2008-11-18 21:58:27

[PHP]<?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);
?>[/PHP]

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

2008-11-19 02:54:06

Username and Password are case sensitive in the $row. Other than that, I think it should work (you could try to lowercase SleepAccounts).

Wizzup?

2008-11-19 05:06:29

From: Timer
[PHP]<?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);
?>[/PHP]

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.

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

That should work.

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

MylesMadness

2008-11-19 07:12:24

Yeah, were going to

Timer

2008-11-19 07:30:44

From: Wizzup?
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.

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

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

2008-11-19 07:51:46

((list($user, $pass) > (list($user, $pass)

Wizzup?

2008-11-19 12:22:33

Yea, sorry. I wrote it without actually testing it. It should indeed start with only one (.

Timer

2008-11-19 15:17:45

Hmm, somthings not working right here, eather, its not entering them in the database, or its not reading them right.

Register:
[PHP]<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);
}
?>[/PHP]

The start to Login.php (used to see if its really entering in the DB)
[PHP]<?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);
?> [/PHP]

nielsie95

2008-11-19 16:40:25

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?

2008-11-19 16:48:14

[php]mysql_select_db("", $con); [/php]

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

[php]
//psuedo

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

Timer

2008-11-19 17:28:07

i just took the table usrname and pass out. i got em.

Timer

2008-11-19 17:47:34

Hey Wizzy?

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

Wizzup?

2008-11-19 18:03:49

From: Timer
Hey Wizzy?

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


The following is untested, but should work:

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

Timer

2008-11-19 19:36:47

Thanks!

*Loves Wizzup? forever*

Floor66

2008-11-30 03:48:55

Don't wanna smartass but:
[php]
$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
[/php]