Olá,
Estou com problema tentando fazer uma consulta ao MySQL utilizando como chave de busca o id, chave em uma tabela. O número do registro é fornecido em um formulário e o código PHP o lê e faz a consulta, só que não retorna nenhum resultado. Eu faço a mesma consulta no prompt do mysql e tudo ok. Seguem o código php (gerado pelo dreamweaver) e a tabela na qual a consulta é feita:
Código PHP
<?php require_once('../Connections/testarbd.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$id_rsSelect =1;
/*$id_rsSelect = $_POST['busca'];
*/echo ($id_rsSelect);
if (isset($_POST['busca'])) {
$id_rsSelect = $_POST['busca'];
}
mysql_select_db($database_testarbd, $testarbd);
$query_rsSelect = sprintf("SELECT * FROM noticia WHERE noticia.id=%s", GetSQLValueString($id_rsSelect, "int"));
$rsSelect = mysql_query($query_rsSelect, $testarbd) or die(mysql_error());
$row_rsSelect = mysql_fetch_assoc($rsSelect);
$totalRows_rsSelect = mysql_num_rows($rsSelect);
//-----------------------------------------------------
// imprimir resultado da consulta
for ($i=0; $row=mysql_fetch_row($rsSelect);$i++) {
foreach($row as $key => $value)
print ("<p> $key $value<br />");
} echo "</p>";
// ---------------------------------------------------
Tabela a ser consultada
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| data | text | NO | | | |
| destaque | varchar(255) | NO | | | |
| manchete | text | NO | | | |
| autor | varchar(255) | NO | | | |
| foto | varchar(255) | NO | | | |
| noticia | text | NO | | | |
| tipo | varchar(255) | NO | | | |
+----------+--------------+------+-----+---------+----------------+
Publicidade
|
|
Page 1 of 1
MySQL + PHP + DW consulta não retorna o resultado
#2
Posted 01 May 2008 - 09:19 AM
QUOTE(alanperes @ 29/4/2008 23:04:52) <{POST_SNAPBACK}>
Olá,
Estou com problema tentando fazer uma consulta ao MySQL utilizando como chave de busca o id, chave em uma tabela. O número do registro é fornecido em um formulário e o código PHP o lê e faz a consulta, só que não retorna nenhum resultado. Eu faço a mesma consulta no prompt do mysql e tudo ok. Seguem o código php (gerado pelo dreamweaver) e a tabela na qual a consulta é feita:
Código PHP
<?php require_once('../Connections/testarbd.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$id_rsSelect =1;
/*$id_rsSelect = $_POST['busca'];
*/echo ($id_rsSelect);
if (isset($_POST['busca'])) {
$id_rsSelect = $_POST['busca'];
}
mysql_select_db($database_testarbd, $testarbd);
$query_rsSelect = sprintf("SELECT * FROM noticia WHERE noticia.id=%s", GetSQLValueString($id_rsSelect, "int"));
$rsSelect = mysql_query($query_rsSelect, $testarbd) or die(mysql_error());
$row_rsSelect = mysql_fetch_assoc($rsSelect);
$totalRows_rsSelect = mysql_num_rows($rsSelect);
//-----------------------------------------------------
// imprimir resultado da consulta
for ($i=0; $row=mysql_fetch_row($rsSelect);$i++) {
foreach($row as $key => $value)
print ("<p> $key $value<br />");
} echo "</p>";
// ---------------------------------------------------
Tabela a ser consultada
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| data | text | NO | | | |
| destaque | varchar(255) | NO | | | |
| manchete | text | NO | | | |
| autor | varchar(255) | NO | | | |
| foto | varchar(255) | NO | | | |
| noticia | text | NO | | | |
| tipo | varchar(255) | NO | | | |
+----------+--------------+------+-----+---------+----------------+
Estou com problema tentando fazer uma consulta ao MySQL utilizando como chave de busca o id, chave em uma tabela. O número do registro é fornecido em um formulário e o código PHP o lê e faz a consulta, só que não retorna nenhum resultado. Eu faço a mesma consulta no prompt do mysql e tudo ok. Seguem o código php (gerado pelo dreamweaver) e a tabela na qual a consulta é feita:
Código PHP
<?php require_once('../Connections/testarbd.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$id_rsSelect =1;
/*$id_rsSelect = $_POST['busca'];
*/echo ($id_rsSelect);
if (isset($_POST['busca'])) {
$id_rsSelect = $_POST['busca'];
}
mysql_select_db($database_testarbd, $testarbd);
$query_rsSelect = sprintf("SELECT * FROM noticia WHERE noticia.id=%s", GetSQLValueString($id_rsSelect, "int"));
$rsSelect = mysql_query($query_rsSelect, $testarbd) or die(mysql_error());
$row_rsSelect = mysql_fetch_assoc($rsSelect);
$totalRows_rsSelect = mysql_num_rows($rsSelect);
//-----------------------------------------------------
// imprimir resultado da consulta
for ($i=0; $row=mysql_fetch_row($rsSelect);$i++) {
foreach($row as $key => $value)
print ("<p> $key $value<br />");
} echo "</p>";
// ---------------------------------------------------
Tabela a ser consultada
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| data | text | NO | | | |
| destaque | varchar(255) | NO | | | |
| manchete | text | NO | | | |
| autor | varchar(255) | NO | | | |
| foto | varchar(255) | NO | | | |
| noticia | text | NO | | | |
| tipo | varchar(255) | NO | | | |
+----------+--------------+------+-----+---------+----------------+
Problema resolvido. Eu não estava entendendo como utilizar o método "mysql_fetch_assoc()" retirei-o do código e tudo funcionou como eu queria.
[]s
Alanperes
Share this topic:
Page 1 of 1
Similar Topics
| Topic | Forum | Started By | Stats | Last Post Info | |
|---|---|---|---|---|---|
|
Canadá diz que não irá regular transmissões de rádio e TV online
|
Notícias |
Notícias
|
|
|
|
Nao consigo utilizar gradient como cor
|
Illustrator |
Akn
|
|
|
|
IE 7 Beta 2 Preview não suporta hack
|
Web Standards |
TiTi Celestino
|
|
|
|
parar animação
não esta dando certo |
Flash & ActionScript |
Only Dust
|
|
|
|
Ajuda com Slides
não para e exibe todos de uma só vez... |
Flash & ActionScript |
Ictusweb
|
|
|
|
Jpg não abre com IE7
Qualquer navegador abre o site, só o IE7 não. |
Dreamweaver |
Felipe Alrean
|
|
|
|
Publicando Dados do PHPBB
Como faço pra pegar os dados e colocar em uma pagina personalizada? |
PHP |
Marcelo Freitas
|
|
|
|
Apache PHP+MySQL
como? |
Outros |
PHPG
|
|
Publicidade
|
|

Help













