1

I am executing the following commands in my model php in Codeigniter:

$sql=mysql_query("select name from reseller_domain where name='".$name."'");
$numrows=mysql_num_rows($sql);
if($numrows!=null || $numrows==0)
{
  return 'f';
}
else
{
 return 't';
}

I am not getting any result whatsoever. However, the same query is running perfectly fine in phpmyadmin.

apomene
  • 14,282
  • 9
  • 46
  • 72

4 Answers4

2

1. mysql_* is deprecated so avoid using that...
2. Are you executing the mysql_connect() command first?
3. Your code is prone to sql injection


4. Since you are using codeIgniter, Im assuming you want to use the CodeIgniter framework to access your database

Have a look here on how to use codeigniter to access your database. In short:
  1. Configure CI by adding your database settings
  2. Try this in CodeIgniter

     $sql = "select name from reseller_domain where name = ?";
     $this->db->query($sql, array($name));
     if($query->num_rows() == 0){
         return 'f';
     }else{
         return 't';
     }
    
Community
  • 1
  • 1
Krimson
  • 7,386
  • 11
  • 60
  • 97
1

try that and see what error you got.

  $sql=mysql_query("select name from reseller_domain where name='".$name."'") or die(mysql_error());

edit:

why you check like that ?

try check like that:

   if($numrows==0)
    {
echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • I want to check whether there are any rows returned or not. If number of rows returned is 0, then I want to return a false. – user3126593 Mar 12 '14 at 15:26
  • try that iff it works `select name from reseller_domain where name='Yourname'` give yourname with an exisitng name and see if it work. – echo_Me Mar 12 '14 at 15:29
  • even tht doesn't return a result.. I tried using an existing name from the table. – user3126593 Mar 12 '14 at 15:33
1

Edit: Seriously my man, you need to start using the CI DB class, this is not a good practice. First of all, try getting your query in a var and exit it to the screen. Then see what the actual query gets and try this in your PHPMYADMIN. I suspect errors with the $name var.

===========================================================================

It looks like you didn't select a database.

Try using Code Igniters DB class?

If you don't already do,

$this->db->query('select name from reseller_domain where name='".$name."');

Must say though you'd better read the docs about it, it might help you a little further. If you did the query with the CI Db class you might want to see the actually outputted query with:

exit($this->db->last_query()); //yes exit isn't good, not even for debugging. But it works.

Then see what goes wrong inside of your query.

Chilion
  • 4,380
  • 4
  • 33
  • 48
  • I tried the CI DB way too. Even that didn't return any result. :( – user3126593 Mar 12 '14 at 15:29
  • Then try to put your query in a var first. $query = 'SELECT etc' and then exit $query;. Look at the query and use it in PHPMYADMIN. What result you get? – Chilion Mar 12 '14 at 15:44
0

It is possible that your DB setup in codeigniter, and where you were in phpmyadmin are using a different default schema.

Try it with: "select name from database_name.reseller_domain where name='".$name."'"

iMakeWebsites
  • 587
  • 3
  • 10
  • Nope, there is only one database that I am connecting to. It is working fine for all other queries. Even for this query, it worked yesterday. But today it suddenly stopped working. I am unable to figure out the issue. Anyway, I tried your suggestion. Still not result. – user3126593 Mar 12 '14 at 15:23
  • Hey buddy, thanks for your advise. Your answer is not fully correct but it was very close. I had to use a 'tank_auth' prefix to the table name. And the issue was solved. Thanks a lot! – user3126593 Mar 13 '14 at 07:40