Do not receive query results from the database after submitting the search form

I have two files, file.php amd get_xml.php . I can easily get the echo table information in both php files, but when I want to use the search form to request data, send it to get_xml.php , I do not get any results.

The following is a working example along with all the rows as a reference to what is in the actual MySQL table.

Now here is the code itself:

file.php

 <?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> 

get_xml2.php

 <?php require("db_access.php"); function parseToXML($htmlStr) { $xmlStr=str_replace('<','&lt;',$htmlStr); $xmlStr=str_replace('>','&gt;',$xmlStr); $xmlStr=str_replace('"','&quot;',$xmlStr); $xmlStr=str_replace("'",'&#39;',$xmlStr); $xmlStr=str_replace("&",'&amp;',$xmlStr); return $xmlStr; } $name=$_POST['name']; $address=$_POST['address']; $type=$_POST['type']; // 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 = sprintf( "SELECT name, address, type FROM markers WHERE name = '%s' AND address = '%s' AND type = '%s'", mysql_real_escape_string($name), mysql_real_escape_string($address), mysql_real_escape_string($type) ); $result = mysql_query($result); if($result == false) { die(mysql_error() . "<br />\n$query"); } if(mysql_num_rows($result) == 0) { user_error("No rows returned by:<br />\n$query"); } 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 'type="' . parseToXML($row['type']) . '" '; echo 'lat="' . $row['lat'] . '" '; echo 'lng="' . $row['lng'] . '" '; echo '/>'; } // End XML file echo '</markers>'; ?> 

When you do a search, as you see, you will get the following:

 > Query was empty SELECT name, address, type FROM markers WHERE name = 'The Melting Pot' AND address = '14 Mercer St, Seattle, WA' AND type = 'restaurant' 

However, when I change the code in xml to

 $query = "SELECT * FROM markers WHERE 1"; $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } 

xml happily retrieves ALL data from the database, as shown here .

Thank you for your time!

+4
source share
2 answers

You are likely to kick yourself for it.

 $result = mysql_query($result); 

Must be

 $result = mysql_query($query); 

Don't you hate this ?:-)

+5
source

you have an error:

 $result = mysql_query($result); 

instead

 $result = mysql_query($query); 
+3
source

All Articles