How to put two $ row results in variable?

0

The code below gives error, it does not add the results of $row , only the comma and the period. What to do?

Code that does not work     

  mysql_select_db('cadastro',$conexao);

$sql="select * from usuario";
$resultado = mysql_query($sql) or die ("Erro: " . mysql_error());

   // Obtém o resultado de uma linha como um objeto
while($row = mysql_fetch_array($resultado))
$variavel=($row['email'].",".$row['senha']."^") ;
// quero que tudo isso que esta entre () vire uma $variavel    
echo $variavel
?>

Code that works fine

<?php
@$conexao = mysql_connect('localhost','root','');

  mysql_select_db('cadastro',$conexao);

$sql="select * from usuario";
$resultado = mysql_query($sql) or die ("Erro: " . mysql_error());

   // Obtém o resultado de uma linha como um objeto
  while($row = mysql_fetch_array($resultado))
  echo $row['email'].",".$row['senha']."";    
  echo "^";
  ?>

This is the code, so the result that it appears to me is EXAMPLE: [email protected], 123456, ^

Email and password are in my database What I want is that all this echo, this result becomes a variable

    
asked by anonymous 30.09.2015 / 06:33

2 answers

0

Hello, I do not have much time, so I'll be brief. Your error is in looping , since you stated the start but did not set an end, or the lines affected by this looping

// Obtém o resultado de uma linha como um objeto
while($row = mysql_fetch_array($resultado))
$variavel=($row['email'].",".$row['senha']."^") ;
// quero que tudo isso que esta entre () vire uma $variavel    
echo $variavel

Normally, delimiters are placed to indicate the beginning and end of the expression that is part of the given group determined by the expression of the beginning of the other various types of control structures. But for while we have: {} or : and endwhile; .

Below are 2 examples, using the 2 types of [grouping] delimiters described above.

1st {Braces}

// Obtém o resultado de uma linha como um objeto
$variavel = "";
while($row = mysql_fetch_array($resultado)){

$variavel .= $row['email'] . "," . $row['senha'] . "^";

}
echo $variavel;

Note that in this example, I instantiated before the variable $variavel out of looping , and then concatenated the values caught within while using the .=

2º Using : and endwhile;

// Obtém o resultado de uma linha como um objeto
$variavel = "";
while($row = mysql_fetch_array($resultado)):

$variavel .= $row['email'] . "," . $row['senha'] . "^";

endwhile;
echo $variavel;

Another observation is, relative to the @ at the beginning of the connection expression, that should be kept in mind, considering that it suppresses errors returned in that expression, and also in relation to mysql . I leave below some good sources.

#

What is the function of '@' at the start of expressions in PHP

Error Tracking Operators - PHP.net

Why should not we use functions of type mysql_ *?

If I could have explained you better.

    
30.09.2015 / 16:27
1

Well, judging from what you've described, you're pretty much a beginner. The first thing you should always do when you are developing is to enable the errors (just disable them when the application is on production server, for example). It seems to me that you fall into one of these situations:

Fields with empty values

$row['email'] and / or $row['password'] are empty strings;

Array indices or fields in the table do not exist

password and / or email are not existing indexes, however you do not get any error because the directive display_errors in its php.ini is set to Off / 0, thus disabling errors;

If this is the case, this justifies the fact that the value of $variavel is always ",^" .

Steps that will help identify why this behavior:

Begin by inserting this portion of code immediately at the beginning of the script:

<?php 

error_reporting(E_ALL);
ini_set('display_errors', 'On');

The above snippet will make any error appear. Then also remove the @ from the $conexão variable. As long as this (@) operator will suppress the error message of the current expression (if any). It would only justify its existence there if there was immediately after the function call a or die('ERROR!'); or or exit('ERROR!'); as part of the whole expression. But of course, here is almost certain (not to say 100%) that there is no error with the connection because if it were the expression mysql_query($sql) or die ("Erro: " . mysql_error()); would have printed the error regardless of how their error control policies are. >     

30.09.2015 / 07:53