altinnovation2 Posted July 11, 2011 Share Posted July 11, 2011 Hi everyone, I'm trying to create a filter that requests data from a table and outputs it into xml. I've been introduced to the $query = "SELECT something FROM table WHERE some value" however I'm confused as to how I can do that when the request from the cient side is something like: BAR, in DOWNTON for date JULY 11, where entrance is LESS THAN 10. I have a simple code that I'm trying to get to work with an html page with no luck. I've searched google for two days straight but found nothing concretely for a begginer like me. If you have the time and/or some examples that you can point me to, I'd be thankful. Here's what I have: $query = "SELECT * FROM events WHERE Location='downtown'"; $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } Thanks a lot! Link to comment https://forums.phpfreaks.com/topic/241676-php-query-filter-with-multiple-options-mysql/ Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 You are trying to parse a string into understandable data. That's a tough one. You're going to have to run some logic codes on that string. I wouldn't bother. There might be some API's out there for it though. Link to comment https://forums.phpfreaks.com/topic/241676-php-query-filter-with-multiple-options-mysql/#findComment-1241258 Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 Sorry, reflecting on what I just said.. You could search for pieces that you understand. Say you expect bar or key words like LESS THAN. But I still wouldn't do it, too much margin for error. Link to comment https://forums.phpfreaks.com/topic/241676-php-query-filter-with-multiple-options-mysql/#findComment-1241261 Share on other sites More sharing options...
altinnovation2 Posted July 11, 2011 Author Share Posted July 11, 2011 Hmmm, I tried searching for this on google, but the problem is that I don't know what the name for what I'm trying to do is. Do you know of any api's? Link to comment https://forums.phpfreaks.com/topic/241676-php-query-filter-with-multiple-options-mysql/#findComment-1241525 Share on other sites More sharing options...
altinnovation2 Posted July 11, 2011 Author Share Posted July 11, 2011 Alright, here's what I tried. I have some code below that retrieves all data from my table. Now, in the textbox, let's say that I input "bar". I want it to send a query to mysql to display all rows (in an xml format) which TYPE = bar. Here's some code below. <html> <body> <?php $username="****"; $password="*****"; $database="****"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM markers"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); ?> <form action="get_xml2.php" method="post"> Name: <input type="text" name="Name"><br> Address: <input type="text" name="Address"><br> Type: <input type="text" name="Type"><br> <input type="Submit"> </form> <table border="0" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif">Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Address</font></th> <th><font face="Arial, Helvetica, sans-serif">Type</font></th> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"Name"); $f2=mysql_result($result,$i,"Address"); $f3=mysql_result($result,$i,"Type"); ?> <tr> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> </tr> <?php $i++; } ?> And here is get_xml2.php <?php require("db_access.php"); function parseToXML($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr); $xmlStr=str_replace('"','"',$xmlStr); $xmlStr=str_replace("'",''',$xmlStr); $xmlStr=str_replace("&",'&',$xmlStr); return $xmlStr; } $Name=$_POST['Value1']; $Address=$_POST['Value2']; $Type=$_POST['Value3']; // Opens a connection to a MySQL server $connection=mysql_connect (localhost, $username, $password); if (!$connection) { die('Not connected : ' . mysql_error()); } // Set the active MySQL database $db_selected = mysql_select_db($database, $connection); if (!$db_selected) { die ('Can\'t use db : ' . mysql_error()); } // Select all the rows in the markers table $query = "SELECT Name,Address,Type FROM markers WHERE Name = 'Value1' AND Address = 'Value2' AND Type = 'Value3'"; $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } header("Content-type: text/xml"); // Start XML file, echo parent node echo '<markers>'; // Iterate through the rows, printing XML nodes for each while ($row = @mysql_fetch_assoc($result)){ // ADD TO XML DOCUMENT NODE echo '<marker '; echo 'name="' . parseToXML($row['name']) . '" '; echo 'address="' . parseToXML($row['address']) . '" '; echo 'lat="' . $row['lat'] . '" '; echo 'lng="' . $row['lng'] . '" '; echo 'type="' . $row['type'] . '" '; echo '/>'; } // End XML file echo '</markers>'; ?> Here is a working example http://tinyurl.com/6a76aek As you can see, entering anything will return an empty xml file. Link to comment https://forums.phpfreaks.com/topic/241676-php-query-filter-with-multiple-options-mysql/#findComment-1241545 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.