search using pdo, mysql and php -
I am trying my hand with PDO and would like to know whether the correct code is correct when searching for keywords An error: mysql_real_escape_string (): [2002] Connection attempt failed because the associated host failed to respond.
php class:
public function searchQuotes () {$ search = mysql_real_escape_string ($ _ POST ['search']); $ Sql = "Select Quotes, Vauthor, Crabic, Wherefrom Where Are Quotes like"%: Search% "Order Quotets DESC"; {$ Query = $ this- & gt; _db-> ($ Sql); $ Query-> Bindamaram (': Search', $ Search, PDO :: Param_st); $ Query-> Executed (); If (! $ Query- & gt; punctiquity () == 0) {while ($ line = $ query-> fetch)) {echo $ this-> Format Search ($ line); }} Else {echo "No results found!"; } $ Query- & gt; Closed cursor (); } Hold (exception $ east) {echo "something went wrong" $ east; }} Public Function Format Search ($ line) {$ cQuote = highlightwords (htmlspecialchars ($ line ['quotes']), $ search; Return "& lt; p id = \" s_arabic \ "& gt; . $ This- & gt; H ($ line ['cArabic']) "& lt; / p>
. "& Lt; p id = \" s_quotes \ "& gt; & lt; q & gt; & amp; nbsp;" . $ CQuote "& Amp; nbsp; & lt; / q & gt; & lt; / p>
. "& Lt; p id = \" s_author \ "& gt; & lt; b & gt; - & lt; / b & gt; & amp; nbsp;" . $ This- & gt; H ($ line ['vAuthor']) "& lt; / p>
. "& Lt; p id = \" s_reference \ "& gt; & lt; span class = \" source \ "& gt; source: & lt; / span & gt; $ This- & gt; H ($ line ['vReference']) "& lt; / p & gt;"; }
php page:
if (isset ($ _ POST ['search'])) $ quotes- & gt; SearchQuotes (); Else $ quotes-> Display quotes ();
displayQuotes () shows the quote ok, so I believe that the connection itself is not wrong.
With PDO and binding consultation / prepared statement you do not need to avoid the string. How you have this setup, you should save yourself the PDO.
Since you are using a PDO, you are not using the mysql_connect driver and in this way you can not use the real_escape_string function because using mysql_connect, valid connections from mysql server Required.
Edit:
If this statement is not definite, but it can be problematic:
if ($ query-> RowCount ()> 0)
It is better to use IMO whether this problem may or may not be the other thing that you should check and if there is any error in some way So be alert to you.
Comments
Post a Comment