Sessions and MySQL

Posted on 2002-03-08
Last Modified: 2008-03-06
I'm making an online app where the my client wants to save a bunch of data into a database including the actual session id then later they'll use this information for something else they're doing.

In any case, I have one part in my program that I can't seem to figure out why.  Here's a snippet


$sessionid = session_id();

$queryresult = mysql_query("SELECT * FROM energybook WHERE session='$sessionid'");
$booknumrows = mysql_num_rows($queryresult);

if($booknumrows == 0)
   print "HEY!, I found a one!.  Let's update the existing record";
   Print "Let's insert the record now";

The problem here is, I know I have an existing record in my table but the thing is not finding it.  Ok, I know that if I close my browser and reopen, i'll get another session but all this occurs within the SAME session so I don't believe that's the problem.

I'm stumped.
Question by:KABOOM
LVL 32

Expert Comment

ID: 6851017
Why this line:

if($booknumrows == 0)

Shouldn't the number of rows be 1 if you should update?

if($booknumrows == 1)


Expert Comment

ID: 6851288
Batalf is Correct, you will only get the message if no records are found.


Accepted Solution

dkjariwala earned 50 total points
ID: 6852382
You don't need to select all the fields by using SELECT * , you can just do like

select ANY_ONE_FIELD_NAME from energybook where session='$sessionid';

This would be more efficient.

Also, make sure that query is executed successfully.

$result = mysql_query("select ANY_ONE_FIELD_NAME from energybook where session='$sessionid') or die('Can not execute query. ' . mysql_error());

   print "Record exists !!";

LVL 40

Expert Comment

by:Richard Quadling
ID: 6864129
You could also use COUNT(*) AS RecordCount, rather than actually getting the entire record set back and then counting the rows.

If you are only ever expecting 0 or 1 record, then OK, but if you COULD get thousands of results, then COUNT(*) SHOULD be faster!



$sessionid = session_id();

$queryresult = mysql_query("SELECT COUNT(*) AS RecordCount FROM energybook WHERE session='$sessionid'");
if (isset($queryresult))
 $booknumrow = mysql_fetch_array($queryresult);

 if($booknumrow[0] > 1) print "Oh dear! Too many SOMEHOW!";
 else if ($booknumrow[0] == 1) print "HEY!, I found a one!.  Let's update the existing record";
 else Print "Let's insert the record now";


Richard Quadling.

Expert Comment

ID: 6864150
Oops I missed on that ,

My friend Richard is DEFINATELY correct.
Infect I shall say use count(*) only. If you just want to check if record is there or not.


Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question