SRL Archive - Web Development

Need help


~alex~

2007-05-10 13:37:40

Well I've been learning Php so I can build myself an auth server. I've been doing pretty well. I've made a page that can add users to the data base and stuff but Im having problems getting the script to actually see if a user exists in the database..

Anytime I try to run this script im getting

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /www/1111mb.com/s/c/a/scaralex/htdocs/login2.php on line 10 "
[PHP]<?php
$con = mysql_connect("localhost","hmmmm..","Cant tell you that hehe");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("users", $con);

while($row = mysql_fetch_array(mysql_query("SELECT * FROM udata WHERE Username= .$_POST[user]")));
{
if ($row['username'] = $_POST["user"]){
if ($row['password'] = $_POST["pass"]){
die("accepted");
}
}
}
die("Declined");
?>[/PHP]

It wouldn't suprise me if the enitre thing was wrong but I'd still like a lil help :P

Markus

2007-05-11 14:48:37

That means that there's an error with the SQL part of your script.
Call somewhere after the query echo(Mysql_error()); and post the error.

Night

2007-05-13 16:40:54

try this:
[php]
$user = $_POST['user'];
while($row = mysql_fetch_array(mysql_query("SELECT * FROM `udata` WHERE `username`= '$user'"))) {
[/php]

try using that for the while statement… Im pretty sure the error ur getting is because of the capital U in username, but try the line I gave you, it looks neater :) Tell me if it helps

Markus

2007-05-18 15:38:38

Try this one:
[php]
<?php
$con = mysql_connect("localhost","hmmmm..","Cant tell you that hehe");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("users", $con);

while($row = mysql_fetch_array(mysql_query("SELECT username, password FROM udata WHERE Username= '$_POST[user]'")));
{
if ($row['username'] = $_POST["user"]){
if ($row['password'] = $_POST["pass"]){
die("accepted");
}
}
}
die("Declined");
?>
[/php]

TOB

2007-05-24 04:22:07

This was your problem:
[php]mysql_query("SELECT * FROM udata WHERE Username= .$_POST[user]")[/php]

See you tried to concatenate but you forgot to close the string then concatenate then reopen.


Also, I tend to make my code a bit more neat.

Actual file:
[php]
<?php
include_once('config.php');
mysql_select_db($dbname) or die('Error selecting database');

$retrieveuserdata = mysql_query("SELECT username, password FROM
udata WHERE username='".$_POST[user]."'");

while($userdata = mysql_fetch_array($retrieveuserdata)) {
if ($userdata['username'] = $_POST["user"]){
if ($userdata['password'] = md5($_POST["pass"])){
die("Authenticated");
}
}
} else {
die("Declined");
}
?>
[/php]

config.php (place in same directory)
[php]
<?php
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = '';
?>
[/php]

Abyssal

2007-05-24 22:21:44

TOB - I like how you added the config file, makes it neat ^^ I'll use that!

~alex~

2007-05-29 06:08:34

Thanks for the help people but i sorta discontinued this a while ago. Ill use it in the future though. :)

Dankness

2007-06-02 17:45:22

also for forward comptability always qoute your array key names. IE

array[key] = php 4 comp
array['key']= php5 comp

Belive itor not but if you pick the right word in php5 ou'll get a fatal error
IE
array[private] = fatal error that will take you forever to find probably

Leo123

2007-06-03 11:12:43

while($row = mysql_fetch_array(mysql_query("SELECT * FROM udata WHERE Username= .$_POST[user]")));


Should be better syntax:

while($row = mysql_fetch_array(mysql_query("SELECT * FROM udata WHERE Username='$_POST[user]'")));

And with no . before $_POST[user]