Jump to content

Minor help


The Praetorian

Recommended Posts

I'm using this script for my dictionary and for some of my other pages. I'd like to add a line so that only one entry at a time is shown.Well, we might need to do different things for each one.For this script.. I want to add a line so that, if someone visits the main page (.php without any ?s=something) only the first entry in that table is shown. (I do have them set on auto-increment, if that helps.) That way all the other entries will be hidden unless they're pointed at.

<?php  $dbh=mysql_connect ("localhost", "****", "****") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("author_localhost");  $result = mysql_query ("SELECT * FROM ihthia WHERE header LIKE '$_GET[s]%'") or die(mysql_error());  while($r = mysql_fetch_array($result))  {	echo '<h1>'.$r['header'].'</h1>';	  echo $r['content'];	  echo "<br />";	  echo "<title>The Shattered Realms ~ Ihthia: $r[header]</title>";  }?>

For this one, I just need it to pull out exactly what it's pointed at (the s= part.) And nothing else. So that way it doesn't pull out similar entries that are one or two letters off. I just want it to get exactly what it's pointed at.

<?php  $dbh=mysql_connect ("localhost", "****", "****") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("author_localhost");  $result = mysql_query ("SELECT * FROM dictionary WHERE word LIKE '$_GET[s]%'") or die(mysql_error());  while($r = mysql_fetch_array($result))  {	echo '<h2>'.$r['word'].'</h2>';	if ($r['category'] != "")	  echo '<span class="blue">Category</span> <b>:</b> '.$r['category'].'<br />';	if ($r['pronounce'] != "")	  echo '<span class="blue">Pronunciation</span> <b>:</b> '.$r['pronounce'].'<br />';	if ($r['definition'] != "")	  echo ''.$r['definition'].'<br />';	if ($r['definitiontwo'] != "")	  echo ''.$r['definitiontwo'].'<br />';	if ($r['related'] != "")	  echo '<span class="blue">Related Entries</span> <b>:</b> '.$r['related'].'<br />';	  echo "<br />";	  echo "<title>The Shattered Realms ~ Search: $r[word]</title>";  }?>

Thanks in advance for any help.

Link to comment
Share on other sites

To just get the first row, leave out the WHERE clause in the SQL statement, and make sure you have an ORDER BY. The first row you get from the result set will obviously be the first one in the database.

$result = mysql_query ("SELECT * FROM ihthia ORDER BY id") or die(mysql_error());$r = mysql_fetch_assoc($result); // the first row

To make an exact match, leave out the LIKE and just use direct comparison.

$result = mysql_query ("SELECT * FROM dictionary WHERE word = '{$_GET['s']}'") or die(mysql_error());

Link to comment
Share on other sites

Eh. I added that line to the wrong file. Fixed that, but it still isn't working right. It's showing the second entry, not the first one. Even clicking the link to the first entry won't make it show up. Here's the code as it looks now.This is everything underneath the connection string.

$result = mysql_query ("SELECT * FROM siymeha ORDER BY id") or die(mysql_error());  $r = mysql_fetch_assoc($result); // the first row  while($r = mysql_fetch_array($result))  {	echo '<h1>'.$r['header'].'</h1>';	  echo $r['content'];	  echo "<br />";	  echo "<title>The Shattered Realms ~ Siymeha: $r[header]</title>";  }

Link to comment
Share on other sites

mysql_fetch_assoc() and mysql_fetch_array() are almost the same thing.what you need to do is replace this:

while($r = mysql_fetch_array($result)) { echo '<h1>'.$r['header'].'</h1>'; echo $r['content']; echo "<br />"; echo "<title>The Shattered Realms ~ Siymeha: $r[header]</title>"; }
with this:
echo '<h1>'.$r['header'].'</h1>';   echo $r['content'];   echo "<br />";   echo "<title>The Shattered Realms ~ Siymeha: $r[header]</title>";

putting mysql_fetch_array() in there is selecting the second line instead of the first, as you already used mysql_fetch_assoc().

Link to comment
Share on other sites

Right, this isn't a one-size-fits-all solution. The above query is to return the first row. If you want to return something that matches a search pattern, that's a different query. So you need to check what is going on and do whatever is appropriate.

if ($_GET['s'] == ""){  //run the query for the first row and show the output}else{  //run the old query that searches on a pattern and loops through the results}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...