evanluke Posted January 17, 2010 Share Posted January 17, 2010 I am trying to use the value of a URL variable called "service" to determine what field in my database table gets outputted in my query. I'm defining the variable first with $theservice = $_GET['service']; I use this variable to set my query $query = mysql_query("SELECT * FROM segments WHERE name <> 'County' ORDER BY ${theservice} DESC", $dbcon) or die("unable to query database"); Then I attempt to output the result of the query here <?php while($segment = mysql_fetch_assoc($query)): ?> <set value='<?=$segment['$theservice']?>' link='profilecharacteristics.php?segid=<?=$segment['id']?>' /> <?php endwhile; ?> <?php mysql_data_seek($query, 0); ?> The last part doesn't work. The value parameter for the "set" tag outputs blank for every returned record. Any help would be most appreciated. The full code for the page is below: <?php $theservice = $_GET['service']; if(empty($service)) $service = "books"; $servstring = "serv".$service; // connect to and query database, then loop through result and output each segment xml attribute $dbcon = mysql_connect("localhost", "natur83_jeffco", "247w0rship") or die("unable to connect to database"); mysql_select_db("natur83_jcplcc", $dbcon) or die("unable to select database"); // test to be sure that material_$service_p is a field in the database $parameter_check = mysql_query("SELECT * FROM segments", $dbcon) or die("unable to query database"); $num = mysql_num_fields($parameter_check); for($i = 0; $i < $num; $i++) { if(mysql_field_name($parameter_check, $i) == "${theservice}") $clean = true; } ($clean == true) or die("invalid parameter"); $query = mysql_query("SELECT * FROM segments WHERE name <> 'County' ORDER BY ${theservice} DESC", $dbcon) or die("unable to query database"); if(mysql_num_rows($query) == 0) $caption = "No customer segments meet your criteria, please try again"; ?> <?php header("Content-type: text/xml"); ?> <?= '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; ?> <chart caption='<?=$caption?>' showLegend='0' showBorder='0' bgColor='FFFFFF' yAxisName='Number of Checkouts' showValues='0' useRoundEdges='1' numVisiblePlot='4'> <categories> <?php while($segment = mysql_fetch_assoc($query)): ?> <category label='<?=$segment['name']?>' /> <?php endwhile; ?> <?php mysql_data_seek($query, 0); ?> </categories> <dataset seriesName='Number of Checkouts' color='53d61f'> <?php while($segment = mysql_fetch_assoc($query)): ?> <set value='<?=$segment['$theservice']?>' link='profilecharacteristics.php?segid=<?=$segment['id']?>' /> <?php endwhile; ?> <?php mysql_data_seek($query, 0); ?> </dataset> </chart> Thanks, Evan Link to comment https://forums.phpfreaks.com/topic/188800-getting-url-variable-into-query-output/ Share on other sites More sharing options...
Catfish Posted January 17, 2010 Share Posted January 17, 2010 try: <set value='<?$segment[$theservice]?>' link='profilecharacteristics.php?segid=<?=$segment['id']?>' /> Link to comment https://forums.phpfreaks.com/topic/188800-getting-url-variable-into-query-output/#findComment-996741 Share on other sites More sharing options...
evanluke Posted January 17, 2010 Author Share Posted January 17, 2010 It worked! Thanks for the quick response! Link to comment https://forums.phpfreaks.com/topic/188800-getting-url-variable-into-query-output/#findComment-996743 Share on other sites More sharing options...
evanluke Posted January 17, 2010 Author Share Posted January 17, 2010 Ran into another problem...when the url variable gets defined as a string with dashes (e.g. serv_child_book-audio-largeprint), my script returns a unable to query database error. Link to comment https://forums.phpfreaks.com/topic/188800-getting-url-variable-into-query-output/#findComment-996763 Share on other sites More sharing options...
Catfish Posted January 18, 2010 Share Posted January 18, 2010 might need someone a bit more capable in mysql to help you with that one. afaik, $theservice is a field name from the database yes? i did a quick googling and found this page: http://www.doctrine-project.org/jira/browse/DC-70 where someone says you cant have dashes in field names. if the mysql manual were as well laid out as the php one i'd probably know a lot more about mysql than i do. Link to comment https://forums.phpfreaks.com/topic/188800-getting-url-variable-into-query-output/#findComment-997062 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 mysql_error(); its a wonderful thing and will tell you exactly why the query isnt working $query = mysql_query("SELECT * FROM segments WHERE name <> 'County' ORDER BY ${theservice} DESC", $dbcon) or die("unable to query database.".mysql_error()); Link to comment https://forums.phpfreaks.com/topic/188800-getting-url-variable-into-query-output/#findComment-997070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.