Closed Thread Icon

Topic awaiting preservation: Resource ID #2 Pages that link to <a href="https://ozoneasylum.com/backlink?for=12646" title="Pages that link to Topic awaiting preservation: Resource ID #2" rel="nofollow" >Topic awaiting preservation: Resource ID #2\

 
Author Thread
shattered
Obsessive-Compulsive (I) Inmate

From:
Insane since: Feb 2003

posted posted 03-11-2003 07:09

Hi all,
I want to SELECT * FROM Music_Library, so that it will return all the fields in my database. But it only returns the first field. Anyone knows why?

code:
<?php

require ("include/mysql_conf.php");

// To connect to MYSQL:
//Attempt to connect with database
if (!($link=mysql_connect($hostname, $username, $password)))
{
printError(sprintf("Error connecting to database. Please
check SQL settings:\n
Hostname: %s\n
Username: %s\n
Password: %s\n", $hostname, $username, $password));
exit();
}


//To select the database:
//Make $dbname the active database
//-------------------------------------
if (!mysql_select_db($dbname, $link))
{
printf("Error making the database <b>%s</b> active", $dbname);
printf("error:%d %s", mysql_errno($link), mysql_error($link));
mysql_close($link);
exit();
}

// You should also keep the connection to MySQL connection open in order to
// reduce overhead and achieve faster queries and system performance.

// To keep an active connection to MySQL:
// Attempt to create persistent connection with database

if (!($link=mysql_connect($hostname, $username, $password)))
{
printf("Error connecting to database. Please check SQL settings:\n
Hostname: %s\n
Username: %s\n
Password: %s\n", $hostname, $username, $password);
exit();
}
//$query = "SELECT * FROM Music_Library";
//$query = "SELECT * FROM Music_Library WHERE title = $find";
$getresults = mysql_query("SELECT title FROM Music_Library WHERE title LIKE '%$find%'");

$resultsnumber = mysql_numrows($getresults);

IF ($resultsnumber == 0){
PRINT "Your search returned no results. Try other keyword(s).";
}

ELSEIF ($resultsnumber > 0){
PRINT "Your search returned $resultsnumber results<BR><BR>";
for($count = 0; $count<$resultsnumber; $count++){
$body = mysql_result($getresults,$count);

//tighten up the results
$body2print = $getresults;
$cnote = $count+1;
PRINT "$cnote. <a href=http://www.radio.deltapath.com/dedicationmail.php>
<i>$body2print</i></a><BR>";
}
}
?>




[This message has been edited by shattered (edited 03-11-2003).]

[This message has been edited by shattered (edited 03-11-2003).]

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 03-11-2003 09:23

That could be becuase the very next line of your code alters the query variable again...so all it's doing is not running the $query = "SELECT * FROM Music_Library"; ...the query you end up running instead is $query = "SELECT * FROM Music_Library WHERE title = $find";

if you want ot pull out all the records you could stick both of those queries in seperate functions and to grab all records have it run one function, to grab specific records have it run the other function.

shattered
Obsessive-Compulsive (I) Inmate

From:
Insane since: Feb 2003

posted posted 03-11-2003 10:54

i'm sorry
here's the updated codes, I know I have to run a while loop to display all the fields.. but I'm not sure how to go about doing it:

code:
<?php

require ("include/mysql_conf.php");

// To connect to MYSQL:
//Attempt to connect with database
if (!($link=mysql_connect($hostname, $username, $password)))
{
printError(sprintf("Error connecting to database. Please
check SQL settings:\n
Hostname: %s\n
Username: %s\n
Password: %s\n", $hostname, $username, $password));
exit();
}


//To select the database:
//Make $dbname the active database
//-------------------------------------
if (!mysql_select_db($dbname, $link))
{
printf("Error making the database <b>%s</b> active", $dbname);
printf("error:%d %s", mysql_errno($link), mysql_error($link));
mysql_close($link);
exit();
}

// You should also keep the connection to MySQL connection open in order to
// reduce overhead and achieve faster queries and system performance.

// To keep an active connection to MySQL:
// Attempt to create persistent connection with database

if (!($link=mysql_connect($hostname, $username, $password)))
{
printf("Error connecting to database. Please check SQL settings:\n
Hostname: %s\n
Username: %s\n
Password: %s\n", $hostname, $username, $password);
exit();
}

$getresults = mysql_query("SELECT * FROM Music_Library WHERE artist LIKE '%$find%'");

$resultsnumber = mysql_num_rows($getresults);

IF ($resultsnumber == 0){
PRINT "Your search returned no results. Try other keyword(s).";
}

ELSEIF ($resultsnumber > 0){
PRINT "Your search returned $resultsnumber results<BR><BR>";
for($count = 0; $count<$resultsnumber; $count++){
$body = mysql_result($getresults,$count);

// tighten up the results
$body2print = $body;
$cnote = $count+1;
PRINT "$cnote. <a href=http://www.radio.deltapath.com/dedicationmail.php>
<i>$body2print</i></a><BR>";
}
}
?>




[This message has been edited by shattered (edited 03-11-2003).]

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 03-11-2003 13:00

ok...first off I'd replace that last ELSEIF just with an ELSE...unless you're planning on adding another condition (which i don't think you are) there's no point in creating a nested if statement.

Now...you don't actually need a while loop there...I just completed a piece of code that does basically the same thing you want yours to do...selects all the records in the table then displays them one after another with certain formatting until it hits the end of the records. the code I used uses a for() statement, not a while. I'm not sure of a while would work here.

Here's what I did: My for statement reads thusly

for($i = 0; $i< MYSQL_NUM_ROWS($result); $i++)

basically, the variable $i is my counter for it...it counts the number of times the for loop is run, the $i++ at the end increments it by 1 each time the loop repeats. The $i< MYSQL_NUM_ROWS($result) repeats the for loop as long as the incrememter is less than the number of rows in the query result which I have stored in the variable $result.

Inside the for loop you then use different variables to store each of the different parts of the query result so they can be called up in the formatting. My for loop is part of a website statistics cruncher, so what it's displaying is unique IP addresses that have visited the site and the number of pages they've viewed. So, to use the $IP variable as an example, I drew it out of the query result by doing:

$IP = mysql_result($result, $i, "IP");

So it stores in the variable $IP the IP field from the query result of $result, but only the record who's number is equal to the incrementer $i.

After you've pulled out all the date from the query that you're going ot need, display it how you choose, echoing out the variables you've just created and all html code you're going to use to format them.

Make sure to put the semicolon after each line of everything inside the for loop, but nothing of the loop itself and you should be fine.

Good luck!



[edit]Damn fingers! Learn to spell![/edit]

[This message has been edited by Skaarjj (edited 03-11-2003).]

« BackwardsOnwards »

Show Forum Drop Down Menu