Jump to content

Little help needed here. PHP/SQL


Samusz

Recommended Posts

Here's my code snippet.

 if(preg_match("/^[  a-zA-Z1-42]+/", $_POST['name'])){
  $name=$_POST['name'];
  $namer=$name;
  $result = mysql_query("select `resource` from resources where `resource_id` =" .$namer. "ORDER BY `resource_id` ASC ");
    echo "<br/>The following nations have " .$namer. " as a resource\n";

  //-query  the database table
  $sql="SELECT  id, nation_id, ruler, resource1, resource2 FROM TS1 WHERE resource1 LIKE '%" . $name .  "%' OR resource2 LIKE '%" . $name ."%'";
  //-run  the query against the mysql query function
  $result=mysql_query($sql);
  //-create  while loop and loop through result set
  while($row=mysql_fetch_array($result)){
          $ID=$row['id'];
	  $Ruler=$row['ruler'];

  //-display the result of the array
  echo "<ul>\n";
  echo "<li>" . "<a  href=\"searcher.php?id=$ID\">" .$Ruler. "</a></li>\n";
  echo "</ul>";
  }
  }
  else{
  echo  "<p>Please enter a search query</p>";
  }
  }
  }

 

Basically I have a number that's posted from a form field from another page to this page. It displays correctly on the results page but what i'm trying to do now is get that number to change based on the query at the top ''  $result = mysql_query("select `resource` from resources where `resource_id` =" .$namer. "ORDER BY `resource_id` ASC "); "

 

So if the number 3 (which is in the `resource_id` column) was posted it will display a result (which is in the `resource` column).

 

But it just seems that the query isn't going through and it continues to display the 3 which was originally posted instead of changing 3 to a number in the database.

 

I was thinking of performing a while loop so it just just displays the result of the query.. something like this:

 

 $name=$_POST['name'];
  $namer=$name;
  $result = mysql_query("select `resource` from resources where `resource_id` =" .$namer. "ORDER BY `resource_id` ASC ");
while($row = mysql_fetch_array($result))
  {
    echo "<br/>The following nations have " . $row['resource'] . " as a resource\n";
  }

But that seems to have more problems as it doesn't display anything at all, not even the original number I had.

 

I don't think it's a big problem.. Maybe something i've just overlooked?

Link to comment
Share on other sites

Actually ignore that post. I figured it out:

  $namer=$name;
$result = mysql_query("select resource from resources where resource_id =$namer");
while($row = mysql_fetch_array($result))
  {
    echo "<br/>The following nations have " . $row['resource'] . " as a resource\n";
  }

 

I didn't need the dots around the $namer variable.

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.