How do I Query a DB and post the results -
Bretth - July 3, 2008 - 21:12
so i have TestMYSQL Query page on my website and i am trying to query and post the results of the role db, and once i can get that figured out - then i can query and post the results of another db I created titled front_page_by_role . This db has the role, description, sort, and include fields.
Basically i have been trying to get a simple query to work before i go and query the other DB. Any Help?
p.s. i see the database's are being queried in modules and such but its hard to pick out the code I need and get it to work.

what else is needed here?
what else is needed here?
<?php$sql = "SELECT rid, FROM {front_page_by_role}
print ($output);
?>
Well $output isn't specified
Well $output isn't specified as anything, you are missing
";at the end of the sql string.<?php$query= "SELECT rid FROM {front_page_by_role}";
$output = db_fetch_object($query);
print $output;
?>
That would be right..... but what module makes the front_page_by_role table?
Shouldn't that
Shouldn't that be?
<?php$query= "SELECT rid FROM {front_page_by_role}";
$output = db_fetch_object(db_query($query));
print $output;
?>
James T
Action Medical Research - www.action.org.uk
this produced
this produced this....
Object id #54
the suggestion above James did not work at all.
How could i get it to actually list all of the rids from that table I made?
oh... i did not use a module to create the front page by role table, I copied the role DB and added fields to it in PHP myadmin.
<?php$query= "SELECT rid
<?php$query= "SELECT rid FROM {front_page_by_role}";
$results = db_query($query);
while($output = db_fetch_object($results)){
print $output->rid;
}
?>
oh ho, thats more like it, i
oh ho, thats more like it, i might need help putting it into a table and ill also be quering more than just rid.
Feel free to get more than
Feel free to get more than just rid. The variable $output is an object and you can get any of the values you selected with the same object syntax as accessed the rid. Putting it in a table is a bit more work. Does someone have a good example? If not I can give one.
<?php$query= "SELECT rid
<?php$query= "SELECT rid FROM {front_page_by_role}";
$results = db_query($query);
while($output = db_fetch_object($results)){
print $output->rid;
}
$query= "SELECT name FROM {front_page_by_role}";
$results = db_query($query);
while($output = db_fetch_object($results)){
print $output->name;
}
$query= "SELECT sort FROM {front_page_by_role}";
$results = db_query($query);
while($output = db_fetch_object($results)){
print $output->sort;
}
$query= "SELECT include FROM {front_page_by_role}";
$results = db_query($query);
while($output = db_fetch_object($results)){
print $output->include;
}
?>
this is what I had to do to get all 4 fields to show up... shouldn't I be able to get them to show up using only 1 query?
when im finished with this,
when im finished with this, i will probably be putting it in the Advanced Front Page module and one will be able to sort the roles so you can choose what page a person signing in will get put on.
<?php$query= "SELECT name,
<?php$query= "SELECT name, sort, include, rid FROM {front_page_by_role}";
$results = db_query($query);
while($output = db_fetch_object($results)){
print $output->rid;
print $output->name;
print $output->sort;
print $output->include;
}
?>
print $output->rid ;
print $output->rid ;
print "\n ";
print $output->name ;
print "\n";
print $output->sort ;
print "\n";
print $output->include ;
print "\n | ";
}
so i added that to make the results presentable. but.... I wanna be able to sort the results mostly based on the SORT field and to do that I need the results to be in an array. I tried to use mysql_fetch_array instead of db_query but wasnt able to get that to work. I have also been reading on arrays here but I just cant figure out how to put these db results into an array.
<?php$query= "SELECT name,
<?php$query= "SELECT name, sort, include, rid FROM {front_page_by_role}";
$results = db_query($query);
$result_array = array();
while($output = db_fetch_array($results)){
$result_array[] = $output; //$output is an array with keys of 'name', 'sort', 'include', and 'rid'
}
?>
didnt work for me.
didnt work for me.
What didn't work? You need
What didn't work? You need to be more specific to what didn't work. It didn't store any of your results in the array? It didn't store them how you were expecting? There was an error thrown? Please provide more information.
I figured out that what you
I figured out that what you gave me just put the data into the array and now im figuring out how to print that out.
cool... figured that out and
cool... figured that out and put it into a table. now im messing with the table some and yea....