Jump to content

mysqli query


urielk

Recommended Posts

Hi,

 

I am having problems returning values from a select statement. When I query directly in the databse, I get back the information I am looking for. I use an includes file for the database connection and my page shows that the connection was successful. Here is my code:

 

<?php
  
    $search = $_GET['searchFor'];
    $words = explode(" ", $search);
    $phrase = implode("%' AND articlename LIKE '%", $words);

   	$sql ="SELECT * FROM articles WHERE articlename LIKE '%phrase%'";
    $result =$conn->query($sql) or  die('Sorry, could not get any articles at this time');
    $row =($result->fetch_all()) or die('No records found');
    $numRows =$result->num_rows;

    If($numRows==0)
    {

    echo "<h2>Sorry, no articles were found with '$search' in them.</h2>";
    } 
else
    {
        While($row=$result->fetch_assoc())
        {
            $articleid = $row['articleid'];
            $title = $row['articlename'];
            $shortdesc = $row['shortdesc'];
		echo "<h2>Search Results</h2><br><br>\n";
            echo "<a href=\"index.php?content=showarticle&id=$articleid\">$title</a><br>\n";
            echo "$shortdesc<br><br>\n";
        }
    }
?>

 

The search term is coming from a search form in the navigation. I have used "echo" statements to check and make sure that the sesrch word is coming through to tghe page containing the above code. I have tried mysqli_error() statements in several places and don't see where the problem is. When I try the search the message that comes back is "No records found" Does not makee sense because I know it is there, can find it, and even have the same syntax as the SELECT statement I use when I ask for the php code. Going crazy trying to sort this out. Any suggestions, help etc are greatly appreciated. Thank youi.

Link to comment
Share on other sites

New to php so I apologize but I don't see an error in the statement. I looked at hph code hint by doing a query using phpMyAdmin. They seemed to have a parentheses between Where and the before the double quotes. I tried that and the query says there are no records when I can find them. For some reason the query is not picking up the $phrase and I am n0ot sure how to figure out why.  Be a big help if anyone can point me to a way to get this information. Thanks.

Link to comment
Share on other sites

Hi,

 

Thank you for pointing out the error. Did not see it neither did someone else who looked at the code several times. I made the change but I am still not getting any results from the query. I have a file index.php where I have an included file for the connection. I also have an included file for the sidebar which is where I have the form for the search. I left an echo statement in my connection file to tell me if the connection is successful and that statement is still there and I get a message that the connection is successful. My query is not working and I am not sure what statements to use to find out the source of the error so I can fix it. Thanks for all the help.

Link to comment
Share on other sites

do a simple test...

 

  $sql ="SELECT * FROM articles WHERE articlename LIKE '%phrase%'"; // assuming that you already changed this line as Pica suggested

 

  echo "** The SQL is : " . $sql;  // add this line and post what your get

 

  $result =$conn->query($sql) or  die('Sorry, could not get any articles at this time');

 

also.. is good idea to include this 2 lines at the beginning of your code for debugging:

error_reporting(E_ALL); 
ini_set("display_errors", 1);

Link to comment
Share on other sites

Hello Everyone,

 

Thanks for the patience. I made the correction suggested by Pica and also added both the error reporting and ini_set statements. What I get back is:

 

** The SQL is : SELECT * FROM articles WHERE articlename LIKE '%Metabolism%'

 

That is all I get and I copied it exactly as it appears. So if the search term is being passed to the query and I can find the result from phpMyAdmin but not from the program, where are things going wrong?  Thanks for the help.

Link to comment
Share on other sites

I get a "1" in addition to the statement that echoed back earlier. Here is an exact copy:

 

** The SQL is : SELECT * FROM articles WHERE articlename LIKE '%Metabolism%'1

 

OK so it returns one row which is non-zero. Since the if statement is false it should go to else and execute the while loop right? Do I need a count at the end of the query so it has some limit to step through? Maybe that is the problem. Thanks for any additional comments or suggestions.

Link to comment
Share on other sites

check your logic again....

 

in your code:

 $row =($result->fetch_all()) or die('No records found');
    $numRows =$result->num_rows;

    If($numRows==0)
    {

    echo "<h2>Sorry, no articles were found with '$search' in them.</h2>";
    } 
else
    {
        While($row=$result->fetch_assoc())

 

you first

$row =($result->fetch_all()) or die('No records found');

already got the first record... which happens to be the only one, and moved the internal pointer... therefore your while loop is no processing anything... is nothing to process right?

Link to comment
Share on other sites

I am not sure I agree. A similar code in another program seems to work fine and even though there is only one result, it returns the details - articlename, date, and short description - where the article name becomes a hyperlink allowing one to get the full article text. That is what I am trying to do here and it is not working. That is the reason for my request. Later as I add more articles I am expecting the program to return a hyperlinked list so people get the details. Thanks for your comment anyway.

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.