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
https://forums.phpfreaks.com/topic/81721-solved-new-to-php/
Share on other sites

i moved this as it has no use of classes/objects and isn't OOP. Please get yourself acquainted with the boards and pick a suitable place before posting.

 

also - if you get errors, please post them as you see them and we'll probably be able to help better.

Link to comment
https://forums.phpfreaks.com/topic/81721-solved-new-to-php/#findComment-415039
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
https://forums.phpfreaks.com/topic/81721-solved-new-to-php/#findComment-416762
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
https://forums.phpfreaks.com/topic/81721-solved-new-to-php/#findComment-416764
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
https://forums.phpfreaks.com/topic/81721-solved-new-to-php/#findComment-416770
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
https://forums.phpfreaks.com/topic/81721-solved-new-to-php/#findComment-416772
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.