debuggng help my simple login system(like 300 lines code)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

debuggng help my simple login system(like 300 lines code)

Post by gautamz07 »

guys i really followed this set of tutorials online , its to help me understand the oops concept :

heres the link for one of the videos https://www.youtube.com/watch?v=uF-L7ympvfw

now i typed out all the code as that guy had done in the video , but i am still getting errors , so i kind of need some help here , for someone to check my code .

i know debugging help is usually not encouraged on forum , but i would really really be grateful and would buy u a beer if you could help me debug this :

heres the link to what i've done on git : https://github.com/gautamz07/oopslogin

now in mysql you will have to create a table :
with the following attributes :

id fname lname email

and don't forget to change the db name in index.php if you run the code.

now one other difference you need to know if you do this is the guy in the video has used mysqli where as i am using mysql , i know its a crime to be using it , but still for this demo thinggi its ok :D ..


Thank you .

Gautam .
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: debuggng help my simple login system(like 300 lines code

Post by Celauran »

gautamz07 wrote:but i am still getting errors
Can you be more specific?
gautamz07 wrote:now one other difference you need to know if you do this is the guy in the video has used mysqli where as i am using mysql
Actually, you're using a mix of both which is responsible for at least some errors.

Because you're using mysql_ functions, you need a call to mysql_select_db in your connect method.

You're assigning to $this->$id instead of $this->id. You're also assuming $row will be an array even though it could very well be a string.

I have also noticed you're using parentheses in a number of places where you should be using braces (example).
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: debuggng help my simple login system(like 300 lines code

Post by Celauran »

With the bugs mostly addressed, I feel I should mention that the tutorial video looks to be quite dated. Using var suggests PHP 4, which is ancient. You have methods without visibility, old school require instead of proper autoloading, singletons, etc. It's a bit of a mess.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: debuggng help my simple login system(like 300 lines code

Post by gautamz07 »

Thanks for the pointers ! i'll implement them .

i get the following error .

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\oopslogin\class.table.php on line 27
() ()
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: debuggng help my simple login system(like 300 lines code

Post by gautamz07 »

:( :'( ok , but i just want to still do this . jst so i can get a hang of oops . please .
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: debuggng help my simple login system(like 300 lines code

Post by Celauran »

gautamz07 wrote:Thanks for the pointers ! i'll implement them .

i get the following error .

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\oopslogin\class.table.php on line 27
() ()
Have you fixed all the issues I highlighted? I had it working locally with only those fixes.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: debuggng help my simple login system(like 300 lines code

Post by gautamz07 »

I updated the code celauran ! , for the last pointer , is't the $obj variable , converted to a array on line 48 ??

if ($this->results) {
$obj = mysqli_fetch_assoc($this->results);
}

i mean here : https://github.com/gautamz07/oopslogin/ ... se.php#L48
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: debuggng help my simple login system(like 300 lines code

Post by Celauran »

Sometimes. What if $this->results is empty or false?
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: debuggng help my simple login system(like 300 lines code

Post by gautamz07 »

so what can i do to correct that celauran .

check (is_array($row)) before the foreach loop ???
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: debuggng help my simple login system(like 300 lines code

Post by Celauran »

You could initialize an empty array instead of a string for the default state, or you could check if $row is an array before trying to iterate over it.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: debuggng help my simple login system(like 300 lines code

Post by gautamz07 »

yes ! but that's a logical thing , i'll implement it ! , i still can't get this code to run , i'll post a updated git link .
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: debuggng help my simple login system(like 300 lines code

Post by Celauran »

gautamz07 wrote:i still can't get this code to run
OK, where's it failing now?
gautamz07 wrote:i'll post a updated git link .
That's the nice thing about Git: you don't have to.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: debuggng help my simple login system(like 300 lines code

Post by gautamz07 »

updated ! made the is_array change too .

https://github.com/gautamz07/oopslogin/ ... le.php#L28

celauran , i mean i'll push my changes to git repo ! :)
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: debuggng help my simple login system(like 300 lines code

Post by gautamz07 »

in te index.php file u'll see the following code :

Code: Select all

<?php
	include('./class.database.php');
	include('./class.table.php');
	include('./user.class.php');

	$dbo = database::getInstance();
	$dbo->connect('localhost', 'root', '' , 'zakoo');
	$user = new user();
	
	// $temp = array(1);
	$user->load('1');

	echo "($user->fname) ($user->lname)";
?>
now i don't get any errors , but the output in the browser i get is :

() ()

i don't know why really .

if you run the code you will know .

Thanks .
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: debuggng help my simple login system(like 300 lines code

Post by Celauran »

You're still using () instead of {} in a lot of your strings. This leads to bad queries, which are failing silently and reporting no results. Since you're only populating your users' properties if you get results, what you're seeing is expected behaviour.
Post Reply