Jump to content

Database Object Problem


davidmyers

Recommended Posts

For convenience and efficiency purposes I'm changing my MySQL database to store PHP Objects as opposed to tons of individuals variables. In converting the user/login system I've come across a really weird problem. I can register a new user to the site, but only the first one in the database will work. I can register others and they get put in the database correctly, but for some reason the app doesn't see them or even recognize that they exist. Only the first entry in the database will work as it's supposed to. For instance, if I drop the first database entry, the second one (now first) will begin to work.

 

In most cases, I'm pulling all Object entries from the database and looping through them with foreach() to find the specific one I want.

 

I've just come off of an 8 hour coding spree and my brain is just shot at the moment. I really think I just need a new pair of eyes. I'm sure that it's something simple, but I can't see it atm. I'm attaching the User class file which handles login, registration, and anything that I think would be affecting this.

 

Thanks for the help.

 

[attachment deleted by admin]

Link to comment
Share on other sites

In most cases, I'm pulling all Object entries from the database and looping through them with foreach() to find the specific one I want.

 

^^^ That's an extremely bad idea. You do realize that php, being a parsed, tokenized, and interpreted language, is about 100 times slower at scanning through and finding information in a database table then if you use the database engine to do this.

 

You have missed the point of why databases exist. To let you efficiently - store, find, retrieve, and manipulate information. By storing serialized object values, all you can do is store/retrieve and since your scheme doesn't have the ability to find and retrieve specific row(s), you must then retrieve everything and scan through it using some slow parsed, tokenized, interpreted php code.

 

You are also not looping through anything in your login function, so of course only the first one in the table works.

Link to comment
Share on other sites

I was working under the assumption that doing things using this object model would increase overall speed. I really only grab all of the database entries in a couple of places and so far haven't seen a performance hit, however I haven't had a chance to see it scale yet. In most cases I'm able to just grab a specific object and instantly have all of the information I need. Doing things this way has dramatically decreased the amount of code that I have because I'm not constantly having to do many database queries and set tons of individual variables.

 

Another thing, when using an object class model, it defies logic to continually break up and recreate objects every time you need to store or retrieve the information. You're essentially wasting all of your time destroying and then re-creating the same objects over and over again when you can instead just store the objects and instantly store and retrieve any changes.

 

I do understand what you're saying, but I'm just not sure that you're correct about the performance. I was initially planning on doing some benchmarking to see what kind of a difference I would experience, but in the end decided that I didn't need to take the time as I was sure it would be an improvement. Perhaps I'll need to do that now.

 

Could  you please explain to me why it's not looping in my login function? foreach() is supposed to loop through an array, but apparently it's not?

 

Thanks again for the help.

Link to comment
Share on other sites

So I've found a solution to my problems on my own.

 

Firstly, I've decided to make a compromise on the object model/database storage solution. For the user system, I'm going to have columns for UserID, Username, PassHash, and Object. This way I keep the advantages of storing all of the objects I create but I can utilize MySQL in the areas that it's best, like searching the database for login confirmation.

 

I also figured out the issues I was having with foreach(). foreach() is supposed to automatically loop through an array according to the PHP manual description. But for some reason it wasn't looping. It would only run once, regardless of what variables I changed. I've changed the code and logic to work with a for loop and the class now works perfectly.

 

This is the for loop that has replaced the foreach() logic in the User class:

for($x = 0; $x < $QueryRowCount; $x++)
{
$Temp = mysql_fetch_assoc($LoginQuery);
$TempObject = unserialize($Temp[Object]);
$TempUser = $TempObject->GetValue(Username);
$TempPass = $TempObject->GetValue(PassHash);
if($TempUser == $NewUsername && $TempPass == $NewMD5Hash)
{
	$Success = 1;
	break;
}
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.