Welcome Guest, Not a member yet? Register   Sign In
simple_query question
#1

[eluser]williamparry[/eluser]
Hi,

I am trying to run a query where the database checks to see if an email address has already been registered.

What do I need to do here?

Code:
$this->db->simple_query("SELECT id FROM users WHERE emailAddress = '$emailAddress'");

For it to return true or false or the number of rows? Do I wrap that into a variable and then use rows()?
#2

[eluser]xwero[/eluser]
Code:
$query = $this->db->query("SELECT count(*) tel FROM users WHERE emailAddress = ?",array($emailAddress));
$row = $query->row();
return ($row->tel == 1)?true:false;
// or if you want to return the number
return $row->tel;
#3

[eluser]williamparry[/eluser]
Great thanks! So var tel is the count of * ?

I now get:

"Unable to access an error message corresponding to your field name."

Code:
$rules['emailAddress']    = "required|valid_email|callback_checkEmailAddress";

Do I add another:

Code:
$fields['emailAddress']    = 'Email Address';
#4

[eluser]xwero[/eluser]
tel is an alias of the count(*) function so you won't have to do $row->count(*), i don't think it will work.

To make a callback function you have to define the message.
Code:
function _checkEmailAddress($str) // start with an underscore to make function invisible for the routing
{
   $query = $this->db->query("SELECT count(*) tel FROM users WHERE emailAddress = ?",array($emailAddress));
   $row = $query->row();
   if ($row->tel == 0)
   {
      $this->validation->set_message('_checkEmailAddress', 'The %s field doesn't exist in our database');
      return false;
   }
   else
   {
      return true;
   }
}
// to add to the rules
$rules['emailAddress']    = "required|valid_email|callback__checkEmailAddress"; // two underscores
#5

[eluser]Derek Allard[/eluser]
The Active Record class in the SVN (link) has a count_all_records() in it.
Code:
$this->db->where('emailAddress', $emailAddress);
echo $this->db->count_all_results('users');
// Produces an integer, like 1
#6

[eluser]williamparry[/eluser]
Thanks for the replies.

I updated the active record class with that file and it gave:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_DB_mysql_driver::$_count_string

Filename: database/DB_active_rec.php

Line Number: 752
#7

[eluser]Seppo[/eluser]
Did you update all your files to SVN revision? I think you are missing, in this case, MySQL driver new version.
#8

[eluser]Derek Allard[/eluser]
Yeah, Seppo is correct, but its my fault... I should have told you. Assuming you're using mysql, grab this.
#9

[eluser]williamparry[/eluser]
Where can I download all SVN files? I'm getting:

Fatal error: Class 'CI_DB' not found in DB_driver.php on line 31
#10

[eluser]Pascal Kriete[/eluser]
Follow the instructions found here.




Theme © iAndrew 2016 - Forum software by © MyBB