m's coming from my db?? help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

eulogy
Forum Newbie
Posts: 14
Joined: Sun Jun 15, 2008 3:07 am

m's coming from my db?? help

Post by eulogy »

I'm hosting my own apache/mysql server for testing and learning PHP.

What I'm trying to do is use phpbb3 to input text into the forum and mysql db,
then print it on the index.php, however when I'm using the following code, all it prints is m.

What am I doing wrong here? As far as I understand it, the $SQL is the query, the $result is the command to query, and the $db_field sorts out the queried information in whichever way I want (mysql_fetch_array, mysql_fetch_assoc, mysql_result - I've tried all of these.)

I've got that much figured out (I think)...

But I really don't understand why this doesn't work. All the names everywhere should be good, I've triple checked them.

Code: Select all

<?PHP
    $user_name = "root";
    $password = "";
    $database = "phpbb3";
    $server = "localhost";
 
    mysql_connect($server, $user_name, $password);
 
    $SQL = "SELECT post_time, poster_id, post_subject, post_text FROM phpbb_posts";
    $result = "mysql_query($SQL)";
    $db_field = "mysql_result($result)";
 
    {
 
        print $db_field['post_time'] . "<BR>";
        print $db_field['poster_id'] . "<BR>";
        print $db_field['post_subject'] . "<BR>";
    }
 
 
?>
And all I get printed when i visit the page looks like this;

Code: Select all

 
m
m
m
 
I'm still learning, and pretty much all on my own. I know basics of coding and html pretty well so I can look at something and understand it usually, but I don't know all the functions and such. I can't see why this doesn't work though. I'm wondering if it's not my code and it has something to do with the db...

Thanks a ton for any info provided.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: m's coming from my db?? help

Post by Ollie Saunders »

You have your function calls inside quotes. They are no being excuted and instead are being treated as strings.

If you say:

Code: Select all

$a = "foo()";
echo $a['something'];
You'll get f because $a is assigned to the literal string "foo()" and 'something' is implicitly cast to 0 (because $a is a string) which represents the first character in the string - 'f'.

The implicit casting of a string to 0 in this instance is one of PHP's stranger behaviours, in fact it makes no sense to me. Personally I think it should go "hey you can't get a associative offset from a string" but meh.

Anyway, you want to take the quotes off your function calls to execute them. Read the section in the manual on strings, and understand that only variables and escape sequences are substituted inside double quotes strings, nothing else (more or less).
eulogy
Forum Newbie
Posts: 14
Joined: Sun Jun 15, 2008 3:07 am

Re: m's coming from my db?? help

Post by eulogy »

I guess I'm in way too deep for me. I thought I was learning something but I guess I wasn't. I've tried every possible combination of quotes and can't get it to work. I'm too frustrated now... I'll try tomorrow. Thank you very much for trying to help me.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: m's coming from my db?? help

Post by Ollie Saunders »

OK eulogy, don't worry. My answer probably complicated things.

Want I'm trying to say is that you want to change this...

Code: Select all

$result = "mysql_query($SQL)";
$db_field = "mysql_result($result)";
...to this...

Code: Select all

$result = mysql_query($SQL);
$db_field = mysql_result($result);
eulogy
Forum Newbie
Posts: 14
Joined: Sun Jun 15, 2008 3:07 am

Re: m's coming from my db?? help

Post by eulogy »

Yes, I tried that.

then I got Warning: Wrong parameter count for mysql_result() on line 11

and reading more about mysql_result, it seems I need to specify the row? so I try 0 and 1

$db_field = mysql_result($result, 0);

both give this error:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource on line 11

so it sounds like it's not getting to the db or atleast the right place, but again I don't know what's wrong. It should be there.

phpbb3 phpbb_posts and all the post_xxx are there, so I don't know where I went wrong.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: m's coming from my db?? help

Post by John Cartwright »

Change

Code: Select all

$db_field = "mysql_result($result)";
to

Code: Select all

while ($db_field = mysql_fetch_assoc($result))
You need to iterate the data set. On a side note, you should also attach or die(mysql_error()) to your queries incase they fail, i.e.

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: m's coming from my db?? help

Post by Ollie Saunders »

Yes, I tried that.

then I got Warning: Wrong parameter count for mysql_result() on line 11
Yep so at least the function is actually being called now. Only it's complaining that you've not used it correctly.
eulogy
Forum Newbie
Posts: 14
Joined: Sun Jun 15, 2008 3:07 am

Re: m's coming from my db?? help

Post by eulogy »

Great! Thank you all very much, I've got the jist of what I need figured out.

Again thank you all very much for being understanding!

Another question though.. I want it so ONLY if the forum_id in the database is equal to 4, then it will actually print the time id and subject.

I've successfully defined $forum_id and can call it (I printed it to make sure), but how would the if statement work?

Code: Select all

<?PHP
    $user_name = "root";
    $password = "";
    $database = "phpbb3";
    $server = "127.0.0.1";
 
    mysql_connect($server, $user_name, $password);
    mysql_select_db ($database);
 
    $SQL = 'SELECT * FROM phpbb_posts';
    $result = mysql_query($SQL) or die(mysql_error());
    $db_field = 'mysql_fetch_assoc($result)';
    $forum_id = ($db_field['forum_id']);
 
 
    while ($db_field = mysql_fetch_assoc($result))
 
    {
    print "<CENTER>";
    print "<B>forum id</B>:";
    print $db_field['forum_id'] . "<BR>";
    print "<B>time of post</B>:";
    print $db_field['post_time'] . "<BR>";
    print "<B>poster</B>:";
    print $db_field['poster_id'] . "<BR>";
    print "<B>subject</B>:";
    print $db_field['post_subject'] . "<BR>";
    print "</CENTER>";
    }
 
?>
I'm assuming
if ($forum_id = 4)
would work, but where would it fit so that it only does the printing near the bottom when true?

Thanks again for everyone's help.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: m's coming from my db?? help

Post by Ollie Saunders »

if ($forum_id = 4) is sort on the right lines but there's a few things to remember.

Firstly we use = to assign value or, in other words, as a synonym of "now becomes".
When you see "$a = 4" you should think "$a now becomes 4".

This is a very different operation to == which is to test for equality. When you see "$a == 4" you should think "$a is 4?". This can then either be true or false.

So you'll want to use == for control structures such as "if".

Once you've got your condition sorted you should use braces to wrap what you want to make conditional.

Code: Select all

if (something) {
    // stuff here only executes if something is true
}
// this will execute regardless
eulogy
Forum Newbie
Posts: 14
Joined: Sun Jun 15, 2008 3:07 am

Re: m's coming from my db?? help

Post by eulogy »

Thank you so much Ole! I'm at work now and I'll give this a test once I get home.


will this work?

Code: Select all

while ($db_field = mysql_fetch_assoc($result))
    
 
    if ($forum_id == 4)
    {
 
        print "<CENTER>";
        print "<B>forum id</B>:";
        print $db_field['forum_id'] . "<BR>";
        print "<B>time of post</B>:";
        print $db_field['post_time'] . "<BR>";
        print "<B>poster</B>:";
        print $db_field['poster_id'] . "<BR>";
        print "<B>subject</B>:";
        print $db_field['post_subject'] . "<BR>";
        print "</CENTER>";
    }
 
I'm just not so sure if "while ($db_field = mysql_fetch_assoc($result))" is going to work with the if.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: m's coming from my db?? help

Post by califdon »

If you only want to output the results from one forum, you should do that in your query, rather than retrieving all the records and looping through and testing them to see which ones you want! In other words, your SQL would be:

Code: Select all

$SQL = "SELECT post_time, poster_id, post_subject, post_text FROM phpbb_posts WHERE $forum_id=4";
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: m's coming from my db?? help

Post by Ollie Saunders »

I forgot to mention last time, you should use the full name....$db_field['forum_id'] not just $forum_id. $forum_id is a totally different variable that hasn't yet been assigned a value.
eulogy
Forum Newbie
Posts: 14
Joined: Sun Jun 15, 2008 3:07 am

Re: m's coming from my db?? help

Post by eulogy »

califdon wrote:If you only want to output the results from one forum, you should do that in your query, rather than retrieving all the records and looping through and testing them to see which ones you want! In other words, your SQL would be:

Code: Select all

$SQL = "SELECT post_time, poster_id, post_subject, post_text FROM phpbb_posts WHERE $forum_id=4";
According to ole's post... can I do WHERE $db_field['forum_id'] = 4 instead of WHERE $forum_id=4 ?

or does my defining $forum_id as $db_field['forum_id'] work?

Sorry for being a huge noob, I'd take a class but I'm afraid I'm too poor ;P I've been reading every night though, doing tutorials etc.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: m's coming from my db?? help

Post by califdon »

eulogy wrote:According to ole's post... can I do WHERE $db_field['forum_id'] = 4 instead of WHERE $forum_id=4 ?

or does my defining $forum_id as $db_field['forum_id'] work?

Sorry for being a huge noob, I'd take a class but I'm afraid I'm too poor ;P I've been reading every night though, doing tutorials etc.
I don't know anyone here who wasn't a noob to begin with. Nobody was born knowing this stuff.

There are numerous ways to get individual field values from a results row from a query. We all have our preferred methods. Basically, the mysql_query() function returns a "result set" which is roughly equivalent to a 2-dimensional array, like a spreadsheet in memory. You use one of the mysql_fetch_...() functions to fetch one row at a time, each time fetching the one after the last one. You can use mysql_fetch_array() or mysql_fetch_assoc() (I can't even remember all of them) to get a row. If you use the _array version, you can address each field with its field name as the index to the row array, so in your case you set an array variable called $db_field to contain the row, so you can refer to the forum_id field in that row as $db_field['forum_id']. Like any php indexed array, you can address its elements using square brackets. You could also refer to it as $db_field[0] if you knew that forum_id was the first item in the array (all arrays are numbered from zero). You can, if you wish, assign the value to a variable, as in:

Code: Select all

$forum_id = $db_field['forum_id'];
which just says, take the value found in the array at that index and store it in a variable named $forum_id. Whether you use the expression $db_field['forum_id'] or assign it to a variable like $forum_id is a matter of choice and to make reading your script easier.

My preference is to use mysql_fetch_assoc(), because then I can call a single function:

Code: Select all

extract($db_field);
and php will assign all the fields to regular variables with the names of the fields. Thus:

Code: Select all

$result = mysql_query($SQL);
while ($row = mysql_fetch_assoc($result)) {  [1.]
    extract($row);   [2.]
    // Now I have all the values for one row from the database in variables,
    // I don't have to assign each one individually,
    // I can just use the variables, like:
    echo "<tr><td>$post_time</td> <td>$poster_id</td> <td>$post_subject</td></tr>";  [3.]
}
[1.] I prefer to name the variable $row, which makes it clear that it is one row of the result. Also, the "while" loop is saying that, as long as mysql_fetch_assoc() returns valid data, do it again! When there are no more rows to fetch, $row will contain a False, which then tells the while loop to stop.
[2.] The extract($row) only works with the _assoc version.
[3.] This would print out one row of an HTML table that would have been started before the code shown above.
eulogy
Forum Newbie
Posts: 14
Joined: Sun Jun 15, 2008 3:07 am

Re: m's coming from my db?? help

Post by eulogy »

Thank you for the insightful post, califdon.

I'm nearing my goal as far as this project.

I've just got a few more things I'd like help with,

Code: Select all

 
    print date('D F j, Y', $db_field['post_time']);
    print " at ";
    print date('g:i a - T', $db_field['post_time']) . "<BR>";
 
prints out as
Sat June 21, 2008 at 1:17 am - PDT
Which is the correct format I wanted, however the timezone is in PDT and I'd like to do the following;

1. If the user is logged into the forums, convert the time to his time-zone
2. Or if possible, based on the specific anonymous visitor's timezone (only when not logged into forum), I don't know how this would be pulled from the client but if it's not as complicated as I think it would be this would be great. I've been reading about date_default_timezone_get, would this help me in what I'm looking for?


As for #1 (logged in user)...

I've looked in phpbb3 database, the phpbb_users row, and the user_timezone field, and for my user it's set to -5.00, which is UTC/GMT -5 (or in other words, EST.).

After selecting user_timezone from phpbb_users, Can I query this and define $db_usertime to a fetch/result, and set the date_default_timezone_set to it? Would I have to

Code: Select all

if ($db_usertime['user_timezone'] == "-5.00")
        ($usertimezone = "EST");
 
if ($db_usertime['user_timezone'] == "-4.00")
        ($usertimezone = "ADT");
etc (for each and every timezone there is)
then

Code: Select all

date_default_timezone_set($usertimezone);
??

I feel like I'm getting a much better hang of this, thanks to you all.

Edit: I just realized I'd also have to pull out the currently logged in user from somewhere (probably from phpbb_sessions? has user_id there) , can I use a variable like that in the query to select the correct row to get the correct timezone field for the right user? This is getting a bit too complicated to worry about for such a small thing..
Post Reply