Array key filled with an expression coming from an SQL query

Asked

Viewed 74 times

0

What exactly does that mean?

Array
(
[0] => Array
    (
        [MAX(nr_ficha)] => 13
    )
)

I gave one print on the screen and saw that my foreign key (nr_ficha) is receiving value 13 (which yes, is the last value of the table), but I do not understand the rest of the code nor why it does not work.

Notice: Array to string Conversion in C: xampp htdocs TESTE_BANCADA Models Teste.php on line 16

Fatal error: Uncaught Pdoexception: SQLSTATE[23000]: Integrity Constraint Violation: 1452 Cannot add or update a Child Row: a Foreign key Cont straint fails (teste__bancada.teste, CONSTRAINT nr_ficha_teste FOREIGN KEY (nr_ficha) REFERENCES cab_teste (nr_ficha)) in C: xampp htdocs TESTE_BANCADA Models Teste.php:18 Stack trace: #0 C: xampp htdocs TESTE_BANCADA Models Teste.php(18): PDO->exec('INSERT INTO tes...') #1 C: xampp htdocs TEST_BANCADA Controllers processa_testeq.php(30): Teste->cadastrar_teste(Array, '4', '4', '1') #2 {main} thrown in C: xampp htdocs TESTE_BANCADA Models Teste.php on line 18

The function I used was this:

public function busca_ficha(){
$conexao = Database::getConnection();

$select="SELECT MAX(nr_ficha) FROM cab_teste";

  $busca = $conexao->query($select);
  $nr_ficha = $busca->fetchAll(PDO::FETCH_ASSOC);

  return $nr_ficha;
}

And I called her that:

$ficha = new Cabecalho();
$nr_ficha = $ficha->busca_ficha();

Somebody help me, Please.

  • Because it’s the return of MySQL you can name the field with AS, would look like this: $select="SELECT MAX(nr_ficha) AS apelido FROM cab_teste";. The AS may also be implied, thus: $select="SELECT MAX(nr_ficha) apelido FROM cab_teste";.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site (when).

1 answer

1

What the mechanism of communication with the database you used takes the result of the query and puts in a array associative (note that even though people think that PHP is object-oriented, much of its library encourages the use of good old array associative and does not use a class). So each element of that array has a key and value. What interests you most is the value, but to get to it you need the key with a name and this name is the name of the column in the database. In this case you used an expression to get the column and did not give a name, so it uses the expression as name, which is what came from the database.

If you want it to have a name, put it on query, thus:

SELECT MAX(nr_ficha) AS ULTIMO FROM cab_teste

I put in the Github for future reference.

What will return the result:

Array
(
[0] => Array
    (
        [ULTIMO] => 13
    )
)

But something tells me you’re doing something you shouldn’t. If two customers do the same they will receive the same number and if you register something based on it will have duplicity.

  • You say you use the same foreign key value? This would not be possible, because the primary key of the other table is auto increment, so whenever the daughter line receives a value, it will be higher than the penultimate of the mother line.

  • And this Array remains a backbone in the project. I didn’t just want to print it, I wanted to pass it to the foreign key. It doesn’t happen at all.

  • Good, but that wasn’t my question. Thank you, your answer at least helped me understand where I’m going wrong.

  • No, I didn’t say primary key, foreign, none of that, I’m talking about array associative. If you do not understand what is a ask a question (check before if you already have on the site). I have no idea what you’re up to, but it seems like the wrong solution, and I would rethink all of this. In fact the question asked is another and that’s what I answered :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.