Jose
Jose

Reputation: 87

Query is always returning 1

The query is always returning 1 and not the id I need. I want the id of 'intervaloshorarios' table which has its own 'cita'. For example, (8:00 am to 9:00 am) for the date: 27/08/1988. I need (8:00 am to 9:00) id. I am returning the query using print_r($expression). How can I get the id that I need?

Database

Database

Model function

public function get_idintervalo($idCitas) {

     $query = $this->db->query('SELECT intervaloshorarios.idIntervaloHorario FROM intervaloshorarios INNER JOIN citas '
             . 'ON intervaloshorarios.idIntervaloHorario = citas.idIntervaloHorario '
             . 'WHERE citas.idCitas = ' . $idCitas . ';');


        return print_r($query);
    }

Controller(trying to get id into hidden field)

 $query = $this->Intervalos_Model->get_idintervalo($idCitas);

  $crud->change_field_type('idIntervaloHorario', 'hidden', $query);

Error (Sihas answer)

Localhost say: error while adding

Print_r($query)(Sihas answer )

1CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 1 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 1 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.5.5-10.1.21-MariaDB [server_version] => 50505 [stat] => Uptime: 1043645 Threads: 1 Questions: 58810 Slow queries: 0 Opens: 257 Flush tables: 1 Open tables: 203 Queries per second avg: 0.056 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 3645 [warning_count] => 0 ) [result_id] => mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 1 [type] => 0 ) [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => [row_data] => )

Upvotes: 0

Views: 1089

Answers (3)

Shihas
Shihas

Reputation: 814

Try this code:

public function get_idintervalo($idCitas)
{
    $this->db->select('intervaloshorarios.idIntervaloHorario');
    $this->db->from('intervaloshorarios');
    $this->db->join('citas','intervaloshorarios.idIntervaloHorario = citas.idIntervaloHorario');
    $this->db->where('citas.idCitas',$idCitas);
    $query = $this->db->get();
    $result =  $query->row('intervaloshorarios.idIntervaloHorario');

    echo $result; exit();
}

Upvotes: 0

Nanne
Nanne

Reputation: 64409

It would be good to look up some of the manual pages for these functions

print_r outputs the variable (it echoes it) and returns TRUE, so returning in this manner will always return true (which could be interpreted as '1' if you for instance print that result). So return your result by actually returning it, not by echoeing it to the output!

Then you should look at your query. You are not saying what you use, but assuming you use PDO, look at the manual of pdo's query

Return Values
PDO::query() returns a PDOStatement object, or FALSE on failure.

You will not get your result, you'll get a PDOStatement. You can itterate over this statement if you like, but it is not a string in itself. What you could do, for instance, is something like this:

$stmt = $pdo->query('SELECT name FROM users');
while ($row = $stmt->fetch())
{
    echo $row['name'] . "\n";
}

I have taken this from the following page: phpdelusions, please take a look (not affiliated, just a random google hit :) )

edit: you seem to be using mysqli. The same thing sort-of applies: check out the manual!

These 2 links should get you started http://php.net/manual/en/mysqli.query.php and http://php.net/manual/en/mysqli.examples-basic.php .

You could, for instance, do this:

 $result = $this->db->query($q);
 $array = $result->fetch_assoc();
 //now $array is an array with your results. var_dump it to inspect

Upvotes: 0

Bahadur Singh Deol
Bahadur Singh Deol

Reputation: 821

You just create alias of table and use Mysql In for multiple Id and pass array of Id

public function get_idintervalo($idCitas = array) 
{
    $query = SELECT in.idIntervaloHorario FROM intervaloshorarios in INNER JOIN citas ct ON in.idIntervaloHorario = ct.idIntervaloHorario WHERE citas.idCitas IN ( $idCitas );

$result = $this->db->query($query);
}

Upvotes: -1

Related Questions