Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

metalero

[Resolvido] Teste do bando de dados

Recommended Posts

Boa noite, estou testando o banco de dados para ver se está aparecendo valores, mais não estou conseguindo fazer aparecer,

 

conexao.php

 

<?
$servidor = "localhost";
$usuario = "root";
$senha = "";

$con = mysql_connect($servidor,$usuario,$senha) or die ("não foi possivel conectar ao servidor de banco de dados");
mysql_select_db("loja",$con) or die ("não foi possivel conectar ao banco");

?>

 

Teste.php

<? include "conexao.php";

$sql   = "select * from categorias";
$dados = mysql_query($sql) ;
$linha =0;

while ($linha < mysql_num_rows($dados))
{
echo mysql_result($dados,$linha, categoria);
echo "<br>";
$linha += 1;

}
?>

 

No teste.php tinha que aparecer o valores da (categoria, e so aparece isso ("; $linha += 1; } ?>), alguem pode me ajudar

Compartilhar este post


Link para o post
Compartilhar em outros sites

tenta assim:

$sql   = "select * from categorias";
$dados = mysql_query($sql) ;

while($row = mysql_fetch_assoc($dados)){
 echo $row['nome_da_coluna'] ."<br>; 

}

Compartilhar este post


Link para o post
Compartilhar em outros sites

consegui arumar faltava colocar <?php ?> estava assim <? ?>, estou com outro problema esse também não está mostrando os valores do BD

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
 <tr>
   <td colspan="2"><?php include "cabecalho.php";?></td>
 </tr>
 <tr>
   <td width="121" valign="top"><?php include "menu.php";?></td>
   <td width="629"><table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
   <td> </td>
 </tr>
 <tr>
   <td><div align="center">
   <strong>Categoria</strong>

   <? if ($acao !="") {

$sql = mysql_query ("SELECT * FROM categoria WHERE id_categoria = $id");?>

   <form id="forml" name="forml" method="post" action="<? echo $PHP_SELF ?>" >
   <table width="300" border="1" cellspacing="0" cellpadding="2" align="center">
 <tr>
   <td width="114">Categoria</td>
   <td width="186">
   <label for="textcategoria"></label>
    <input type="text" name="text_categoria" id="text_categoria" size="35" /></td>
 </tr>
 <tr>    
   <td colspan="2" align="center"><input type="submit" name="button" id="button" value="OK" style="al" /></td>
 </tr>
</table>
</form>
<? } else {?>

<table width="80%" border="1" cellspacing="0" cellpadding="0">
 <tr>
   <td width="94%">Categorias</td>
   <td colspan="2" align="center"> Ação</td>
   </tr>
   <? $sql = mysql_query ("SELECT * FROM categorias");
  while ($coluna = mysql_fetch_array($sql)) { ?>


 <tr>
   <td><? echo $coluna{categoria}?></td>
   <td width="4%"><img src="imagens/inserir.png" width="16" height="16" /></td>
   <td width="2%"><img src="imagens/x.png" width="16" height="16" /></td>
 </tr>
 <? } ?>
 <tr>
   <td colspan="3"><img src="imagens/bt_inserir.png" width="65" height="16" align="right" /></td>
   </tr>
</table>    
    </div></td>
 </tr>  
</table>
<? } ?>
</td>
 </tr>
 <tr>    
   <td colspan="2" align="center"><?php include "rodape.php";?></td>
 </tr>
</table>


</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites
esse também não está mostrando os valores do BD

 

1- veja essa linha do seu 1º post:

<? include "conexao.php";

2- veja essa linha do seu ultimo post:

while ($coluna = mysql_fetch_array($sql)) { ?>

tenta ver para que essas linhas servem..

Compartilhar este post


Link para o post
Compartilhar em outros sites

conexao.php

<?
$servidor = "localhost";
$usuario = "root";
$senha = "";

$con = mysql_connect($servidor,$usuario,$senha) or die ("não foi possivel conectar ao servidor de banco de dados");
mysql_select_db("loja",$con) or die ("não foi possivel conectar ao banco");

?>

 

Teste.php

<? include "conexao.php"; ?>
<?
$sql   = "select * from categorias";
$dados = mysql_query($sql,$con) ;

while($user = mysql_fetch_array($dados)) {

echo mysql_result($dados); //se não der serto assim troque  o "'$dados'" por "'$sql'"
echo "<br>";

}
?>

 

 

 

TENTA VER COM ESSAS PEQUENAS MODIFICAÇÕES!!!!Se NÃO DER CERTO você ME AVISA QUAL O ERRO QUE DEU!!!VLw

Compartilhar este post


Link para o post
Compartilhar em outros sites

O teste.php já consegui, só não estou conseguindo agora com esse

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
 <tr>
   <td colspan="2"><?php include "cabecalho.php";?></td>
 </tr>
 <tr>
   <td width="121" valign="top"><?php include "menu.php";?></td>
   <td width="629"><table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
   <td> </td>
 </tr>
 <tr>
   <td><div align="center">
   <strong>Categoria</strong>

   <? if ($acao !="") {

$sql = mysql_query ("SELECT * FROM categoria WHERE id_categoria = $id");?>

   <form id="forml" name="forml" method="post" action="<? echo $PHP_SELF ?>" >
   <table width="300" border="1" cellspacing="0" cellpadding="2" align="center">
 <tr>
   <td width="114">Categoria</td>
   <td width="186">
   <label for="textcategoria"></label>
    <input type="text" name="text_categoria" id="text_categoria" size="35" /></td>
 </tr>
 <tr>    
   <td colspan="2" align="center"><input type="submit" name="button" id="button" value="OK" style="al" /></td>
 </tr>
</table>
</form>
<? } else {?>

<table width="80%" border="1" cellspacing="0" cellpadding="0">
 <tr>
   <td width="94%">Categorias</td>
   <td colspan="2" align="center"> Ação</td>
   </tr>
   <? $sql = mysql_query ("SELECT * FROM categorias");
  while ($coluna = mysql_fetch_array($sql)) { ?>


 <tr>
   <td><? echo $coluna{categoria}?></td>
   <td width="4%"><img src="imagens/inserir.png" width="16" height="16" /></td>
   <td width="2%"><img src="imagens/x.png" width="16" height="16" /></td>
 </tr>
 <? } ?>
 <tr>
   <td colspan="3"><img src="imagens/bt_inserir.png" width="65" height="16" align="right" /></td>
   </tr>
</table>    
    </div></td>
 </tr>  
</table>
<? } ?>
</td>
 </tr>
 <tr>    
   <td colspan="2" align="center"><?php include "rodape.php";?></td>
 </tr>
</table>


</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

já resolvi esse problema! vlw pela ajuda

 

tenho outro problema que não estou conseguindo puxar os valores para form

 

<?php if ($_GET['acao']) {

$sql = mysql_query ("SELECT * FROM categorias WHERE id_categoria = $id");?>

   <form id="forml" name="forml" method="post" action="<?php echo $PHP_SELF ?>" >
   <table width="300" border="1" cellspacing="0" cellpadding="2" align="center">
 <tr>
   <td width="114">Categoria</td>
   <td width="186">
   <label>
    <input type="text" name="txt_categoria" id="txt_categoria" size="35" value="<?php echo @mysql_result($sql,0,categoria); ?>" />
    </label></td>
 </tr>
 <tr>    
   <td colspan="2" align="center"><input type="submit" name="button" id="button" value="OK" style="al" /></td>
 </tr>
</table>
</form>
<?php } else {?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Que tal voltar a usar o mysql_fetch_array? O mysql_result, é somente para contar quantos registros há.

Vamos fazer da seguinte forma:

PHP:

<?php if ($_GET['acao']) {

       $sql = mysql_query ("SELECT * FROM categorias WHERE id_categoria = $id");
$result = mysql_fetch_array($sql);
?>

HTML/PHP:

<input type="text" name="txt_categoria" id="txt_categoria" size="35" value="<?php echo $result['categoria']; ?>" />

Compartilhar este post


Link para o post
Compartilhar em outros sites

Coloquei mais está dando esse erro

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\loja\admin\categoria.php on line 27

 

 

Line 27

 $result = mysql_fetch_array($sql);

Compartilhar este post


Link para o post
Compartilhar em outros sites

O erro ocorre na consulta. Acho que você não declarou a variável $id.

Se declarou, revise seu select, sua conexão e veja se está tudo correto.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Tá assim não achei nada por enquanto de errado

 

 

<?php include"conexao.php"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
 <tr>
   <td colspan="2"><?php include "cabecalho.php";?></td>
 </tr>
 <tr>
   <td width="121" valign="top"><?php include "menu.php";?></td>
   <td width="629"><table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
   <td> </td>
 </tr>
 <tr>
   <td><div align="center">
   <strong>Categoria</strong>

   <?php if ($_GET['acao']) {

       $sql = mysql_query ("SELECT * FROM categorias WHERE id_categoria = $id");
       $result = mysql_fetch_array($sql);
?>

   <form id="forml" name="forml" method="post" action="<?php echo $PHP_SELF ?>" >
   <table width="300" border="1" cellspacing="0" cellpadding="2" align="center">
 <tr>
   <td width="114">Categoria</td>
   <td width="186">
   <label>
    <input type="text" name="txt_categoria" id="txt_categoria" size="35" value="<?php echo $result['categoria']; ?>" />
    </label></td>
 </tr>
 <tr>    
   <td colspan="2" align="center"><input type="submit" name="button" id="button" value="OK" style="al" /></td>
 </tr>
</table>
</form>
<?php } else {?>

<table width="80%" border="1" cellspacing="0" cellpadding="0">
 <tr>
   <td width="94%">Categorias</td>
   <td colspan="2" align="center"> Ação</td>
   </tr>
   <?php $sql = mysql_query ("SELECT * FROM categorias");
  while ($coluna = mysql_fetch_array($sql)) { ?>


 <tr>
   <td><?php echo $coluna{categoria};?></td>
   <td width="4%"><a href="?acao=Alterar$id=<?php echo $coluna[id_categoria];?>"><img src="imagens/inserir.png" width="16" height="16" /></a></td>
   <td width="2%"><img src="imagens/x.png" width="16" height="16" /></td>
 </tr>
 <?php } ?>
 <tr>
   <td colspan="3"><img src="imagens/bt_inserir.png" width="65" height="16" align="right" /></td>
   </tr>
</table>    
    </div></td>
 </tr>  
</table>
<?php } ?>
</td>
 </tr>
 <tr>    
   <td colspan="2" align="center"><?php include "rodape.php";?></td>
 </tr>
</table>



</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Justamente o que eu falei, você não declarou a variável $id, não deu um valor para ela.

Exemplo:

$id = 'variável';
echo $id;
/* Saída: variável */

Se for comente $id:

echo $id;
/* Saída: Notice: Undefined variable: id in C:\wamp\www\testes\teste6.php on line 2 */

Entendeu?

De um valor à $id antes de fazer o select.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.