以下は、データベース フィールド "Never" からの HTML 結果を表示します。出力に CSS スタイルを適用しようとしています。
これが私が持っているものです...
echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />";
これが私が試したことです...
echo "<p><strong>Never:</strong> ".$results['<div id="nevermsg">'Never'</div>]."".$results['text']."</p><br />";
これが私のCSSです...
#nevermsg { color: red; }
...しかし、それは適切に適用されていません。構文エラーが発生し、頭が痛くなります。これを間違った場所に置いていますか?
$results
変数が入力されていません。
編集:コードが追加されました
これが私の接続です...
<?php
mysql_connect("localhost", "jpcso_compliance", "abc*123") or die("Error connecting to database: ".mysql_error());
/*
localhost - it's location of the mysql server
root - username
third is your password
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("jpcsolut_compliance") or die(mysql_error());
/* jpcsolut_webfro_HS is the name of database we've created */
?>
ここにあるもの以外に、出力用の HTML 形式は他にありません...
<div id="title">
<p><h1>Database Search Results</h1></p></div>
<br />
<div id="main_inner">
<?php
$query = $_GET['query'];
// gets value sent over search form
$min_length = 2;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM Compliance
WHERE (`Question` LIKE '%".$query."%') OR (`Sample Response / Must` LIKE '%".$query."%') OR (`Must` LIKE '%".$query."%') OR (`Can` LIKE '%".$query."%') OR (`Never` LIKE '%".$query."%') OR (`Tags` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// IllegitimateHighSchools is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<p><strong><u><h2>Question:</u> ".$results['Question']."".$results['text']."</h2></u></strong></p><br />";
echo "<p><strong>Sample Response / Must:</strong> ".$results['Sample Response / Must']."".$results['text']."</p><br />";
//echo "<p><strong>Location:</strong> <a href='".$results['Location']."' target='_blank'>".$results['SchoolLocation']."</a>".$results['text']."</p><br />";
echo "<p><strong>Must:</strong> ".$results['Must']."".$results['text']."</p><br />";
echo "<p><strong>Can:</strong> ".$results['Can']."".$results['text']."</p><br />";
//echo "<p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p><br />";
echo "<span id=\"nevermsg\"><p><strong>Never:</strong> ".$results['Never']."".$results['text']."</p></span><br />";
echo "<p><strong>_____________________________________________</strong> "."</p><br />";
echo "<p><strong>Tags:</strong> ".$results['Tags']."".$results['text']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "<br /><br /><br /><strong><h2>"."You have searched a term that is not in the database. Please contact <a href=\"mailto:" . htmlentities('email@domain.com') . "\">".htmlentities('email@domain.com') . "</a>, if you think this term should be added."."</h2></strong>";
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
<br />
<br />
<br />
<br />
<br />
<br />
</div>
<!--End of Inner Main-->
さらに、ここに URL にクエリを含めたサイトへのリンクを示します。
最後に、スタイルが存在するスタイルシート「global.css」を呼び出します。
#nevermsg { color: red; }