PDA

View Full Version : Calling specific DB entries


kamu
05-08-2004, 03:57 AM
I'm really new at PHP, I just bought a book to help me modify my boards a little bit. To practice, I tried calling user ID #1 from the database and posting it on the forum home.

I know this may look way off but here's what I did.

// Locate Forum Administrator
$findadmin = mysql_query ($adminquery);
$adminquery = "SELECT username FROM user WHERE userid = '1'";
$whosadmin = "The administrator of this forum is $findadmin";

I added this into index.php and added the variable $whosadmin to the forumhome template.

It's appearing on my board as "The administrator of this forum is" - and stops at the good part >.<


What did I miss or where did I mess up?

filburt1
05-08-2004, 04:08 AM
I'm really new at PHP, I just bought a book to help me modify my boards a little bit. To practice, I tried calling user ID #1 from the database and posting it on the forum home.

I know this may look way off but here's what I did.

// Locate Forum Administrator
$findadmin = mysql_query ($adminquery);
$adminquery = "SELECT username FROM user WHERE userid = '1'";
$whosadmin = "The administrator of this forum is $findadmin";

I added this into index.php and added the variable $whosadmin to the forumhome template.

It's appearing on my board as "The administrator of this forum is" - and stops at the good part >.<


What did I miss or where did I mess up?
Except for the query (but there's a slight problem there too), you are off. :)

The correct code:

$result = mysql_query("SELECT username FROM user WHERE userid = 1");
$user = mysql_fetch_array($result);
$adminusername = $user['username'];

$admintext = "The administrator of this forum is $adminusername.";

What I changed (other than some variable names but yours are not that bad):

The correct procedure is execute the query, saving the result to a variable, and then save the result of mysql_fetch_array(that result) to another variable. mysql_fetch_array() returns one resulting row of the query as an array, where each array key corresponds to a column name in the query.
Do not use quotes for numeric columns in MySQL.

If you are using a vB-powered page, then replace mysql_query() with $DB_site->query() and mysql_fetch_array() with $DB_site->fetch_array().
[/list]

ZranX
05-08-2004, 04:09 AM
Use this code instead.
// Locate Forum Administrator
$findadmin = $DB_site->query_first("
SELECT username
FROM user
WHERE userid = '1'
");

$whosadmin = 'The administrator of this forum is ' . $findadmin['username'];

I'm sure as you learn php some more you will find an infinite amount of other methods of doing this but this will do for now. :p

EDIT: Wrote this post when there was no reply. ;)

kamu
05-08-2004, 04:24 AM
Thanks guys, your examples really helped me out. Your prompt responses are greatly appriciated.