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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.