Page 2 of 3

Re: Trouble with a really simple project

Posted: Tue Jun 17, 2008 11:35 pm
by John Cartwright
Then I suggest you read WebbieDave suggestion.

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 12:08 am
by andysm849
Okay so Ive changed some stuff, but it stillll isnt working; it just produces a blank screen as it always has.

Code: Select all

 
<?php
/* Program: wordDisplay.php
 */
?>
<html>
<head><title>Definition</title></head>
<body>
<?php
  $user="------";
  $host="localhost";
  $password="--------";
  $database = "dictionary";
  $cxn = mysqli_connect($host,$user,$password,$database)
         or die ("couldn't connect to server");
 if (isset($_POST['name'])) {
    $wordname = mysql_real_escape_string($_POST['name']);
 } else {
   //handle error.. missing word
 }
  $query = "SELECT * FROM word WHERE wordname='$wordname'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
 
  /* Display results in a table */
  $wordname = ucfirst($wordname);
  echo "<h1>$wordname</h1>";
  echo "<table cellspacing='15'>";
  echo "<tr><td colspan='3'><hr /></td></tr>";
  while($row = mysqli_fetch_assoc($result))
  {
     extract($row);
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='3'><hr /></td></tr>\n";
  }
  echo "</table>\n";
?>
</body></html>
 
The searchbox's get was changed to post.

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 2:29 am
by tecktalkcm0391
andysm849 wrote:

Code: Select all

  while($row = mysqli_fetch_assoc($result))
  {
     extract($row);
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='3'><hr /></td></tr>\n";
  }
  echo "</table>\n";
?>
 
Just from my point of view, I'm not liking the mysql_fetch_assoc($result) then extract($row)... try just doing...

Code: Select all

while($row = mysqli_fetch_assoc($result))
  {
     echo "<tr>\n
            <td>$row['worddef']</td>\n
            <td>$row['wordsent']</td>\n
           </tr>\n";
     echo "<tr><td colspan='3'><hr /></td></tr>\n";
  }
  echo "</table>\n";
?>
 
One other thing is check the Outputed HTML's Source, because you may just have hidden the output, with something like <td colspan='3'> (just throwing that out there, i don't know if its even possible)... because i don't see the three columns, only two... but its late, so i may be wrong

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 10:20 am
by andysm849
Okay so I changed the code again to get rid of the extract $row thing, and made colspan = '2'. Still does not work. What could be wrong?!?

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 3:22 pm
by tecktalkcm0391

Code: Select all

<?php 
 
$result = mysqli_query($cxn,$query)
     or die ("Couldn't execute query.");
 
if(!$result){
     die(mysqli_error());
}
 
?>
Try adding this after the line $result... it will die if there is an error normally...
and by looking at the $result line carefully... you are going to be :banghead: after you read the next line:

the mysqli_query() command is mysqli_query(query, link or connection identifier)... update the line to say mysqli_query($query, $cxn);

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 5:57 pm
by andysm849
Ok so now I get a Couldn't Execute Query, which I spose is positive because its better than nothing. But of course it still refuses to work Heres the code now...

Code: Select all

<html>
<head><title>Definition</title></head>
<body>
<?php
  $user="-------";
  $host="localhost";
  $password="-------";
  $database = "dictionary";
  $cxn = mysqli_connect($host,$user,$password,$database)
         or die ("couldn't connect to server");
 if (isset($_POST['name'])) {
    $wordname = mysql_real_escape_string($_POST['name']);
 } else {
   //handle error.. missing word
 }
  $query = "SELECT * FROM word WHERE wordname='$wordname'";
  $result = mysqli_query($query,$cxn)
            or die ("Couldn't execute query.");
 if(!$result){
            die(mysqli_error());
 }
 
  /* Display results in a table */
  $wordname = ucfirst($wordname);
  echo "<h1>$wordname</h1>";
  echo "<table cellspacing='15'>";
  echo "<tr><td colspan='2'><hr /></td></tr>";
  while($row = mysqli_fetch_assoc($result))
  {
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table>\n";
?>
</body></html>
 

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 6:15 pm
by tecktalkcm0391
You really need to look at the database commands...

Is your line correct:
$cxn = mysqli_connect($host,$user,$password,$database);

No, it should be:
$cxn = mysqli_connect($host, $user, $password);

Then you need to add the following, so you can select the database properly.
mysqli_select_db($database, $cxn);


You'll get the hang of it soon. :wink:

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 6:27 pm
by andysm849
Thanks for the help, I really hope so.

Anyways its still saying "Couldn't execute query"
Here's the code now:

Code: Select all

<html>
<head><title>Definition</title></head>
<body>
<?php
  $user="-------";
  $host="localhost";
  $password="--------";
  $database = "dictionary";
  $cxn = mysqli_connect($host,$user,$password)
         or die ("couldn't connect to server");
    mysqli_select_db($database, $cxn);
 if (isset($_POST['name'])) {
    $wordname = mysql_real_escape_string($_POST['name']);
 } else {
   //handle error.. missing word
 }
  $query = "SELECT * FROM word WHERE wordname='$wordname'";
  $result = mysqli_query($query,$cxn)
            or die ("Couldn't execute query.");
 if(!$result){
            die(mysqli_error());
 }
 
  /* Display results in a table */
  $wordname = ucfirst($wordname);
  echo "<h1>$wordname</h1>";
  echo "<table cellspacing='15'>";
  echo "<tr><td colspan='2'><hr /></td></tr>";
  while($row = mysqli_fetch_assoc($result))
  {
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table>\n";
?>
</body></html>
 

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 6:32 pm
by tecktalkcm0391
Could you try deleting (or just commenting) line 19 and see what they mysqli_error says in the next line....?

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 6:34 pm
by tecktalkcm0391
Try this... and post the HTML that is generated please.

Code: Select all

<html>
<head><title>Definition</title></head>
<body>
<?php
  $user="-------";
  $host="localhost";
  $password="--------";
  $database = "dictionary";
  $cxn = mysqli_connect($host, $user, $password)
         or die ("couldn't connect to server");
    mysqli_select_db($database, $cxn);
 if (isset($_POST['name'])) {
    $wordname = mysql_real_escape_string($_POST['name']);
 } else {
   //handle error.. missing word
   die('Missing word!');
 }
  $query = "SELECT * FROM `word` WHERE wordname='$wordname'  ";
  // you can delete this next line if the script works.... but post the output please...
  echo $query.'<br>'; 
  $result = mysqli_query($query,$cxn)
            or die ("Couldn't execute query.");
 if(!$result){
            die(mysqli_error());
 }
 
  /* Display results in a table */
  $wordname = ucfirst($wordname);
  echo "<h1>$wordname</h1>";
  echo "<table cellspacing='15'>";
  echo "<tr><td colspan='2'><hr /></td></tr>";
  while($row = mysqli_fetch_assoc($result))
  {
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table>\n";
?>
</body></html>
 

Re: Trouble with a really simple project

Posted: Wed Jun 18, 2008 11:14 pm
by andysm849
Thank you sooooo much for all the help so far but Ive had a change of plans which I think will be far more effective. I realized that that program really didn't suit my needs and that I should use a program that was actually made to search. Anyways I found one online, and it seems to be working a heck of a lot better than my previous misguided program. It seems for the most part to be displaying results. But I just have one final problem; it only displays data from one field in the database, when i need to display the word, the definition, and it used in a sentence, aka 3 fields. The code is:

Code: Select all

<?php
 
  // Get the search variable from URL
 
  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable
 
// rows to return
$limit=10; 
 
// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }
 
// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }
 
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","------","--------"); //(host, username, password)
 
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("dictionary") or die("Unable to select database"); //select which database we're using
 
// Build SQL Query  
$query = "select * from word where wordname like \"%$trimmed%\"  
  order by wordname"; // EDIT HERE and specify your table and field names for the SQL query
 
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 
// If we have no results, offer a google search as an alternative
 
if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
 
// google
 echo "<p><a href=\"http://www.google.com/search?q=" 
  . $trimmed . "\" target=\"_blank\" title=\"Look up 
  " . $trimmed . " on Google\">Click here</a> to try the 
  search on google</p>";
  }
 
// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }
 
// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");
 
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
 
// begin to show results set
echo "Results";
$count = 1 + $s ;
 
// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["wordsent"];
 
  echo "$count.)&nbsp;$title" ;
  $count++ ;
  }
 
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
 
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp&nbsp;";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }
 
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>
So I want to display the results in a table like form so something along the lines of my previous database:

Code: Select all

 
$wordname = ucfirst($wordname);
  echo "<h1>$wordname</h1>";
  echo "<table cellspacing='15'>";
  echo "<tr><td colspan='2'><hr /></td></tr>";
  while($row = mysqli_fetch_assoc($result))
  {
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table>\n";
 
When I try to interchange the stuff after "while($row = mysqli_fetch..." It just comes up blank and doesnt work at all. I know there is something wrong with this result statement but could someone tell me how to fix it? I feel I am soo close now.

Re: Trouble with a really simple project

Posted: Thu Jun 19, 2008 11:07 am
by tecktalkcm0391
Replace

Code: Select all

// now you can display the results returned
   while ($row= mysql_fetch_array($result)) {
   $title = $row["wordsent"];
  
   echo "$count.)&nbsp;$title" ;
   $count++ ;
   }
with

Code: Select all

 
$wordname = ucfirst($wordname);
  echo "<h1>$wordname</h1>";
  echo "<table cellspacing='15'>";
  echo "<tr><td colspan='2'><hr /></td></tr>";
  while($row = mysqli_fetch_assoc($result))
  {
     echo "<tr>\n
           <td>$worddef</td>\n
           <td>$wordsent</td>\n
          </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table>\n";
 
But look at your lines:

Code: Select all

<td>$worddef</td>\n
           <td>$wordsent</td>\n
I think we changed it to $row['worddef'] and $row['wordsent'] because you need to get them out of the array ($row) that is create by the function mysqli_fetch_assoc(...)

Hope it works now!

Re: Trouble with a really simple project

Posted: Thu Jun 19, 2008 9:07 pm
by andysm849
Thanks for your continued help you guys, but its still not working... Okay so when I replaced the $worddef with $row['worddef'] and the same with wordsent I got just a blank page. However when I just had $worddef with the table replacing code, it gave me "You searched for ----- Results:" with a space where the results should have been, and then below "showing results 1 of 1". So I assume theres something wrong with $row['worddef']...But neither method ($worddef or $row['worddef']) shows the word in bold as echo "<h1>$wordname</h1>"; says it should. So none of that results block of code seems to be displaying anything. Any advice?

Code: Select all

<?php
 
// Build SQL Query  
$query = "select * from word where wordname like \"%$trimmed%\"  
  order by wordname"; // EDIT HERE and specify your table and field names for the SQL query
 
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 
// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }
 
// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");
 
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
 
// begin to show results set
echo "Results";
$count = 1 + $s ;
 
// now you can display the results returned
 $wordname = ucfirst($wordname);
   echo "<h1>$wordname</h1>";
   echo "<table cellspacing='15'>";
   echo "<tr><td colspan='2'><hr /></td></tr>";
   while($row = mysqli_fetch_assoc($result))
   {
     echo "<tr>\n
           <td>$row['worddef']</td>\n
           <td>$row['wordsent']</td>\n
          </tr>\n";
      echo "<tr><td colspan='2'><hr /></td></tr>\n";
   }
   echo "</table>\n";
 
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
 
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp&nbsp;";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }
 
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>
 

Re: Trouble with a really simple project

Posted: Thu Jun 19, 2008 11:31 pm
by tecktalkcm0391
First of all are you using MySQL or MySQLi... cause you'd need to change the functions, i think :?

Try this... I added a line to echo everything that the database is returning.... post what is outputed...

Code: Select all

<?php
 
// Build SQL Query  
$query = "select * from word where wordname like \"%$trimmed%\"  
  order by wordname"; // EDIT HERE and specify your table and field names for the SQL query
 
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 
// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }
 
// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");
 
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
 
// begin to show results set
echo "Results";
$count = 1 + $s ;
 
// now you can display the results returned
 $wordname = ucfirst($wordname);
   echo "<h1>$wordname</h1>";
   echo "<table cellspacing='15'>";
   echo "<tr><td colspan='2'><hr /></td></tr>";
   while($row = mysqli_fetch_assoc($result))
   {
 
// test for data
print_r($row);
 
     echo "<tr>\n
           <td>$row['worddef']</td>\n
           <td>$row['wordsent']</td>\n
          </tr>\n";
      echo "<tr><td colspan='2'><hr /></td></tr>\n";
   }
   echo "</table>\n";
 
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
 
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp&nbsp;";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }
 
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>
 

Re: Trouble with a really simple project

Posted: Thu Jun 19, 2008 11:42 pm
by Benjamin
Just wanted to mention that mysqli_connect can accept the database name as a 4th parameter, while mysql_connect cannot.