Can't get data

MySQL database:[code]

mysql> SELECT * FROM login_data;
±—±---------±-----------------------------------------+
| id | username | password |
±—±---------±-----------------------------------------+
| 1 | foo | 62cdb7020ff920e5aa642c3d4066950dd1f01f4d |
±—±---------±-----------------------------------------+
1 row in set (0.00 sec)
[/code]
The password is SHA(‘bar’).
PHP code:
[php]

Logged In <?php $dbc = mysqli_connect(**********) or die('

Error!

'); $user = $_POST['username']; $pass = $_POST['password']; $data = mysqli_query("SELECT * FROM login_data WHERE username = $user;",$dbc); if(empty($data)) { echo '

Sorry, that username is unregistered.

'; } else { $row = mysqli_fetch_array($data); if($row['password'] == sha1($pass)) { session_start(); $_SESSION = array(); $_SESSION['username'] = $user; $_SESSION['user_id'] = $row['id']; ?>

<?php echo "

You are now logged in as $user.

"; ?>

Back to Home

<?php } else { ?>

<?php echo "

THe username $username and/or password were incorrect.

" ?>

Back Home Back to the login page

<?php } mysqli_close($dbc); } ?> [/php] Whenever I try to login with 'foo' and 'bar', the output is always "Sorry, that username is unregistered." What's wrong?

tghe stript is not getting as far as the password check so there much be an issue with the query using the username

[php]
$data = mysqli_query(“SELECT * FROM login_data WHERE username = $user;”,$dbc);
if(empty($data)) {
echo ‘

Sorry, that username is unregistered.

’;
}
[/php]

I’d try putting quotes around $user and also show if there are any errors:

[php]$data = mysqli_query(“SELECT * FROM login_data WHERE username = ‘$user’”,$dbc)or die(mysqli_error());
if(empty($data)) {
echo ‘

Sorry, that username is unregistered.

’;
}[/php]

I did that, and now I’m getting nothing, which probably means there’s a problem with ‘foo’. I put the statement “[tt]echo $data[/tt]” in, like:
[php]

$data = mysqli_query(“SELECT * FROM login_data WHERE username = ‘$user’;”,$dbc)
or die(mysqli_error());
echo “

$data

”; //<-- here’s where it is
if(empty($data)) {
echo ‘

Sorry, that username is unregistered.

’;
}
[/php]
I even tried a [tt]SELECT * FROM login_data;[/tt] statement similar to that one, which should show the whole table, but I couldn’t get anything. The [tt]or die[/tt] in the [tt]mysqli_connect[/tt] statement should show up an error if there’s a problem, which there’s not. All of this means there is most likely a typo somewhere, so, like so many problems, the answer should be completely obvious. I’d just like it to be obvious to me, too.

okay lets just get it to loop through the user table and show usernames to start with.

this should loop through all records and show the username, I haven’t used mysqli to maybe there might be something wrong with how mysqli extracts data.

[php]$data = mysqli_query(“SELECT * FROM login_data WHERE username = ‘$user’”,$dbc)or die(mysqli_error());
while($r = mysql_fetch_object($data)){
echo “

$r->username

”;
}[/php]

If I were using mysql this would work:

[php]
//connect to db
$data = mysql_query(“SELECT * FROM login_data WHERE username = ‘$user’”)or die(mysqli_error());
while($r = mysql_fetch_object($data)){
echo “

$r->username

”;
}[/php]

The reason I’m not getting anything is because I have errors disabled. I’ll get back to you when I find out what’s going wrong.

Okay, here’s the error:

[tt]Catchable fatal error: Object of class mysqli_result could not be converted to string in /var/www/qoqqoph/login.php[/tt](that’s my file)[tt] on line 24[/tt]
Line 24 says:
[php]
echo “

$data

”;
[/php]
because I was checking if there was data.

Hooray, it’s finally working!

Sponsor our Newsletter | Privacy Policy | Terms of Service