urielk Posted April 19, 2011 Share Posted April 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/ Share on other sites More sharing options...
Pikachu2000 Posted April 19, 2011 Share Posted April 19, 2011 See anything wrong here? $phrase = implode("%' AND articlename LIKE '%", $words); $sql ="SELECT * FROM articles WHERE articlename LIKE '%phrase%'"; // <---------- HERE . . . Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203275 Share on other sites More sharing options...
urielk Posted April 19, 2011 Author Share Posted April 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203432 Share on other sites More sharing options...
Pikachu2000 Posted April 19, 2011 Share Posted April 19, 2011 You implode the $words array and assign it to the variable $phrase, then you never use $phrase in the query string. Right now the query will only match records that actually have the word "phrase" in the articlename field. Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203439 Share on other sites More sharing options...
urielk Posted April 19, 2011 Author Share Posted April 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203457 Share on other sites More sharing options...
mikosiko Posted April 19, 2011 Share Posted April 19, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203481 Share on other sites More sharing options...
urielk Posted April 19, 2011 Author Share Posted April 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203584 Share on other sites More sharing options...
mikosiko Posted April 19, 2011 Share Posted April 19, 2011 if you echo $numRows what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203624 Share on other sites More sharing options...
urielk Posted April 19, 2011 Author Share Posted April 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203657 Share on other sites More sharing options...
mikosiko Posted April 19, 2011 Share Posted April 19, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203659 Share on other sites More sharing options...
urielk Posted April 19, 2011 Author Share Posted April 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234109-mysqli-query/#findComment-1203708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.