Jump to content

[SOLVED] new to php


nero1009

Recommended Posts

hi guys im new too using classes and objects to program stuff

i made a little bit of code

but i keep getting weird errors

what exactly am i doing wrong

This is for a search page

<html>

 

<head></head>

<title>Search Comments </title>

<body><h1 style="color: #444; margin: 10px 0px;"> Search Comments</h1>

 

<form action="search_comments.php" method="get" name="search">

  <input name="q" type="text" value="<?php echo $query; ?>">

      <input name= "search_comments.php" type="submit" value="Search ">

 

</form>

 

 

 

  </div>

 

 

</div>

 

</body>

</html>

 

 

<?php

//connect to the database

require '/Library/WebServer/Documents/sitesadmin/comments_dev/functions/sqlF.php';

connect_comments();

 

require '/Library/WebServer/Documents/sitesadmin/comments_dev/includes/config.php';

 

config(1);

require PATH.'functions/commentF.php';

require PATH.'classes/asset.php';

require PATH.'functions/dateF.php';

 

// results to display per page

$limit = 10;

 

// Get the search variable from URL

  $var = @$_POST['q'] ;

 

//trim whitespace from the stored variable

  $trimmed = trim($var);

 

//string and display a message.

if ($trimmed == "") {

  $resultmsg =  "<p>Search Error</p> <p>Please enter a search...</p>" ;

   

  }

  echo "<p>Please enter a search...</p>";

 

  if (!isset($var)){

  $resultmsg =  "<p>Search Error</p> <p>We don't seem to have a search parameter! </p>" ;

 

  }

 

// table and field names for the SQL query

$query = mysql_query("SELECT * FROM tblcommentsall WHERE comment_text = LIKE '%$trimmed%''")  or die("Couldn't execute query");

 

 

            $numresults = mysql_query($query);

 

 

//  no results

if ($numresults == 0){

 

  echo "<h4>Results</h4>";

  echo "<p>Sorry, your search: " . $trimmed . " returned zero results</p>";

  }

    //  determine if s has been passed to script, if not use 0

    if (empty($s)) {

  $s=0;

  }

 

  // get results

  $query = "  LIMIT $s,$limit";

  $result = @mysql_fetch_array($query);

 

 

 

// display what the person searched for

echo "<p>You searched for: " . $var . "</p>";

 

// begin to show results

echo "Results";

$result = 1 + $s ;

 

?>

 

So when I run this code i can not connect for query is this the wrong way to do this

 

please help

Link to comment
Share on other sites

 

<?php

//connect to the database

require '/Library/WebServer/Documents/sitesadmin/comments_dev/functions/sqlF.php';

connect_comments();

 

require '/Library/WebServer/Documents/sitesadmin/comments_dev/includes/config.php';

 

config(1);

require PATH.'functions/commentF.php';

require PATH.'classes/asset.php';

require PATH.'functions/dateF.php';;

 

query for the search

 

 

 

Link to comment
Share on other sites

Can you post the relevent code? Nowhere in what you have posted do you make a connection.

 

You might also change your code to...

 

$query = mysql_query("SELECT * FROM tblcommentsall WHERE comment_text = LIKE '%$trimmed%''")  or die(mysql_error());

 

so we get an actual error message.

Link to comment
Share on other sites

You need

 

mysql_connect('localhost', 'user', 'password');
mysql_select_db('db_name');

 

somewhere before the mysql queries, so you then have actually connected to the database; unless the connection details are within

'/Library/WebServer/Documents/sitesadmin/comments_dev/functions/sqlF.php'?

 

Also, in

$query = mysql_query("SELECT * FROM tblcommentsall WHERE comment_text = LIKE '%$trimmed%''")  or die(mysql_error());

 

why do you have an extra ' in your query - that extra apostraphe would probably cause errors. (As thorpe just said, he beat me to it).

 

Sam

 

Link to comment
Share on other sites

<?php

//connect to the database

require '/Library/WebServer/Documents/sitesadmin/comments_dev/functions/sqlF.php';

connect_comments();

 

require '/Library/WebServer/Documents/sitesadmin/comments_dev/includes/config.php';

 

config(1);

require PATH.'functions/commentF.php';

require PATH.'classes/asset.php';

require PATH.'functions/dateF.php';;

 

// results to display per page

$limit = 10;

 

// Get the search variable from URL

  $var = @$_POST['q'] ;

 

//trim whitespace from the stored variable

  $trimmed = trim($var);

 

//string and display a message.

if ($trimmed == "") {

  $resultmsg =  "<p>Search Error</p> <p>Please enter a search...</p>" ;

   

  }

  echo "<p>Please enter a search...</p>";

 

  if (!isset($var)){

  $resultmsg =  "<p>Search Error</p> <p>We don't seem to have a search parameter! </p>" ;

 

  }

 

// table and field names for the SQL query

$query = mysql_query("SELECT * FROM tblcommentsall WHERE comment_text = LIKE '%$trimmed%'")  or die("Couldn't execute query");

 

 

            $numresults = mysql_num_rows($query);

 

 

//  no results

if ($numresults == 0){

 

  echo "<h4>Results</h4>";

  echo "<p>Sorry, your search: " . $trimmed . " returned zero results</p>";

  }

    //  determine if s has been passed to script, if not use 0

    if (empty($s)) {

  $s=0;

  }

 

  // get results

  $query = "  LIMIT $s,$limit";

  $result = @mysql_fetch_array($query);

 

 

 

// display what the person searched for

echo "<p>You searched for: " . $var . "</p>";

 

// begin to show results

echo "$Results";

$result = 1 + $s ;

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.