Solved

Sessions and MySQL

Posted on 2002-03-08
5
207 Views
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

<?PHP
session_start();

$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";
else
   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.
Thanks,
KABOOM
0
Comment
Question by:KABOOM
5 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 6851017
Why this line:

if($booknumrows == 0)

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

if($booknumrows == 1)



0
 
LVL 5

Expert Comment

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



0
 
LVL 5

Accepted Solution

by:
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());

if(mysql_num_rows($result)>0)
   print "Record exists !!";

JD
0
 
LVL 40

Expert Comment

by:RQuadling
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!

e.g.


<?PHP
session_start();

$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";
 }
?>


Regards,

Richard Quadling.
0
 
LVL 5

Expert Comment

by:dkjariwala
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.

JD
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now