0

Iam trying to get some data from my table record_notas in mysql, i've been using this same syntax to get data rom another tables without error. Note: i used query in workbrench and it works This is my function:

function getRecordNotas($dni){
    global $basemy;
    $db = conectodb_pdo($basemy);
    $record = array();
    try{
        $stmt = $db->prepare("SELECT maestria,cod_curso,ciclo,periodo,curso,creditos,nota FROM record_notas WHERE dni ='" . $dni . "';");
        $db->beginTransaction();
        $stmt->execute();
        $i = 0;
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $record[$i]['maestria'] = $row['maestria'];
            $record[$i]['codigoCurso'] = $row['cod_curso'];
            $record[$i]['ciclo'] = $row['ciclo'];
            $record[$i]['periodo'] = $row['periodo'];
            $record[$i]['curso'] = $row['curso'];
            $record[$i]['creditos'] = $row['creditos'];
            $record[$i]['nota'] = $row['nota'];
            $i++;
        }
        $stmt = null;
        $db = null;
    }
    catch(Exception $e){
        echo "Failed: " . $e->getMessage();
    }
    $jsondata = json_encode($record, JSON_PRETTY_PRINT);

    return $jsondata;
    }

Using function i tried to print lenght but i get 0

$getNotas = json_decode(getRecordNotas(anynumber));
$countNotas= count($getNotas);

Also tried json_encode to print in console but i get false but if i change values in function. For example

$record[$i]['maestria'] = $row['maestasdsafdafdssdria'];
            $record[$i]['codigoCurso'] = $row['cod_asdsafdafdssdcurso'];
            $record[$i]['ciclo'] = $row['ciclasdsafdafdssdo'];
            $record[$i]['periodo'] = $row['periasdsafdafdssdodo'];
            $record[$i]['curso'] = $row['curasdsafdafdssdso'];
            $record[$i]['creditos'] = $row['crediasdsafdafdssdtos'];
            $record[$i]['nota'] = $row['nasdsafdafdssdota'];
            

and use $getNotas = json_encode(getRecordNotas(anynumber)) to print in console i get lenght i should get but obiously with null because row doesnt match.

This is what i get when i json_encode and print in console result

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Have you checked that `$dni` contains what you expect? You say it works in Workbench, but that must be using a fixed value for $dni. So maybe it's different here...you need to verify that. Also have you tried to var_dump($row) within the loop to check the real content? – ADyson Feb 01 '22 at 17:01
  • 1
    P.S. You're entirely missing the point of prepared statements, because you're not using **parameters** with it. Your code is still vulnerable to SQL injection and also to silly syntax errors. Check out some examples of how to _correctly_ use prepared statements and parameters with PDO, at https://phpdelusions.net/pdo and [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – ADyson Feb 01 '22 at 17:03
  • using the function i pass a value gaven for another function extracting `$dni` from database, also tried using a `number` and `string` within ''. value i tried exists in database – user8282183292 Feb 01 '22 at 17:06
  • If you're sure `$dni` contains a valid value when you run it, then maybe it's a different problem - have you debugged the `$row` data too, as I mentioned? – ADyson Feb 01 '22 at 17:13
  • But yes, as the just-nominated duplicate points out, all you're doing is recreating the same data structure as the database already outputs. The looping is entirely redundant. Switch to just encoding the result of fetchAll() and you might find your problem disappears anyway. – ADyson Feb 01 '22 at 17:15
  • changed `WHERE dni ='" . $dni . "';` to `WHERE dni = :dni;` then `$stmt->execute(['dni'=>$dni]); ` as example you linked `how to prevent sql injection` and it worked – user8282183292 Feb 01 '22 at 17:17
  • @ADyson nope. Because, first, there is no such issue "a number being treated as a string by the database, due to the quote-marks" and also because of that fake array of nulls clearly indicating that database returned all rows. a null result simply means json_encode() failed – Your Common Sense Feb 01 '22 at 17:40

0 Answers0