Jump to content

[SOLVED] help with php and mysql


curt22

Recommended Posts

whats wrong with this code?
[quote]
<?php
if ($_post[field] == chapter) {
include 'connect.php';
$sql = "SELECT * FROM bible WHERE MATCH($_POST[field])
      AGAINST('$_POST[find]' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    echo "\r\n$row[testimate] $row[book] $row[chapter]:$row[verse] <br>$row[versetext]</br>";
    elseif
}
elseif ($_post[field] == book) {
include 'connect.php';
$sql = "SELECT * FROM bible WHERE MATCH($_POST[field])
      AGAINST('$_POST[find]' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result))
    echo "\r\n$row[testimate] $row[book] $row[chapter]:$row[verse] <br>$row[versetext]</br>";
    }
elseif ($_post[field] == testimate){

include 'connect.php';
$sql = "SELECT * FROM bible WHERE MATCH($_POST[field])
      AGAINST('$_POST[find]' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());
}
else
{
include 'connect.php';
$result = mysql_query("select * from bible
Where $_post[field] like '%$_post[find]%'") or die(mysql_error());
while($row = mysql_fetch_array($result))
    echo "\r\n$row[testimate] $row[book] $row[chapter]:$row[verse] <br>$row[versetext]</br>";

}




?>[/quote]
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???
Link to comment
Share on other sites

What errors are you getting? A little more information would be very helpful.

From the Posting Guidelines:
[url=http://www.phpfreaks.com/forums/index.php/topic,6264.0.html]http://www.phpfreaks.com/forums/index.php/topic,6264.0.html[/url]

[quote]4. Ask good questions! Consider the wording of your question carefully, [b]be specific about your problem(s),[/b] and make sure that you have actually asked a question.  Use punctuation and spelling to the best of your ability.  Run on paragraphs without sentence breaks, and full of mispelled words limits your chances for a reply.[/quote]
Link to comment
Share on other sites

1.  You should put quotes around all string constants, like this:

[code=php:0]if ($_post['field'] == 'chapter') {[/code]


2.  You should include connect.php only once.

3.  You have a lone "elseif" in the first while loop.  Removing that should fix the "blank page".  Blank page usually means there's a problem with your syntax.
Link to comment
Share on other sites

I agree with btherl...

The empty elseif on line 11 is probably the most likely culprit of a blank page. But if that's the case, then your error reporting is not set right (for development anyway). If its a server where you have no control over the php.ini file, I suggest using this line at the top of your pages while developing them

[code]<?php ini_set('error_reporting', E_ALL); ?>[/code]

This will turn error reporting for things like this. My system outputs the following error for the problem on line 11.

Parse error: parse error, unexpected T_ELSEIF in C:\Program Files\xampp\htdocs\nate.localnet\testing.php on line 11
Link to comment
Share on other sites

I changed the code to this
[quote]
<?php
ini_set('error_reporting', E_ALL);
include 'connect.php';
if ($_post['field'] == 'chapter') {

$sql = "SELECT * FROM bible WHERE MATCH($_POST['field'])
      AGAINST('$_POST['find']' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    echo "\r\n$row['testimate'] $row['book'] $row['chapter']:$row['verse'] <br>$row['versetext']</br>";
   
}
elseif ($_post['field'] == 'book') {

$sql = "SELECT * FROM bible WHERE MATCH($_POST['field'])
      AGAINST('$_POST['find']' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result))
    echo "\r\n$row['testimate'] $row['book'] $row['chapter']:$row['verse'] <br>$row['versetext']</br>";
    }
elseif ($_post['field'] == 'testimate'){


$sql = "SELECT * FROM bible WHERE MATCH($_POST['field'])
      AGAINST('$_POST['find']' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());
}
else
{

$result = mysql_query("select * from bible
Where $_post[field] like '%$_post['find']%'") or die(mysql_error());
while($row = mysql_fetch_array($result))
    echo "\r\n$row['testimate'] $row['book'] $row['chapter']:$row['verse'] <br>$row['versetext']</br>";

}




?>

[/quote]
but its still not working.
Link to comment
Share on other sites

since I don't have  form and database to work with, I can't really do much more right now than have you try some things, but try echoing the form variables at the top and make sure you are recieving them. then echo your sql statements to ensure they are correctly inserting the variables, and do a $num=mysql_num_rows; echo $num to make sure you are returning results. See what you find there and I will try to help more tomorrow.

EDIT:

the full line just in case you don't know, is
$num=mysql_num_rows($result);
echo $num;
insert this right below each of your $result  variables and ensure that its giving you results for your search.
Link to comment
Share on other sites

Thanks for the help I changed the error reporting level and got the page to work  :) but now I have another problem  :( how can I make this part of the code :
[quote]
if ($find == 'book') {

$sql = "SELECT * FROM bible WHERE MATCH($find)
      AGAINST('$whattofind' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result))
    echo "</br>$row[testimate] $row[book] $row[chapter]:$row[verse]
$row[versetext]</br>";

    }

[/quote]
search for words with less than 4 letters. I know I could use like %$find% but I don't want to. Thanks for the help.
Link to comment
Share on other sites

I tried using the explode function then counting the letters and and if any words have less than 4 letters it lets them know it won't be included, and I got it working but now it shows there search results twice.
It looks like this:
[quote]
You searched for: VERSETEXT TEST.
In the field: versetext.
Your search returned 1 matches.

old genesis 1:1
versetext
Note: the word TEST will not be included because it has less than 5 letters try using a differant word or click below to try to use a Like search.

and a button to use like search is here
 
Your search returned 2 matches.

old genesis 1:1
versetext

[/quote]
This is the code:
[quote]
<?php

//links
include 'links.html';

//connect
include 'connect.php';
//remove ""
$find2 = $_REQUEST['field'];
$find1 = htmlentities($find2);
$find = mysql_real_escape_string($find1);

$whattofind2 = $_REQUEST['find'];
$whattofind1 = htmlentities($whattofind2);
$whattofind = mysql_real_escape_string($whattofind1);
$whattofind = preg_replace ( "/\s\s+/" , " " , $whattofind );

// $a is used to count results results
$a = 0;

//make capitals
$whattofind = strtoupper($whattofind);
//remind them what they searched for
print '<br>';
print "You searched for: $whattofind.";
print '<br>';
print "In the field: $find.";
print '</br>';

    //if they searched for a verse's text



/////////////////////////////////start the search////////////////////////////////////////////////////
if ($find == 'versetext') {
  //check for less than 4 letters
$wordchunks= explode(" ", $whattofind) ;
for($i = 0; $i < count($wordchunks); $i++){


$length=strlen($wordchunks[$i]);
if ($length < 5){
echo "<b> Note: the word $wordchunks[$i] will not be included because it has less than 5 letters try using a differant word or click below to try to use a Like search.</b><br>  <form action='searchresults2.php' method='post'>  <input type='hidden' size='20' maxlength='40' name='find' value='$whattofind'>    <input type='submit' value='use like search' />    ";                           

}

echo '</form>';
$sql = "SELECT * FROM bible WHERE MATCH(versetext)
      AGAINST('$whattofind' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$a++;
}
echo "Your search returned $a matches.";
    $sql = "SELECT * FROM bible WHERE MATCH(versetext)
      AGAINST('$whattofind' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    echo "<br></br>$row[testimate] $row[book] $row[chapter]:$row[verse]
<br>$row[versetext]</br>";

    }
    }
    }
else {
//if they searched for something else


$result = mysql_query("SELECT * FROM bible WHERE $find LIKE '%$whattofind%' ") or die(mysql_error());
while($row = mysql_fetch_array($result)){


$a++;
}
echo "your search retutrned $a matches.";
$result = mysql_query("SELECT * FROM bible WHERE $find LIKE '%$whattofind%' ") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
    echo "<br><br>$row[testimate] $row[book] $row[chapter]:$row[verse] </br>
$row[versetext]</br>";
}
}
mysql_free_result($result);
?>

[/quote]
Link to comment
Share on other sites

Thanks for the help I got it to work.
Incase anyone wants to see it this this what i changed it to:
[quote]
<html>
<head><title></title>
<body>
<?php

//links
include 'links.html';

//connect
include 'connect.php';
//remove ""
$find2 = $_REQUEST['field'];
$find1 = htmlentities($find2);
$find = mysql_real_escape_string($find1);

$whattofind2 = $_REQUEST['find'];
$whattofind1 = htmlentities($whattofind2);
$whattofind = mysql_real_escape_string($whattofind1);
$whattofind = str_replace(",", " ", $whattofind);
$whattofind = preg_replace ( "/\s\s+/" , " " , $whattofind );

// $a is used to count results results
$a = 0;

//make capitals
$whattofind = strtoupper($whattofind);
//remind them what they searched for
print '<br>';
print "You searched for: $whattofind.";
print '<br>';
print "In the field: $find.";
print '</br>';

    //if they searched for a verse's text



/////////////////////////////////start the search////////////////////////////////////////////////////
if ($find == 'versetext') {
  //check for less than 4 letters
$wordchunks= explode(" ", $whattofind) ;
for($i = 0; $i < count($wordchunks); $i++){


$length=strlen($wordchunks[$i]);
if ($length < 5){
echo "<b> Note: the word $wordchunks[$i] will not be included because it has less than 5 letters try using a differant word or click below to try to use a Like search.</b><br>  <form action='searchresults2.php' method='post'>  <input type='hidden' size='20' maxlength='40' name='find' value='$whattofind'>    <input type='submit' value='use like search' />    ";                           
echo '</form>';
}
}

$sql = "SELECT * FROM bible WHERE MATCH(versetext)
      AGAINST('$whattofind' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$a++;
}
echo "Your search returned $a matches.";
    $sql = "SELECT * FROM bible WHERE MATCH(versetext)
      AGAINST('$whattofind' IN BOOLEAN MODE) LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    echo "<br></br>$row[testimate] $row[book] $row[chapter]:$row[verse]
<br>$row[versetext]</br>";

    }
    }
 
else {
//if they searched for something else


$result = mysql_query("SELECT * FROM bible WHERE $find LIKE '%$whattofind%' ") or die(mysql_error());
while($row = mysql_fetch_array($result)){


$a++;
}
echo "your search retutrned $a matches.";
$result = mysql_query("SELECT * FROM bible WHERE $find LIKE '%$whattofind%' ") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
    echo "<br><br>$row[testimate] $row[book] $row[chapter]:$row[verse] </br>
$row[versetext]</br>";
}
}
mysql_free_result($result);
?>
</body>
</html>

[/quote]
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.