[PHP & MySQL] returning records, if/else statements

windows

Guest
Hi there,

I am pulling records from a MySQL table and I want to add an if/else statement that either returns the results if any records are found, or returns some text saying "no records matched your search criteria".

I am having trouble getting this to work. Here is my PHP code:

<?php

# See if there is a form submission

if ( $_POST['keyword'] ) {
# KEYWORD SEARCH

# Assign POST input to local variable $k
$k = $_POST['keyword'] ;

# Divide up search phrase into an array of separate words
$words = split("[ ,]+", $k) ;

$q = "SELECT Lname,Fname,ShortTitle,Keywrds,ID FROM $tablename_abs WHERE " ;

foreach ($words as $w) {
$q .= "(Keywrds LIKE '%$w%') and ";
}

$q = substr($q, 0, -4) ;

/* Connecting, selecting database */
$link = mysql_connect("$dbhost", "$dbusername", "$dbuserpassword") or die("Could not connect") ;
mysql_select_db("$default_dbname") or die("Could not select database") ;

/* Performing SQL query */

# HERE IS WHERE I HAVE TROUBLE
# THE COMMENT BELOW SHOWS CODE THAT WORKED BUT ALWAYS RETURNED THE QUERY
# RESULTS, EVEN IF NOTHING WAS FOUND.

#if ($result = mysql_query($q, $link) )

# THIS IS MY IF/ELSE STATEMENT ATTEMPT THAT DOES NOT WORK (I STILL GET THE SAME RESULT)

$result = mysql_query($q, $link) ;

if (!$result) {
# I WANT THIS PRINTED OUT
# print "<p>No results found with those keywords</p>" ;
}
elseif ($result)
{
printf ("HTML stuff") ;
printf ("HTML stuff") ;

/* retrieve data from buffer and run loop for each record retrieved */
while ($line = mysql_fetch_row($result))
{
/* Assign local variables from array $line */
list($Lname,$Fname,$ShortTitle,$Keywrds,$ID)=$line ;

/* Display the link */
printf ("HTML stuff") ;
}
printf ("HTML stuff") ;
printf ("HTML stuff") ;
}

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);
}

# ALSO, IF NO SEARCH CRITERIA IS ENTERED BUT THE USER HITS 'SUBMIT' I WANT THIS CODE TO COME OUT. IT DOESN'T WORK YET - ANY IDEAS?


elseif ( $_POST[''] ) {
print "<p>You did not enter any search criteria!</p>" ;
}

?>

Thanks very much. I can clarify if this wasn't all that clear.

- TatlarI am a little confused? where ar eyou having problems? I don't see why it shouldn't work.

$words = split("[ ,]+", $k) ;

why are you doing that. are they able to submit keywords that have "[ ,]+" in them?

the split function doesn't do regular expression I don't think.

if you start out as

if($_POST["keyword"]){

then why are you doing it at the bottom as well? you don't need that elseif at the bottom. just you "else"

so try this and don't use fetch_row, as fetch_array is better and faster. so take out all the comments and see what you get as the # is real confusing.

$result = mysql_query($q, $link) ;

if (!mysql_num_rows($result)) {
# I WANT THIS PRINTED OUT
print "<p>No results found with those keywords</p>" ;
exit;
}
else
{
printf ("HTML stuff") ;
printf ("HTML stuff") ;

/* retrieve data from buffer and run loop for each record retrieved */
while ($line = mysql_fetch_array($result))
{
/* Assign local variables from array $line */
list($Lname,$Fname,$ShortTitle,$Keywrds,$ID)=$line ;

/* Display the link */
printf ("HTML stuff") ;
}
printf ("HTML stuff") ;
printf ("HTML stuff") ;
}

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);
}

# ALSO, IF NO SEARCH CRITERIA IS ENTERED BUT THE USER HITS 'SUBMIT' I WANT THIS CODE TO COME OUT. IT DOESN'T WORK YET - ANY IDEAS?


else {
print "<p>You did not enter any search criteria!</p>" ;
}

?>Scoutt,

You can use regular expressions with the function split (see <!-- m --><a class="postlink" href="http://www.php.net/manual/en/function.split.php">http://www.php.net/manual/en/function.split.php</a><!-- m -->).

Regarding my problem, the solution you suggested didn't work, I kept getting the following error:

Fatal error: Call to undefined function: () in /path/to/search.php on line 95

where line 95 is:

if (!$mysql_num_rows( $result ) ) {
print "<p><strong>No results found with those keywords</strong></p>" ;
}

So what I did (above line 95) was this:

$result = mysql_query($q, $link) ;
$query_data = mysql_fetch_array($result);
$total_num_results = $query_data[0];

if (!$total_num_results) {

However, if one search result is found, it doesn't print out. If more than one is found it works (but still excluding the first result). This works better than the mysql_num_rows function, but I don't know why it excludes the initial result, maybe because the function total_num_results returns the first value in the array $query_data?

Any advice would help in solving this. Thanks,

- Tatlaryou added a $ to it

if (!$mysql_num_rows( $result ) ) {

you are not suppose to have one

if (!mysql_num_rows( $result ) ) {

that will work. and I never used split to use regular expressions, it is slow that way.

also I would do it this way

$total = mysql_num_rows($result);

if(!$total){
// print none found
} else {
//use a for next loop to print out results
for ($x=0; $x<$total; $x++){
$row = mysql_fetch_array($result);
// print out result here
}
}Scoutt,

Why do:

for ( $x=0; $x<$total; $x++ )

over:

while ($line = mysql_fetch_array( $result ) )

they both have the same result, right? Is one much faster?

Oh by the way - thanks for solving my problem - it is working now :)

Best,

- TatlarI have had better luck doing it with the for loop, as I can show jsut the number of the rows found. if you have it the other way and it works fine than great, kepe it as they are both the same speed.
 
Back
Top