0

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] => )

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Jose
  • 87
  • 1
  • 8
  • if you want to fetch records from the table then you have to write like this: `$query = $this->db->query("Your select query")->row_array();` for one record OR `$query = $this->db->query("Your select query")->result_array();` for multiple records. – Nidhi May 24 '17 at 12:01
  • Thanks @Nidhi but i have tried that too and it's not working... – Jose May 24 '17 at 13:58
  • Write `echo $this->db->last_query()` before return statement and execute that query in mysql and check results return or not. – Nidhi May 25 '17 at 03:46
  • I have solved this question with another question. Thanks anyway, I will answer below. – Jose May 25 '17 at 07:16
  • Does this answer your question? [getting the value of the single field output using the codeigniter active record](https://stackoverflow.com/questions/16954107/getting-the-value-of-the-single-field-output-using-the-codeigniter-active-record) – mickmackusa Nov 20 '20 at 00:06

3 Answers3

0

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
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • Thank you! i am new with php and mysql and you're helping a lot! I have modified the function and it is throwing error: Message: Call to undefined method CI_DB_mysqli_result::fetch(). `public function get_idintervalo($idCitas) { $query = $this->db->query('SELECT ... INNER JOIN citas ....' . 'WHERE ...); while ($row = $query->fetch()) { echo $row['intervaloshorarios.idIntervaloHorario'] . "\n"; } }` – Jose May 24 '17 at 07:08
  • This will turn into a big question - answer - question - answer thread, and that is a bad format for this site. You should train yourself a bit in finding out things using the manual (no offence meant, but it will really help). In this case the issue is you are not using `PDO`, but you seem to be using `mysqli`. I will add a quick example for you. – Nanne May 24 '17 at 07:26
  • Thank you! i will try that! – Jose May 24 '17 at 08:29
0

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();
}
Shihas
  • 814
  • 15
  • 44
  • Thanks for your code! it is throwing add error, so i can't add anything. I don't understand why... – Jose May 24 '17 at 07:27
  • Ok i will add the error message above. It is in Spanish but it's throwing: 'Localhost say: Error occurred while adding' – Jose May 24 '17 at 07:32
  • Are you getting any value in the hidden field `idIntervaloHorario` ? – Shihas May 24 '17 at 07:34
  • I have done print_r($query) to see what is happening, I think it is gettin 1 again. I will add the print above. – Jose May 24 '17 at 07:40
  • you please don't print `$query`. Check the updated answer to see the value in the query. – Shihas May 24 '17 at 07:43
  • instead of `$query->row('intervaloshorarios.idIntervaloHorario');` try `$query->result('intervaloshorarios.idIntervaloHorario');` also. – Shihas May 24 '17 at 07:48
  • How can I do that? i am using your `echo $query; exit();` but nothing is shown, i'm sorry for my ignorance... – Jose May 24 '17 at 07:50
  • why you echo `$query` ? Please echo `$result` – Shihas May 24 '17 at 07:52
  • `$query->result('intervaloshorarios.idIntervaloHorario');` is throwing Message: Class 'intervaloshorarios.idIntervaloHorario' not found and `echo $result; exit();` doesn't show anything... – Jose May 24 '17 at 07:55
  • Are you sure about the class definition and all while `extends`? – Shihas May 24 '17 at 07:59
  • Yes, i'm sure, with row it doesn't show class error but with result. What do you mean with while extends? class definition is correct. – Jose May 24 '17 at 08:05
  • with `row` are getting any value in `$result`? – Shihas May 24 '17 at 08:06
  • I have checked all and everything is right, with the code above i click on the controller and nothing is showing (blank page), but if I use `return $query->result('intervaloshorarios.idIntervaloHorario');` it's throwing class error – Jose May 24 '17 at 08:13
  • add `echo $this->db->get_compiled_select(); exit();` before this `$query = $this->db->get();` line and run the query in your database to make sure the query is correct. – Shihas May 24 '17 at 08:17
  • Ok! this query is shown now: `SELECT `intervaloshorarios`.`idIntervaloHorario` FROM `intervaloshorarios` JOIN `citas` ON `intervaloshorarios`.`idIntervaloHorario` = `citas`.`idIntervaloHorario` WHERE `citas`.`idCitas` = '2'` – Jose May 24 '17 at 08:21
  • Did you run that query in your database and see the result? – Shihas May 24 '17 at 11:05
  • Yes, I have tried too and it throws the expected result, so I don't know why it's not working on model and controller. – Jose May 24 '17 at 11:20
  • In the above code can change that `$query->row('intervaloshorarios.idIntervaloHorario');` to `$query->result_array('intervaloshorarios.idIntervaloHorario');` and use `print_r($result);` instead of `echo $result` – Shihas May 24 '17 at 11:23
  • I tried that before and throws next error: `Severity: Error Message: Class 'intervaloshorarios.idIntervaloHorario' not found` – Jose May 24 '17 at 11:40
-1

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);
}
Eduard Uta
  • 2,477
  • 5
  • 26
  • 36
Bahadur Singh Deol
  • 753
  • 2
  • 6
  • 17
  • I have done this query now and it is returning array to string conversion: `get_idintervalo($idCitas = array())` `$query = "SELECT intervaloshorarios.idIntervaloHorario FROM intervaloshorarios INNER JOIN citas " . "ON intervaloshorarios.idIntervaloHorario = citas.idIntervaloHorario " . "WHERE citas.idCitas IN (' " . $idCitas . " ') "; ` `$result = $this->db->query($query); return $result->result();` – Jose May 24 '17 at 06:51
  • Please refer this https://stackoverflow.com/questions/19565118/notice-array-to-string-conversion-in-php – Bahadur Singh Deol May 24 '17 at 08:41