Forum Moderators: coopster

Message Too Old, No Replies

mysql fetch assoc($result) doesnt work

         

nil111

11:19 am on Jul 23, 2008 (gmt 0)

10+ Year Member



thanks.here's my question.
here I get the msg:

".......Got paintings for this category!";

but then I get:

"data cannot be retrived from the resultset";

Please tell me why I cant display the values in $result.

<!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>Gallery</title>
<link rel="stylesheet" type="text/css" href="css/common.css">
<link href="vista_toolbar/style.css" media="all" rel="stylesheet"
type="text/css" />
<style type="text/css">
<!--
body {
background-image: url(images/back.jpg);
background-color: #F0E3C1;
background-repeat: repeat-x;
}
-->
</style></head>

<body class="twoColFixRtHdr">
<br>
<div id="container">
<div><?php @include_once("main.php");?></div>

<div id="mainContent">
<?php
global $gname;
global $result;
$gname= $_GET['x'] ;
echo"the category is............... $gname";

$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'dba';

$database = 'gallery';
$table = 'paintings';

if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database");
if (!mysql_select_db($database)) die("Can't select database");

$result=mysql_query("select p_painting_artistid from paintings where p_painting_categoryid='$gname1'") or die("MYSQL Returned: " . mysql_error());

if($result==false)
{
echo "Sorry there aren't any available paintings for this category";
}
else
{
echo ".......Got paintings for this category!";
}
?>

</div>
<br class="clearfloat" />
</div>


<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150" height="265" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30" align="center" valign="middle" bgcolor="#CCCCCC"><strong>Artists</strong></td>
</tr>

<?php
//just to check whether I can retrive data from the $result

if(!mysql_fetch_assoc($result))
{
echo "data cannot be retrived from the resultset";
}
else
{
echo "data can be retrived from the result set";
}
?>


<?php
while($row = mysql_fetch_assoc($result))
{
?>
blabla
<tr>
<td><a href="artist.php">Artist <?php echo $row['p_painting_artistid'] ?></a></td>
</tr>
<?php } ?>

<?php @include_once("ui_footer.php");?>

</body>


</html>

nick279

11:26 am on Jul 23, 2008 (gmt 0)

10+ Year Member



$result=mysql_query("select p_painting_artistid from paintings where p_painting_categoryid='$gname1'") or die("MYSQL Returned: " . mysql_error());

check $gname1

eelixduppy

5:59 am on Jul 24, 2008 (gmt 0)



Yup, most likely an issue with your query. I would just echo out the query to the browser and see what it contains. If it looks OK try running it through MySQL manually instead of through the script to see if it returns any results.

Also, don't forget that

$gname1
might contain potentially harmfull syntax so you should always escape these query variables with mysql_real_escape_string [php.net] for safety.

nil111

7:54 am on Jul 24, 2008 (gmt 0)

10+ Year Member



yea thanks everyone i figured (with help ;) ) the solution.
I'v used,
$gname= $_GET['x'] ;

yet used,

$gname1 in the query.

;) wow that was hard to find. thanks all!