Welcome Guest, Not a member yet? Register   Sign In
How to do straight-up MySQL?
#1

[eluser]tokyotech[/eluser]
In my model, how can I do straight-up MySQL? I need to do an advanced query and the active record model is getting in my way. After I do this MySQL query, how can I return an object version of the resulting array just like the active record model?

Thanks in advance.
#2

[eluser]kyleect[/eluser]
http://ellislab.com/codeigniter/user-gui...eries.html
#3

[eluser]CI Coder[/eluser]
[quote author="tokyotech" date="1258854187"]In my model, how can I do straight-up MySQL? I need to do an advanced query and the active record model is getting in my way. After I do this MySQL query, how can I return an object version of the resulting array just like the active record model?

Thanks in advance.[/quote]

Code:
$query = $this->db->query('YOUR VERY COMPLICATED QUERY');
Hope that helps.
#4

[eluser]tokyotech[/eluser]
This works. Thanks to you both.

Is there a reason to using active record? I heard it allows you to interface with any database language, but what other database languages are out there besides MySQL and MSSQL? Don't you need ASP to use MSSQL?
#5

[eluser]jedd[/eluser]
[quote author="tokyotech" date="1258960716"]
Is there a reason to using active record? I heard it allows you to interface with any database language, but what other database languages are out there besides MySQL and MSSQL? Don't you need ASP to use MSSQL?[/quote]

Postgres and Oracle are two obvious examples. You don't need ASP to talk to Microsoft SQL.

From the [url="/user_guide/database/active_record.html"]user guide[/url] :
Quote:... allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.

Beyond simplicity, a major benefit to using the Active Record features is that it allows you to create database independent applications, since the query syntax is generated by each database adapter. It also allows for safer queries, since the values are escaped automatically by the system.

This is all covered in the user guide.
#6

[eluser]CI Coder[/eluser]
There are many other database systems besides those 2 -- SQLite, Postgres, Oracle etc.; just google "database systems". No you don't need to have ASP to access MSSQL server databases, you can have PHP installed on IIS if you have a windows server, or even Apache with PHP on Windows.

The active record paradigm is actually a common denominator interface that CI uses so that you use the same way of addressing the database, no matter if it's MySQL or MSSQL or other system. Active record also has other features that come in handy when you just don't have time to spend on details like, for instance, escaping strings when you save them to the database.

Anyway, this can become more clear if you read the database class documentation.

All the best.
#7

[eluser]tokyotech[/eluser]
Yes, I read the documentation a long time ago.

I guess my problem is that I've never even touched any other database language. Should I just stick with MySQL or is there a compelling reason to switch over to any of those other database languages you've listed?
#8

[eluser]CI Coder[/eluser]
Well, the common language for all of them is SQL. I recommend learning it to anyone who works with computers. Databases have become such an important part of all sorts of applications that knowing SQL is as important as knowing how to talk in many situations. If you know SQL you will do well no matter what the database system is. Differences between SQL implementations are minor things that can be learned as you go, but pretty much all of them support standard SQL syntax.

As far as CI is concerned, I would suggest learning how to use Active Record precisely because, for the most part, you do not have to worry about what database system is underneath -- results are the same whether they come from MySQL, MSSQL, Oracle or any other system. And then Active record has a way to let you run your hand written queries (I showed it above) in case its automation is not enough for you.

If you know MySQL and feel comfortable with it, I see no reason to switch. MySQL is free, very stable and in fact is the most used database system. I don't think there is any hosting company out there who doesn't offer MySQL. Combined with PHP it makes for a killer combo; and with CI it's lethal if you just look at it :-)




Theme © iAndrew 2016 - Forum software by © MyBB