Jump to content

[SOLVED] return a single value


cleary1981

Recommended Posts

Hi if a was querying a database to find a single result how would I output the result?

 

<?php
require "config.php";

$model = $_REQUEST['model']);
$sql = "SELECT mod_desc FROM module WHERE type='$model'";
$result = mysql_query($sql) or trigger_error(mysql_error());
$row = mysql_fetch_object($result);
$description=$row->description;
echo $description;
?>

 

The result should only be one item. How do I get this to output from the script?

Link to comment
https://forums.phpfreaks.com/topic/112745-solved-return-a-single-value/
Share on other sites

You almost have it, but you're trying to get the item by the wrong name:

<?php
<?php
require "config.php";

$model = $_REQUEST['model']);
$sql = "SELECT mod_desc FROM module WHERE type='$model'";
$result = mysql_query($sql) or trigger_error(mysql_error());
$row = mysql_fetch_object($result);
$description=$row->mod_desc;  //this has to match what you have in the select statement.
echo $description;
?>

 

Ken

Your selecting mod_desc then trying to display description. Your code ought be....

 

<?php

require "config.php";

if (isset($_REQUEST['model'])) {
 $model = mysql_real_escape_string($_REQUEST['model']);
 $sql = "SELECT mod_desc FROM module WHERE type='$model'";
 if ($result = mysql_query($sql)) {
   if (mysql_num_rows($result)) {
     $row = mysql_fetch_object($result);
     echo $row->mod_desc;
   }
 }
}

?>

From what I understand, you want to return only a single result even if there are more results of the same criteria. I'm not really sure how you want to organize it; but there are plenty of SQL statements that can organize it by ID etc..

 

<?php
$model = $_REQUEST['model'];

if(isset($model)) {
  $query = sprintf("SELECT `mod_desc` FROM `module` WHERE `type` = '%s' LIMIT 1", mysql_real_escape_string($model));
  $result = mysql_query($query) or trigger_error(mysql_error());

  if(mysql_num_rows($query) > 0) {
   while($obj = mysql_fetch_object($query)) {
     print $obj->mod_desc; // You were calling it by the wrong name, dammit those 2 got there before me >.<
   }
  }
  else {
    print 'No results!';
}
?>

 

I'm not entirely sure what you are looking for, if this is not acceptable. Please try to make yourself clearer.

kenrbnsn your answer works best for me but i still have one problem. when the result passes back into my javascript I get "undefined". Any ideas why?

 

Heres my code

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>TES - Pricing System</title>
<link rel="stylesheet" type="text/css"
href="style.css" />

</script>
<script type="text/javascript" 
        src="prototype.js"></script>
<script type="text/javascript"
        src="scriptaculous.js"></script>
<script language="javascript" src="list.php"></script>
<script type="text/javascript" src="getdimensions.js"> </script>
<script type="text/javascript" src="text-utils.js"> </script>



<script type="text/javascript">


var request = null;

try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
	request = null;
	}

}
}
if (request == null)
alert("Error creating XMLHttp Request!");

window.onload = function() {
     new Draggable('object', {snap:25} );
fillCategory();
}

function get_description() {
var model = document.getElementById("model").value;


var url = "lookupdescription.php?model=" + escape(model);

request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}

function updatePage() {
if (request.readyState ==4) {
var modeldescription = request.reponseText;
var x = document.getElementById("description");
replaceText(x, modeldescription);
}
}

</script>


</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

<div id="container">
<div id="top">
	<h1>TES - Pricing System (Design Mode)</h1>
</div>

<div id="leftSide">
	<fieldset>
	<legend>Module</legend>
	<FORM name="drop_list">
	<label for="type">Type</label>
	<div class="div_texbox">
		<SELECT  NAME="Category" onChange="SelectSubCat();" >
		<Option value="">Select Type</option>
		</SELECT>
	</div>
  		<label for="mod_named">Model</label>
    		<div class="div_texbox">
		<SELECT id="model" NAME="SubCat" onChange="get_description();">
		<Option value="">Select Model</option>
		</SELECT>
	</div>
	<fieldset>
	<legend>Description</legend>
	<span id="description">description goes here</span>

	</fieldset>
	<label for="mod_name">Name</label>
    		<div class="div_texbox">
    			<input name="username" type="text" class="username" id="username" value="" />
	</div>
	<div class="button_div">
		<input name="Submit" type="button" value="Generate" class="buttons" onClick="generate_module()"/>
	</div>
	</form>
	</fieldset>

	<fieldset>
	<fieldset>
	<legend>Delete</legend>
	</fieldset>
	<fieldset>
	<legend>Generated Module</legend>
	<img id="object">
	</fiedset> 
	</fieldset>	
</div>


<div id="content">
	<div id="drop_area">

	</div>
</div>  

  	<div class="clear"></div>
</div>

</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.