# Can't get data

**URL:** <https://www.phphelp.com/t/cant-get-data/11556>\
**Category:** MySQL database\
**Created:** [June 27, 2010, 10:12pm UTC](https://www.phphelp.com/t/cant-get-data/11556 "2010-06-27T22:12:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zebby12](https://www.phphelp.com/user_avatar/www.phphelp.com/zebby12/32/184_2.png) [@zebby12](https://www.phphelp.com/u/zebby12)\
**Post date:** [June 27, 2010, 10:12pm UTC](https://www.phphelp.com/t/cant-get-data/11556/1 "2010-06-27T22:12:04Z")

</div>

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 HomeBack 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?

---

<div class="post-metadata">

**Author:** ![daveismyname](https://www.phphelp.com/user_avatar/www.phphelp.com/daveismyname/32/79_2.png) [@daveismyname](https://www.phphelp.com/u/daveismyname)\
**Post date:** [July 1, 2010, 12:54pm UTC](https://www.phphelp.com/t/cant-get-data/11556/2 "2010-07-01T12:54:40Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![zebby12](https://www.phphelp.com/user_avatar/www.phphelp.com/zebby12/32/184_2.png) [@zebby12](https://www.phphelp.com/u/zebby12)\
**Post date:** [July 6, 2010, 10:06pm UTC](https://www.phphelp.com/t/cant-get-data/11556/3 "2010-07-06T22:06:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![daveismyname](https://www.phphelp.com/user_avatar/www.phphelp.com/daveismyname/32/79_2.png) [@daveismyname](https://www.phphelp.com/u/daveismyname)\
**Post date:** [July 6, 2010, 10:17pm UTC](https://www.phphelp.com/t/cant-get-data/11556/4 "2010-07-06T22:17:30Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![zebby12](https://www.phphelp.com/user_avatar/www.phphelp.com/zebby12/32/184_2.png) [@zebby12](https://www.phphelp.com/u/zebby12)\
**Post date:** [July 7, 2010, 9:25pm UTC](https://www.phphelp.com/t/cant-get-data/11556/5 "2010-07-07T21:25:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zebby12](https://www.phphelp.com/user_avatar/www.phphelp.com/zebby12/32/184_2.png) [@zebby12](https://www.phphelp.com/u/zebby12)\
**Post date:** [July 21, 2010, 7:59pm UTC](https://www.phphelp.com/t/cant-get-data/11556/6 "2010-07-21T19:59:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zebby12](https://www.phphelp.com/user_avatar/www.phphelp.com/zebby12/32/184_2.png) [@zebby12](https://www.phphelp.com/u/zebby12)\
**Post date:** [August 13, 2010, 8:31pm UTC](https://www.phphelp.com/t/cant-get-data/11556/7 "2010-08-13T20:31:51Z")

</div>

Hooray, it’s finally working!
