Author Topic: Can't get data  (Read 567 times)

zebby12

  • New Member
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Can't get data
« on: June 27, 2010, 05:12:04 PM »
MySQL database:
Code: [Select]

mysql> SELECT * FROM login_data;
+----+----------+------------------------------------------+
| id | username | password                                 |
+----+----------+------------------------------------------+
|  1 | foo      | 62cdb7020ff920e5aa642c3d4066950dd1f01f4d |
+----+----------+------------------------------------------+
1 row in set (0.00 sec)
The password is SHA('bar').
PHP code:
PHP Code: [Select]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
<
html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <
head>
    <
meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <
title>Logged In</title>
    <
link type="text/css" rel="stylesheet" href="Axerta/CSS1.css" />
  </
head>
  <
body>
<?
php
$dbc 
mysqli_connect(**********)
  or die(
'<h1>Error!</h1>');
$user $_POST['username'];
$pass $_POST['password'];
$data mysqli_query("SELECT * FROM login_data WHERE username = $user;",$dbc);
if(empty(
$data)) {
  echo 
'<h1>Sorry, that username is unregistered.</h1>';
}
else {
  
$row mysqli_fetch_array($data);
  if(
$row['password'] == sha1($pass)) {
    
session_start();
    
$_SESSION = array();
    
$_SESSION['username'] = $user;
    
$_SESSION['user_id'] = $row['id'];
  
?>
      <h2><?php echo "<h2>You are now logged in as $user.</h2>"?></h2>
      <p><a href="home.html">Back to Home</a></p>
  <?php
  
}
  else { 
?>
    <h2><?php echo "<h2>THe username $username and/or password were incorrect.</h2>" ?></h2>
  <p>
  <a href="home.html">Back Home</a>
  <a href="login.html">Back to the login page</a>
  </p>
  <?php 
  
}
mysqli_close($dbc);
}
?>
  </body>
</html>

Whenever I try to login with 'foo' and 'bar', the output is always "Sorry, that username is unregistered." What's wrong?
« Last Edit: June 30, 2010, 04:46:49 PM by zebby12 »
Whoops. Is it okay if your toast is burnt?

daveismyname

  • Senior Member
  • ****
  • Posts: 204
  • Karma: +1/-0
  • PHP Helper
    • View Profile
    • Dave is my name
Re: Can't get data
« Reply #1 on: July 01, 2010, 07:54:40 AM »
tghe stript is not getting as far as the password check so there much be an issue with the query using the username

PHP Code: [Select]

$data 
mysqli_query("SELECT * FROM login_data WHERE username = $user;",$dbc);
if(empty(
$data)) {
  echo 
'<h1>Sorry, that username is unregistered.</h1>';
}


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

PHP Code: [Select]
$data mysqli_query("SELECT * FROM login_data WHERE username = '$user'",$dbc)or die(mysqli_error());
if(empty(
$data)) {
  echo 
'<h1>Sorry, that username is unregistered.</h1>';
}

zebby12

  • New Member
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Can't get data
« Reply #2 on: July 06, 2010, 05:06:05 PM »
I did that, and now I'm getting nothing, which probably means there's a problem with 'foo'. I put the statement "echo $data" in, like:
PHP Code: [Select]


$data 
mysqli_query("SELECT * FROM login_data WHERE username = '$user';",$dbc)
  or die(
mysqli_error());
echo 
"<h1>$data<h1>"//<-- here's where it is
if(empty($data)) {
  echo 
'<h1>Sorry, that username is unregistered.</h1>';
}

I even tried a SELECT * FROM login_data; statement similar to that one, which should show the whole table, but I couldn't get anything. The or die in the mysqli_connect 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.
Whoops. Is it okay if your toast is burnt?

daveismyname

  • Senior Member
  • ****
  • Posts: 204
  • Karma: +1/-0
  • PHP Helper
    • View Profile
    • Dave is my name
Re: Can't get data
« Reply #3 on: July 06, 2010, 05:17:30 PM »
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 Code: [Select]
$data mysqli_query("SELECT * FROM login_data WHERE username = '$user'",$dbc)or die(mysqli_error());
while(
$r mysql_fetch_object($data)){
    echo 
"<p>$r->username</p>";
}


If I were using mysql this would work:

PHP Code: [Select]

//connect to db
$data mysql_query("SELECT * FROM login_data WHERE username = '$user'")or die(mysqli_error());
while(
$r mysql_fetch_object($data)){
    echo 
"<p>$r->username</p>";
}

zebby12

  • New Member
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Can't get data
« Reply #4 on: July 07, 2010, 04:25:56 PM »
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.
Whoops. Is it okay if your toast is burnt?

zebby12

  • New Member
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Can't get data
« Reply #5 on: July 21, 2010, 02:59:24 PM »
Okay, here's the error:

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

echo "<h1>$data</h1>";

because I was checking if there was data.
Whoops. Is it okay if your toast is burnt?

zebby12

  • New Member
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Can't get data
« Reply #6 on: August 13, 2010, 03:31:51 PM »
Hooray, it's finally working!
Whoops. Is it okay if your toast is burnt?