PDA

View Full Version : Need some PHP and MySQL help


m1a2x3x7
06-18-2007, 08:37 PM
A little background. I've created a site when I can post blogs. Really simple, really generic. I have an admin login page where I can type in the title and the blog.

All this works great I also created a page that displays the blogs. All that works great.

I'm trying to create a search page. I can type a keyword in and it brings up the search page and it tells what I'm searching for. I just can't get the database to retrieve any of the blogs.

Iím doing a like statement in my query but Iím having problems figuring out the syntax for using a PHP variable in the sql statement. Any help would be great.

<?php

ob_start();

require_once("connections/connect.php");

session_start();

$query = sprintf("SELECT title FROM blogs WHERE (title) LIKE values ('$keyword') DESC");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
$numResults = mysql_num_rows($result);


$keyword = $_GET['Keyword'];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Schultz | Studio - Blog</title>
<link href="blogcss.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
background-color: #CCCCCC;
}
.style2 {color: #98D05B}
-->
</style></head>

<body>
<div id="wrapper">
<div id="banner">
<div id="loginlink"><a href="login.php"><img src="images/login.gif" width="55" height="10" border="0" /></a></div>
</div>
<div id="search">
<table width="796" height="30" border="0">
<tr>
<td width="454" class="font-date"><?php
echo date('F j, Y');
?></td>
<td align="right" valign="top"><img src="images/search.gif" width="72" height="20" /></td>
<td align="right" valign="top"><form id="form1" name="form1" method="get" action="search.php">
<input name="Keyword" type="text" id="Keyword"/>
<input name="Search" type="submit" id="Search" value="Find"/>
</form></td>
</tr>
</table>
</div>
<div id="body">
<p class="font-date">Your search for <b><?php echo $keyword; ?></b> has turned up <?php if (empty$numResults); {
echo $numResults; ?> results.</p>
<p class="font-date"> <table width="526" border="0">
<?php do { ?>
<tr>
<td width="385" class="blog_title"><?php echo $row['title'];?></td>
</tr>
<tr>
<td class="date"><?php echo $row['date'];?></td>
</tr>
<tr>
<td class="blog"><?php echo nl2br($row['blog']);?></td>
</tr>
<tr>
<td class="blog">.................................................. .................................................. ....</td>
</tr>

<?php } while ($row = mysql_fetch_array($result));

?>
</table>&nbsp;</p>
</div>
<div id="footer">
<div id="footerinfo">Copyright &copy; 2007 Schultz | Studio. All Rights Reserved.</div>
</div>
</div>
</body>
</html>


ps I've searched, and found may different ways how mysql would read a PHP variable but none of them have worked right so IDK

Thanks

davidj
06-18-2007, 08:51 PM
it could be the way your writing your SQL

try this...

select * from table where field like '$var%'

m1a2x3x7
06-19-2007, 12:10 AM
ok ive made some changes and it will now show with this

$keyword = $_GET['Keyword'];
$query = sprintf("SELECT * FROM blogs WHERE title LIKE '$keyword';");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

but if i use the % it will no longer show a result any ideas why?

davidj
06-19-2007, 07:09 AM
does it return correctly? do you get results back on partial searches...

example...
dav would find david, davison, dave etc

m1a2x3x7
06-19-2007, 10:34 AM
no it has the be they exact title i know thats why i need the % but if i use that the search no longer shows a result

davidj
06-19-2007, 11:11 AM
remove the semicolon from $keyword and add the % again


$query = sprintf("SELECT * FROM blogs WHERE title LIKE '$keyword';");

m1a2x3x7
06-19-2007, 05:12 PM
I've tried

$keyword = $_GET['Keyword'];
$query = sprintf("SELECT * FROM blogs WHERE title LIKE '$keyword%' ");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

This

$keyword = $_GET['Keyword'];
$query = sprintf("SELECT * FROM blogs WHERE title LIKE '%$keyword' ");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

This

$keyword = $_GET['Keyword'];
$query = sprintf("SELECT * FROM blogs WHERE title LIKE '$keyword'% ");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

This

$keyword = $_GET['Keyword'];
$query = sprintf("SELECT * FROM blogs WHERE title LIKE %'$keyword' ");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

This

$keyword = $_GET['Keyword'];
$query = sprintf("SELECT * FROM blogs WHERE title LIKE '%$keyword%' ");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

and this

$keyword = $_GET['Keyword'];
$query = sprintf("SELECT * FROM blogs WHERE title LIKE %'$keyword'% ");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

none of them will show a result =/

again works great with out the % but i have to have the exact title name for it to show

its like once i put the % the database no longer works

m1a2x3x7
06-19-2007, 05:50 PM
ok i might know my problem but im not sure. when i created the database i was going to use a fulltext search so a gave it a full text index. would this not allow the %?

m1a2x3x7
06-19-2007, 06:10 PM
ok i dont know if that was the reason or not but i got it to work with the FULLTEXT