happygolucky Posted May 6, 2009 Share Posted May 6, 2009 <html> <head><title>Media Mart</title> </head> <body> <p>Search Media Mart:<br /> <form action="index.php" method="post">Keyword:<br /> <input type="text" name="keyword" size="20" maxlength="40" value="" /><br />Field:<br /> <select name="field"> <option value="">Choose field:</option> <option value="artist_name">Artist</option> <option value="album_name">Album</option> <option value="track_name">Track</option> </select> <input type="submit" value="Search!" /> </form> </p> <?php // set db variables // Edit these with your own info $dbhost = "xxxxxxxxx.xxxx.xxxx.co.uk"; $dbuser = "xxxxxx"; $dbpass = "xxxxxxxxxx"; $dbname = "xxxxxx"; /* If the form has been submitted with a supplied keyword */ if (isset($_POST['field'])){ include "pgsql.class.php"; /* Connect to server and select database */ $pgsqldb = new pgsql($dbhost,$dbuser,$dbpass,$dbname); $pgsqldb->connect(); /* Set the posted variables to a convenient name */ $keyword = $_POST['keyword']; $field = $_POST['field']; /* Create the query based on the type of search */ if ($field == "artist_name") { $pgsqldb->query("SELECT artist_name, genre FROM artists WHERE artist_name='$keyword'"); } else if($field == "album_name") { $pgsqldb->query("SELECT album_name FROM albums WHERE album_name='$keyword'"); } else if($field == "track_name") { $pgsqldb->query("SELECT track_name FROM tracks WHERE track_name='$keyword'"); } // if records are found, output them if ($pgsqldb->numrows() > 0){ while ($row = $pgsqldb->fetchobject()) { if ($field == "artist_name") { echo "Artist: $row->artist_name<br />Genre: $row->genre<br />"; } else if($field == "album_name"){ echo "Album: $row->album_name<br />"; } else if($field == "track"){ echo "Track: $row->track_name<br />"; } } } else { echo "No Results found."; } } ?> </body> </html> <?php class pgsql { private $linkid; // PostgreSQL link identifier private $host; // PostgreSQL server host private $user; // PostgreSQL user private $passwd; // PostgreSQL password private $db; // PostgreSQL database private $result; // Query result private $querycount; // Total queries executed /* Class constructor. Initializes the $host, $user, $passwd and $db fields. */ function __construct($host, $db, $user, $passwd) { $this->host = $host; $this->user = $user; $this->passwd = $passwd; $this->db = $db; } /* Connects to the PostgreSQL Database */ function connect(){ try{ $this->linkid = @pg_connect("host=$this->host dbname=$this->db user=$this->user password=$this->passwd"); if (! $this->linkid) throw new Exception("Could not connect to PostgreSQL server."); } catch (Exception $e) { die($e->getMessage()); } } /* Execute database query. */ function query($query){ try{ $this->result = @pg_query($this->linkid,$query); if(! $this->result) throw new Exception("The database query failed."); } catch (Exception $e){ echo $e->getMessage(); } $this->querycount++; return $this->result; } /* Determine total rows affected by query. */ function affectedRows(){ $count = @pg_affected_rows($this->linkid); return $count; } /* Determine total rows returned by query */ function numRows(){ $count = @pg_num_rows($this->result); return $count; } /* Return query result row as an object. */ function fetchObject(){ $row = @pg_fetch_object($this->result); return $row; } /* Return query result row as an indexed array. */ function fetchRow(){ $row = @pg_fetch_row($this->result); return $row; } /* Return query result row as an associated array. */ function fetchArray(){ $row = @pg_fetch_array($this->result); return $row; } /* Return total number of queries executed during lifetime of this object. Not required, but interesting nonetheless. */ function numQueries(){ return $this->querycount; } } /* Return the number of fields in a result set */ function numberFields() { $count = @pg_num_fields($this->result); return $count; } /* Return a field name given an integer offset. */ function fieldName($offset){ $field = @pg_field_name($this->result, $offset); return $field; } function getResultAsTable() { if ($this->numrows() > 0) { // Start the table $resultHTML = "<table border='1'>\n<tr>\n"; // Output the table headers $fieldCount = $this->numberFields(); for ($i=0; $i < $fieldCount; $i++){ $rowName = $this->fieldName($i); $resultHTML .= "<th>$rowName</th>"; } // end for // Close the row $resultHTML .= "</tr>\n"; // Output the table data while ($row = $this->fetchRow()){ $resultHTML .= "<tr>\n"; for ($i = 0; $i < $fieldCount; $i++) $resultHTML .= "<td>".htmlentities($row[$i])."</td>"; $resultHTML .= "</tr>\n"; } // Close the table $resultHTML .= "</table>"; } else { $resultHTML = "<p>No Results Found</p>"; } return $resultHTML; } function pageLinks($totalpages, $currentpage, $pagesize, $parameter) { // Start at page one $page = 1; // Start at record 0 $recordstart = 0; // Initialize page links $pageLinks = ''; while ($page <= $totalpages) { // Link the page if it isn't the current one if ($page != $currentpage) { $pageLinks .= "<a href=\"".$_SERVER['PHP_SELF']. "?$parameter=$recordstart\">$page</a> "; // If the current page, just list the number } else { $pageLinks .= "$page "; } // Move to the next record delimiter $recordstart += $pagesize; $page++; } return $pageLinks; } ?> im recieving an error message from the second part of the code that reads.... Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home2/webusers/07/344740/public_html/mediamart/pgsql.class.php on line 3 this is after i submit the search. the databse was built with PuTTy onto an apache server. i may have some sill errors as im new to this. if anyone can tell me where my mistakes are id appreciate it. Thankyou Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 What do you mean the database was built with PuTTy? ??? What PHP version are you running? Quote Link to comment Share on other sites More sharing options...
happygolucky Posted May 7, 2009 Author Share Posted May 7, 2009 putty is a programme to build PSQL databases and its the latest version of php Quote Link to comment Share on other sites More sharing options...
happygolucky Posted May 7, 2009 Author Share Posted May 7, 2009 sorry, its v4.4.8 Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 putty is a programme to build PSQL databases Really? So there are 2 programs called PuTTy? I only know one which is used for SSH. private keyword is only available in PHP 5. Change them to var. Quote Link to comment Share on other sites More sharing options...
happygolucky Posted May 7, 2009 Author Share Posted May 7, 2009 if you type psql into putty it willask for login details to your databse. then you can create within putty from there. can you please explain what needs changing? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 private keyword is only available in PHP 5. Change them to var. Did you read? Quote Link to comment Share on other sites More sharing options...
happygolucky Posted May 7, 2009 Author Share Posted May 7, 2009 sorry. yes is that $var or just var bear with me im new to this and learning the ropes Quote Link to comment Share on other sites More sharing options...
happygolucky Posted May 7, 2009 Author Share Posted May 7, 2009 its okay i got it. im now gettting error on line 20, which is try? is that specific for php 5? ir are my problems elsewhere? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Please post the error. Quote Link to comment Share on other sites More sharing options...
happygolucky Posted May 7, 2009 Author Share Posted May 7, 2009 Parse error: syntax error, unexpected '{' in /home2/webusers/07/344740/public_html/mediamart/pgsql.class.php on line 20 i checked it and as far as i can tell { is needed Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 7, 2009 Share Posted May 7, 2009 Try/catch is also php5 specific. The end of life and end of support for php4 was more than one year ago. You need to upgrade to php5. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.