SQL query does not return value

0

Good afternoon everyone.

I have a question about what could be going wrong in a database query in which I use templates in PHP. Based on this project: link

$sql = mysql_query("SELECT * FROM users ORDER BY idS DESC") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
$tpl->USER_SHOW_TITLE = $row['username'];
$tpl->USER_SHOW_DESC = substr($row['profi_desc'],0,125).'...';
}

I have tried in many ways, but the reusltado is always the same! It shows only a record of the 5 that I have. :

    
asked by anonymous 05.10.2017 / 21:17

3 answers

0

I got the charade of this CLASS! Since it returns only one value, I can use the addFile method of the class itself. That is: addFile ($ str, $ directory); With this, I can include a PHP file outside of the class that executes the query without restrictions! Then, in the HTML template, just create the variable you used in AddFile.

$tpl->addFile('CONTEUDO','site/tpl/html/index.php');

in the index.html file put the variable:

{CONTEUDO}

And that's it!

Thank you all for the assistance!

    
15.10.2017 / 18:50
0

Avoid using msql as your friend said, it's obsolete.

Try this way

    $sql = mysql_query("SELECT * FROM users ORDER BY idS DESC") or die(mysql_error());
    while($row = mysql_fetch_array($sql)){
    $i[] = $row;
    }
    foreach($i as $v) {
    $tpl->USER_SHOW_TITLE = $v['username'];
    $tpl->USER_SHOW_DESC = substr($v['profi_desc'],0,125).'...';
}

If not, make sure you are not overlapping variável and printing only the last one on:

$tpl->USER_SHOW_TITLE 
    
05.10.2017 / 21:37
0

Try the following:

$sql = mysql_query("SELECT * FROM users ORDER BY idS DESC") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
print_r ($row);
}

With this we are asking to print the contents of the array, come all the records is something in your while that is wrong.

As everyone is talking about PDO, below is a sample selection using PDO:

$sql = $connection->query("SELECT * FROM users ORDER BY idS DESC");
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
    $tpl->USER_SHOW_TITLE = $row['username'];
    $tpl->USER_SHOW_DESC = substr($row['profi_desc'],0,125).'...';
}

Your PDO connection to the bank:

<?php
$dsn = 'mysql:dbname=tua_tabela;host=localhost';
$user = 'usuario_mysql';
$password = 'senha_mysql';

try {
    $connection = new PDO($dsn, $user, $password);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>
    
05.10.2017 / 21:27