Forum Moderators: coopster

Message Too Old, No Replies

accessing mySQL

         

joePIPed

8:37 am on Dec 19, 2003 (gmt 0)

10+ Year Member



Hello. I don't have much experience with php/mySQL, but I am trying to access my data base using php. I want to acces to "comments" column, while choosing a "tid" row. So far I have:

<html>
<body>
<?
mysql_connect ("localhost", "username", "codeword") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db");
$result = mysql_query(SELECT 'replies' FROM xmb_threads WHERE tid=11);
echo "comments: ";
echo $result;
?>
</body>
</html>

Does anyone understand what I am trying to get at? Andif so could someone please help. Thank you.

Paul in South Africa

9:02 am on Dec 19, 2003 (gmt 0)

10+ Year Member



mysql_query() returns a result identifier which then should be passed to mysql_fetch_array().

This should get you started down the right road.

$result = mysql_query(SELECT 'replies' FROM xmb_threads WHERE tid=11);
$resulta = mysql_fetch_array($result);
echo "comments: ";
echo $resulta[replies];

coopster

12:28 pm on Dec 19, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, joePIPed!

You may also want to read Basics of extracting data from MySQL [webmasterworld.com] in the PHP Library [webmasterworld.com] here at WebmasterWorld.

joePIPed

3:38 am on Dec 20, 2003 (gmt 0)

10+ Year Member



Okey my code now looks like this:

<html>
<body>
<?
mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("payingi_xmb1");
$result = mysql_query(SELECT 'replies' FROM xmb_threads WHERE tid=11);
$resulta = mysql_fetch_array($result);
echo "comments: ";
echo $resulta[replies];

?>
</body>
</html>

And I get this error:
Parse error: parse error in /home/payingi/public_html/pages/test3.html on line 6

Also I'm not sure is I have to use an array function because "replies" is only one number.

NOw does this mean line 6 of the whole page or line 6 of where the actual php code starts.
Also what am I doing wrong. I know it's so simple I just can't figure it out.

coopster

1:40 pm on Dec 20, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Line 6 of the whole page, not where the actual php code starts -- it's your query statement that is incorrect. You don't want quotation marks around your field names you are retrieving from the table. I tend to build the query statement as a separate variable, then send the query. It makes it much easier to troubleshoot incorrect query statements especially once they become more complex. You don't have to do do it this way, I'm just offering some friendly advice from experience ;)

$sql = "SELECT replies FROM xmb_threads WHERE tid=11";
$result = mysql_query($sql);

Also I'm not sure is I have to use an array function because "replies" is only one number.

Because the result identifier returned from mysql_query [php.net] is going to be accessed using a mysql function, mysql_fetch_array [php.net], that return results of type
array
. The mysql_result [php.net] function would work for you, but you'll find yourself using the other functions recommended on that page of the manual more often (as mentioned, the other functions are MUCH quicker as well).

Also, you should always use quotes around a string literal array index.


echo $resulta['replies'];

See the PHP Array do's and don'ts [php.net] for more information.

joePIPed

7:09 pm on Dec 20, 2003 (gmt 0)

10+ Year Member



Okey I got it to do what I wanted to. You guys with more experience are going to look at my code and cringe because I know it's wierd. I had already made a similar thing in my website before so I just kinda tweaked that and made it work for what I wanted. Here is what works:

<?

$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("payingi_xmb1");

$tid="1";

$result = mysql_query("SELECT * FROM xmb_threads WHERE tid=$tid ");
while($row = mysql_fetch_array($result)) {
$mem = explode("¦", $row[lastpost]);
$row[replies] = stripslashes($row[replies]);

echo "<a href=\"/forum/viewthread.php?tid=$row[tid]\" target=\"main\" ONMOUSEOVER=\"this.style.color='#666666'\" ONMOUSEOUT=\"this.style.color='#000000'\">comments $row[replies]</a></br>";

echo "last comment by: <a href=\"/forum/member.php?action=viewpro&member=$mem\" target=\"main\" ONMOUSEOVER=\"this.style.color='#666666'\" ONMOUSEOUT=\"this.style.color='#000000'\">$mem[1]</a>";

}
?>

What it inables users of my website to do is comment on any section of the website where I put this code. The code links to a topic on my bullitan board. Check out the final results at <snip> If you click on any of the sections you will see the code at work and you will be able to comment on that section. Thanks for the help. I will have more questions as I delve further into mySQL. I am planning to eventually re-build my entire site and base it around a database so I have a lot of learning to do.

[1][edited by: jatar_k at 4:07 am (utc) on Dec. 22, 2003]
[edit reason] no personal urls thanks [/edit]