Jump to content

My query is lying to me!


Canadian

Recommended Posts

I've spent the afternoon building a page that counts entries into a database.  My table "key" is an auto increment column named "count".  Here is what I am trying to do...  I think the error is in the value I am receiving from my database but I don't know why.  I will post my DB table below.

 

1.  I'm querying the database for the last entry.

 


$npn_count_query = "SELECT * FROM npn ORDER BY count DESC LIMIT 1";

$npn_count = $db->query($npn_count_query);

 

2.  For testing purposes, I'm echoing $npn_count.  For some reason I keep getting the following value for $npn_count regardless of what "count" actually is in the table.

 


Object id #2

 

3.  After that I'm using preg_match().  My goal is the following.  IF the auto increment "count" ends in 9 "do something" ELSE "do something else".

 


if (preg_match('/9$/', $npn_count)) {

do something 

} else {

do something else

};

 

 

 

For some reason even when the "count" ends in 9 in the DB table, the IF statement always defaults to the ELSE.  I'm guessing that is because my query keeps returning "Object id #2"

 

 

Can anyone tell my why I keep getting a value of "Object id #2"?

 

 

Thanks!

 

 

Here is my DB table setup script.

 


CREATE TABLE `npn` (
  `count` int(12) NOT NULL auto_increment,
  `first_name` varchar(64) NOT NULL default '',
  `last_name` varchar(64) NOT NULL default '',
  `email` varchar(128) NOT NULL default '',
  `timestamp` timestamp NULL default NULL,
  `result` char( NOT NULL default '',
  PRIMARY KEY  (`count`)
) ENGINE=MyISAM AUTO_INCREMENT=22 DEFAULT CHARSET=utf8

 

 

Link to comment
Share on other sites

When you query a database, a result is created and what is returned is a resource identifier pointing to the result. So, when you run this:

$npn_count = $db->query($npn_count_query);

 

You get a pointer to the result - NOT the result itself. You are apparently using a class to do your database functions, so you should look into what methods you should be using to extract the database results.

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.