Hey all, i am new here and just wanted to let you know that I use to be at htmlforums and I am interested in to learning php more and what not, and I thought of signing up here and did. So, you should be seeing more of me 🙂. haha
I have a php script, that lets a user add a CSS/HTML and then later i will add php to it, and then list it with page numbers, a search function, and alternating row coors, I am getting this error on list.php:
You have an error in your SQL syntax near '' at line 4
I have been working with this for awhile and cant get it.
list.php
<?php
include "config/config.php"; // config file
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('random_code', $db);
if (!$db) {
echo( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
exit();
}
if (!isset($_GET['limit']) and empty($_GET['limit'])){
$limit = 10;
} else {
$limit = $_GET['limit'];
}
if (!isset($_GET['page']) and empty($_GET['page'])){
$page = 0;
} else {
$page = $_GET['page'];
}
$numresults = mysql_query("SELECT * FROM random WHERE title LIKE '%". $query ."%'");
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query. LINE 14
if ($numrows == 0){
echo("No results found matching your query - $query");
exit();}
$pages = intval($numrows/$limit);
if ($numrows%$limit) {
$pages++;}
$current = ($page/$limit) + 1;
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;
}
$first = $page + 1;
if (((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;
} //If not last results page, last result equals $page plus $limit.
else{
$last = $numrows;} // If last results page, last result equals total number of results.
?>
<table width="65%" border="0" align="center" class="results">
<tr>
<td width="66%" align="left"> Results found: <b><?php echo"$numrows"?></b><br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get" name="code">
Sort by Category:
<select name="sort">
<option value="category">Category</option>
<option value="title">Title</option>
<option value="ID">Id</option>
</select>
Sort by Precedence:
<select name="sortdate">
<option value="ASC">Ascending</option>
<option value="DESC">Descending</option>
</select>
<input type="hidden" name="query" value="<?=$query?>">
<input type="hidden" name="page" value="<?=$page?>">
<input type="hidden" name="limit" value="<?=$limit?>">
<!-- note above added hidden fields...you'll need to get these values to the submitted page either way -->
<input name="go" type="submit" value="Sort">
</form></td>
<td width="34%" align="right">Page <b>
<?=$current?>
</b> of <b>
<?=$total?>
</b> <br>
Results per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a> </td>
</tr>
<tr>
<td></td>
</tr>
</table>
<?php
include "config/config.php"; // config file
include "search_code.php"; // file containing code for search
// Request the text of all the code
$result = @mysql_query('SELECT * FROM random');
if (!$result) {
die('<p>Error performing query: ' . mysql_error() .
'</p>');
}
/* USED FOR SORTING */
$sortby = ($_GET['sort'] != '')? addslashes($_GET['sort']) : 'date';
$sortdate = ($_GET['sortdate'] != '') ? addslashes($_GET['sortdate']) : 'DESC';
$query = addslashes($_GET["query"]);
$page = addslashes($_GET["page"]);
$limit = addslashes($_GET["limit"]);
$results = mysql_query("SELECT * FROM random
WHERE title LIKE '%".$query."%'
ORDER BY ".$sortby." ".$sortdate."
LIMIT ".$page.",".$limit."") or die(mysql_error().'<br/>Query: '.$query);
/* END */
/* Start display of alternating row colors for code listings */
$numrowslimited = mysql_num_rows($results); // Number of rows returned from above query.
echo "<div id=\"table\"><table width=\"50%\" align=\"center\">\n";
echo "<tr><th>Code Display</th><th>Category</th><th>Options</th></tr>\n";
for($i = 0; $i < $numrowslimited; $i++) {
// check for existing color, if it is the same we change it.
$color = ($color =="#E9EBE6")?"#DFE3DC":"#E9EBE6";
$row = mysql_fetch_array($result);
echo "<TR bgcolor=\"$color\">\n";
echo "<TD><b>Title:</b> ".$row['title']."<b><div align=\"right\">Date Added:</b> ".$row['date']."</div></TD>\n";
echo "<TD>".$row['category']."</TD>\n";
echo "<TD>[<a href='edit.php?id=$id'>Edit</a>]</TD>\n";
echo "</TR><br>\n";
}
echo "</table></div>\n";
echo"<p align=\"center\">\n";
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"{$_SERVER['SCRIPT_NAME']}?query=$query&page=$back_page&limit=$limit\">Back</a>\n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b> \n");} // If current page don't give link, just text.
else{
echo("<a href=\"{$_SERVER['SCRIPT_NAME']}?query=$query&page=$ppage&limit=$limit\">$i</a>\n");}
}
if (((($page+$limit) / $limit) >= $pages) && ($pages != 1)) { // If last page don't give next link.
$next_page = $page + $limit;
echo("<a href=\"{$_SERVER['SCRIPT_NAME']}?query=$query&page=$next_page&limit=$limit\">Next</a>\n");}
?>
</table>
That is the page where the rows and queries are priinted out.
search.php
<?
include "config/config.php"; // config file
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('random_code', $db);
$query = $_GET["query"]; //this should be protected..but lets make sure the script works first
if($query != '')
{
$search = mysql_query("SELECT ID, title FROM random WHERE title LIKE '%".$query."%'");
while($listing = mysql_fetch_array($search))
{
?>
<a href=".....?id=<?=$listing["ID"]?>"><?=$listing["title"]?></a><br/>
<?
}
/* Pagination Code */
/* Pagination Options */
}
else
{
?>
<form action="<?=$_SERVER["PHP_SELF"]?>" method="get">
Query containing text: <input type="text" name="query">
<!-- <select name="category">
/*< Select CATEGORIES,echo 'option tags' */ >
</select> -->
<input type="submit" name="submit" value="submit">
<?
}
?>
This code works fine. And is used for searching the table for entries.
I cant seem to see why I am getting that error on list.php
Thanks in Advance, Ryan