Jump to content

Is there anything wrong with this code ?


jd2007

Recommended Posts

<?php
if (!$n) {
  include("search.php");
  echo"No search term given.";
}

$name=explode(" ", $n);
$namelen=count($name);

if ($namelen==0) {
  include("connectdb.php");
  $query = "SELECT *
	FROM Searching WHERE tags LIKE '%".$name."%'";
  $result = mysql_query($query);
  while ($record = mysql_fetch_assoc($result)) {
	while (list($fieldname, $fieldvalue) = each ($record)) {
		echo $fieldname.": <B>".$fieldvalue."</B><BR>";
	}
	echo "<BR>";
}
} else {}
?>

Link to comment
https://forums.phpfreaks.com/topic/59053-is-there-anything-wrong-with-this-code/
Share on other sites

<?php
if (!$_REQUEST['n']) {
  include("search.php");
  echo"No search term given.";
  exit();
}

$name=str_replace(' ','%', $_REQUEST['n']);
$namelen=count($name);

if ($namelen > 0) {
  include("connectdb.php");
  $query = "SELECT *
	FROM Searching WHERE tags LIKE '%".$name."%'";
  $result = mysql_query($query);
  while ($record = mysql_fetch_assoc($result)) {

	//while (list($fieldname, $fieldvalue) = each ($record)) {
	//	echo $fieldname.": <strong>".$fieldvalue."</strong><br />";
	//}

	echo "<br />";
}
} else {}
?>

 

I have commented out the the while loop as that part is a little confusing...

list() only works on numerical arrays and mysql_fetch_assoc returns associative arrays (fieldnames as keys instead of numbers). it also relys on there being the correct fields in the correct order from the mysql_fetch_assoc.

 

Now as I said mysql_fetch_assoc returns an associative array and we need a numerical one so that must change to mysql_fetch_row and the select query should  select only the fields you want in the correct order...

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.