RE: How can you connect models to a database manually?

How can you connect models to a database manually?

dip Team Leader Asked on June 7, 2018 in CODEIGNITER.
Add Comment
1 Answers

Using Functional php to connect to a mysql db. It comes form the php manual. Put this in your model. Call it by calling a model function the normal CI way. It will return the result array, so assign the model call to a variable in your controller.

<?php
function my_model_query(){
    //replace the function arguments with your information
    $link = mysql_connect('host_name', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    //your code here
    $query = "SELECT * FROM mytable";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result)){

        $result_array[]['your_field_1'] = $row['your_field_1'];
        $result_array[]['your_field_2'] = $row['your_field_2']; 
        //and so on

       }

  
    //send results back to controller
    return $result_array;


}//endfunction
?>
Senior Developer Answered on July 4, 2018.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.