SQL query does not return value

Asked

Viewed 404 times

0

Good afternoon to all.

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: http://raelcunha.com/template/

All right, I deployed the project and everything works precisely, however, when creating a query to query the BD tables, it returns what is there, but, only a record.

$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 several ways, but the reused is always the same! It shows only a record of the 5 that I have. :(

  • Do not use Mysql_, use PDO or Mysqli_

  • http://rberaldo.com.br/mysql-obsoleto-php/

  • If you run the query in your manager returns how many rows?

  • He’s just adding the last one isn’t?

  • If I run the query with echo, it returns everything normal, but using the Template class method, it returns only one @Robertofagundes

3 answers

0

Try the following on:

$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 records is something in your while that is wrong.

As everyone is speaking in PDO, below follows example of 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();
}

?>
  • Using only the print_r it returned everything, but the only detail is that using the variable as follows in the class, it does not return all the results. Only one. : ( Thanks anyway!

0

Avoid using the msql as the friend said, is 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 overwriting the variável and printing only the last in:

$tpl->USER_SHOW_TITLE 
  • What do you mean "overlapping"? It would then be a problem within the project class itself that is shown on the site above?

  • That ! For example: if your object is not printing, and you print after the loop would already appear only the last. The way I went (creating an array before printing) did not work ?

  • Raoni, unfortunately it didn’t work out, friend. I’m trying in every way, but I think the most viable way is to just abandon this system and build your own class. :(

  • Robson, we’ll sort it out ! Please post the class and a script to generate the database so I can test it here.

  • It’s very Raoni extension... :( I see it on github: https://github.com/raelgc/template/blob/master/raelgc/view/Template.php About the BD connection script, I think this is what you asked for, right? $sql["host"] = "localhost"; $sql["user"] = "USER"; $sql["pass"] = "PASSWORD"; $sql["base"] = "BANK"; $Connection = mysql_connect($sql["host"],$sql["user"],$sql["pass"]); $select_database = mysql_select_db($sql["base"], $Connection); mysql_query("SET NAMES utf8");

  • Once I get it I’ll take a look at it... the one in the bank is the script for creating the database, with tables, etc... can put in a git everything that is necessary to test ?

  • Ah yes... Now I understand. With you! I will try to assemble one here for you!

  • https://github.com/robsonlivrebr/first-stack/blob/master/startBD.sql

  • Robson I didn’t find this USER_SHOW_TITLE and the USER_SHOW_DESC in the class. But on second thought, you’re calling a class method, so if it prints something, you’d have to pass a parameter, and it wouldn’t be by = and yes in parentheses, like this: $tpl->USER_SHOW_TITLE($row['username']); If not, then I can’t help you if you don’t pass a ZIP or GIT of the entire project, because I can’t simulate it here, probably because of changes you made to the original. Or give me your Skype I help you remotely.

  • USER_SHOW_TITLE is the variable I have to put in the HTML that will be replaced by the value (=). That is: USER_SHOW_TITLE = "Hello world". In HTML, the variable {USER_SHOW_TITLE }will be placed in HTML. The "class" will replace the {USER_SHOW_TITLE } with the value = "Hello world".

  • The documentation asks to do so. This USER_SHOW_TITLE variable is customizable, so we can change it to "USER_NAME" and put a $Row['username'] as value, and returns the user name in the database.

  • So, I didn’t find this in class... unfortunately it’s a lot to read and to be able to simulate... then complicates !

  • More is well... I thank you for your help. I will continue doing tests... : D Thanks!

  • Having a while I will simulate it ! I like challenges ! I just need time ! Rs

  • Thank you Raoni... : D Thank you very much for your help. : D

  • Raoni, I killed the riddle 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 the class that runs the query without restrictions! In short, it is now working. : ) Again, thank you for your attention!

Show 11 more comments

0


I tried the riddle 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 the class that runs 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 just like that!!

Thank you all for your assistance!

Browser other questions tagged

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