Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not receiving query results from database after search form submitted

Tags:

php

mysql

I have two files, file.php amd get_xml.php. I can, without a problem echo table information in both php files, but when I want to use a search form to query data, send it off to get_xml.php, I get no results.

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

Now here's 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 can see you will get this:

> 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 the xml to

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

The xml happily retrieves ALL data in from the database, as seen here.

Thanks for your time!

like image 271
pufAmuf Avatar asked Feb 23 '23 14:02

pufAmuf


2 Answers

You'll probably kick yourself for this.

$result = mysql_query($result);

Should be

$result = mysql_query($query);

Don't ya hate that? :-)

like image 133
Mech Avatar answered Feb 26 '23 19:02

Mech


you have a mistake:

$result = mysql_query($result);

instead of

$result = mysql_query($query);
like image 39
yoavmatchulsky Avatar answered Feb 26 '23 21:02

yoavmatchulsky