Jump to content

[SOLVED] Will this work? Wrapped function for numrows.


matthewhaworth

Recommended Posts

Oh..and:

 

I'd have to call the class and set everything up.. or use test data, when I was hoping for a quick answer on here?

 

You want us to do the hard part of thinking it out for you, so you don't have to actually use the code you've written?

Link to comment
Share on other sites

I don't know, it just doesn't look right...but I never use ternary because it never looks right. It's too confusing. It's like using ASP style tags.

funny lol you said its wrong but then you dont know whats wrong hahahah

 

ternary gives shorter code but slower output  i guess

Link to comment
Share on other sites

Oh..and:

 

I'd have to call the class and set everything up.. or use test data, when I was hoping for a quick answer on here?

 

You want us to do the hard part of thinking it out for you, so you don't have to actually use the code you've written?

 

 

No.  I simply did not know if I was using the 'ternary' operator correctly. 

you use the class within your function so iguess it wont work

 

$query = $this->_db->query($sql);

 

you have to declare the class global if you want it that way or declare it inside your functions

 

The function is used within the class.  The $_db is a mysqli object.

Link to comment
Share on other sites

It would have been faster I bet to have tried it out by now ;) Or at least switched it to an if-else.

 

According to the manual

http://us3.php.net/ternary

 

I think it will work.

 

 

*sigh*.  I know.  I have switched to an if statement.

 

    function numrows($table, $condition)
    {
        $sql = "SELECT * FROM " . $table . " WHERE " . $condition;
        $query = $this->_db->query($sql);
        //return ($query != false) ? $query:"Error in numrows";
        if ($query != false) {
		return $query;
	}
	else
	{
		return "Error in numrows";
	}
    }

 

Thanks for the argument guys. :).

 

Note: the weird formatting accorded after I'd posted it..

Link to comment
Share on other sites

Hrmm. This is not at all how I would do this.

 

Most database interfaces you're going to use have a function for number of rows... no need to try to reinvent it. Furthermore, the SELECT * is going to be inefficient and if you're querying multiple tables you'd have to pass it in as "table1, table2, table3" or something. Why not just set something up like this:

 

mysql.php

mssql.php

oracle.php

etc...

 

then you have your main db class that includes the appropriate db class (above). If you change databases you swap out the include in that single file and move on. By doing this you can use the native mysql_num_rows function, etc.

Link to comment
Share on other sites

id rather do this, this way

 

if ($query != false) {

$result =$query;

}

else{

$result = "Error in numrows";

}

return $result ;

 

I've noticed a lot of people do that, any reason why, and weren't you the person complaining about longer processing.. that'd take longer wouldn't it?

Link to comment
Share on other sites

I think returning different types from a single function is stupid... even if PHP will let you do it. If you're returning a result set... return a result set... at the very worst you should return a result set or false... not a result set or some string you have to test for.

Link to comment
Share on other sites

id rather do this, this way

 

if ($query != false) {

$result =$query;

}

else{

$result = "Error in numrows";

}

return $result ;

 

I've noticed a lot of people do that, any reason why, and weren't you the person complaining about longer processing.. that'd take longer wouldn't it?

 

yes but i guess it is more handy when the result is in the variable

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.