Jump to content

pg_connect search database


happygolucky

Recommended Posts

Hello people, im new to this forum, but hopefuly after i become stronger in programming i will be a long standing member. Let me tell you what I have so far and then explain what im trying to do in PHP.

 

I have an sql database with three tables containing details of  local bands.

table 1 = artists with these columns

artistid(pk) int

artist_name text

genre text

 

table 2 = albums

ablumid(pk) int

artistid int

album_name text

year int

 

table 3 = tracks

tackid(pk) int

albumid int

artistid int

track_name text

video_stream boolean

video_download boolean

 

Now i want to create a php interface which has a text box a drop down box and a search button. in the drop down box contains asrtists, albums, tracks, and genre.

 

in the text box the user types the appropriate text on what the are searching for, selects an area in the dropdown box then the appropriate results are returned. then it would say is that band has tracks with videos available.

 

I have no idea where to start with this. any clues or help, suggestions would be greatly appreciated. Its similar to the amazon search in what i am trying to create

 

thanks in advance

 

Im connecting to the database with pg_connect, after that im stuck

Link to comment
Share on other sites

okay. lets say i wanted to have track search, album search and artist search on seperate pages.

This is what i have for the album search page

<html>
    <head><title>Music Mart</title>
    </head>
        <body>
            <p>Search the database:<br />
                <form action="artistsearch2.php" method="post"> Artist Name:<br />
                <input type="text" name="artist_name" size="20" maxlength="40" value="" /><br />
                <input type="submit" value="Search!" />
                </form>
        </p>
            <?php
            /* If the form has been submitted with a supplied artist name */
            if (isset($_POST['$artist_name'])){
            //include "pgsql.class.php"; (throws an error on this line when is active)
            // Connect to server and select database
            $pgsqldb = @pg_connect("host=ginding address.co.uk user=****** password=********** dbname=******")
            or die("Could not connect to db server.");
            /* Set the posted variable to a convenient name */
            $artist_name = $_POST['artist_name'];
            /* Query the employee table */
            $pg->query("SELECT artist_name FROM artists
            WHERE artist_name='$artist_name'");
            /* If records are found, output record */
            if ($pgsqldb->numrows() > 0){
            while ($row = $pgsqldb->fetchobject()) {
            echo "$row->artist_name, $<br />";
            }
            }
            else {
            echo "No Results found.";
            }
            }
            ?>
    </body>
</html>

 

Blue October is a band in the database but it isnt returning anything, nor is any other band. where is this code letting me down.

 

as you can see i have put my own work in but i am still struggling

Link to comment
Share on other sites

Try this:

 

  $sql = "SELECT artist_name FROM artists WHERE artist_name='$artist_name'";
$result = pg_query($pgsqldb, $sql) or die("Query: $sql\nFailed with: " . pg_last_error());
while ($row = pg_fetch_assoc($result)) {
  echo "{$row['artist_name']}<br>";
}

 

If you still get no results from your query, echo the query so you can see what's actually being called.

 

Good luck :)

Link to comment
Share on other sites

Ah, no worries.  Here's how:

 

  $sql = "SELECT artist_name FROM artists WHERE artist_name='$artist_name'";
echo "We're about to run this query: $sql <br>";
$result = pg_query($pgsqldb, $sql) or die("Query: $sql\nFailed with: " . pg_last_error());
while ($row = pg_fetch_assoc($result)) {
  echo "{$row['artist_name']}<br>";
}

 

Then in your output you will see "We're about to run this query: SELECT artist_name ..."

 

You can take a look there and check that the query is what it should be.  If you're not sure if the query is correct or not, post the output here.

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.