Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE</strong>: I integrated Eran's function into the refactored code. NOTE: I corrected it by passing the $table variable into it and renamed it since it doesn't search the query text only but mainly returns the needed rows!</p> <p><strong>MAIN MISTAKES</strong>:</p> <ul> <li>mistake 1: you overwrite query with query2 in all cases which breaks the code.</li> <li>mistake 2: LIKE'%$query%' there is a space missing between LIKE and ' => LIKE '%... this most probably breaks your code too</li> </ul> <p><strong>OTHER ISSUES</strong> </p> <ul> <li>security problem: sql injection danger, use mysql_real_escape_string</li> <li>\n not platform independent: use PHP_EOL</li> <li>alternative way of writing short if blocks</li> <li>use curly brackets for normal if structures and all such structures for the matter</li> </ul> <p>here is your code with some changes, <strong>look at the comments</strong>:</p> <pre><code>&lt;?php session_start(); //ommit, no session var used //use braces, always! //you may write such statements with the short form like if (isset($_GET['cmd'])) : $cmd = $_GET['cmd']; else : die (_MSG_NO_PARAM); endif; $query = ''; //escpae your input - very important for security! sql injection! if ( isset ($_GET["query"])) { $query = mysql_real_escape_string($_GET["query"]); } //no need for the other part you had here $con = mysql_connect("localhost", "root", "geheim"); if (!$con) : die ('Connection failed. Error: '.mysql_error()); endif; mysql_select_db("ebay", $con); if ($cmd == "GetRecordSet") { $table = 'Auctions'; $rows = getRowsByArticleSearch($searchString, $table); //use PHP_EOL instead of \n in order to make your script more portable echo "&lt;h1&gt;Table: {$table}&lt;/h1&gt;".PHP_EOL; echo "&lt;table border='1' width='100%'&gt;&lt;tr&gt;".PHP_EOL; echo "&lt;td width='33%'&gt;Seller ID&lt;/td&gt;".PHP_EOL; echo "&lt;td width='33%'&gt;Start Date&lt;/td&gt;".PHP_EOL; echo "&lt;td width='33%'&gt;Description&lt;/td&gt;".PHP_EOL; echo "&lt;/tr&gt;\n"; // printing table rows foreach ($rows as $row) { $pk = $row['ARTICLE_NO']; echo '&lt;tr&gt;'.PHP_EOL; echo '&lt;td&gt;&lt;a href="#" onclick="GetAuctionData(\''.$pk.'\')"&gt;'.$row['USERNAME'].'&lt;/a&gt;&lt;/td&gt;'.PHP_EOL; echo '&lt;td&gt;&lt;a href="#" onclick="GetAuctionData(\''.$pk.'\')"&gt;'.$row['ACCESSSTARTS'].'&lt;/a&gt;&lt;/td&gt;'.PHP_EOL; echo '&lt;td&gt;&lt;a href="#" onclick="GetAuctionData(\''.$pk.'\')"&gt;'.$row['ARTICLE_NAME'].'&lt;/a&gt;&lt;/td&gt;'.PHP_EOL; echo '&lt;/tr&gt;'.PHP_EOL; } } mysql_free_result($result); //mysql_close($con); no need to close connection, you better don't function getRowsByArticleSearch($searchString, $table) { $searchString = mysql_real_escape_string($searchString); $result = mysql_query("SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME FROM {$table} WHERE upper ARTICLE_NAME LIKE '%" . $searchString . "%'"); if($result === false) { return mysql_error(); } $rows = array(); while($row = mysql_fetch_assoc($result)) { $rows[] = $row; } return $rows; } // ?&gt; ommit closing php tag </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload