Jump to content

Archived

This topic is now archived and is closed to further replies.

bobleny

I can't figuer this database connect stuff!

Recommended Posts

I don't belive I have this correct. all it says on the page is Resource id #4.
so what is wrong with this code?

[code]mysql_connect('db4.awardspace.com:3306',$username,$password);
mysql_select_db($database);
$forumpass = mysql_query("SELECT forumpass FROM forum_users WHERE forumname = '" . $inputname . "'");

echo $forumpass;[/code]

Share this post


Link to post
Share on other sites
There's nothing wrong with that -- mysql_query() returns a PHP "resource", better known as a recordset. If you want do anything with it, you need to fetch the information in each of the records -- mysql_fetch_assoc() is a good way to do this. The PHP manual has plenty of good examples.

Share this post


Link to post
Share on other sites
Now all it says is, "Array". All the examples that I've seen have the mysql_fetch_assoc() in a while loop. I would think that what I have would work? It fetches the $query results and assigns it as $forumpass, right?

[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$inputname[!--colorc--][/span][!--/colorc--] = [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]'Bob Leny'[!--colorc--][/span][!--/colorc--];

[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]mysql_connect[!--colorc--][/span][!--/colorc--]([!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]'db4.awardspace.com:3306'[!--colorc--][/span][!--/colorc--],[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$username[!--colorc--][/span][!--/colorc--],[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$password[!--colorc--][/span][!--/colorc--]);
[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]mysql_select_db[!--colorc--][/span][!--/colorc--]([!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$database[!--colorc--][/span][!--/colorc--]);
[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$query[!--colorc--][/span][!--/colorc--] = [!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]mysql_query[!--colorc--][/span][!--/colorc--]([!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]"SELECT forumpass FROM forum_users WHERE forumname = '"[!--colorc--][/span][!--/colorc--] . [!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$inputname[!--colorc--][/span][!--/colorc--] . [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]"'"[!--colorc--][/span][!--/colorc--]);
[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$forumpass[!--colorc--][/span][!--/colorc--] = [!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]mysql_fetch_assoc[!--colorc--][/span][!--/colorc--]([!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$query[!--colorc--][/span][!--/colorc--]);

[!--coloro:#009900--][span style=\"color:#009900\"][!--/coloro--]echo[!--colorc--][/span][!--/colorc--] [!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]$forumpass[!--colorc--][/span][!--/colorc--];

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]It fetches the $query results and assigns it as $forumpass, right?[/quote]
Yes, an [b]array[/b] named $forumpass. So ...

[code]mysql_connect('db4.awardspace.com:3306',$username,$password);
mysql_select_db($database);
$query = mysql_query("SELECT forumpass FROM forum_users WHERE forumname = '" . $inputname . "'");
$row = mysql_fetch_assoc($query);
$forumpass = $row['forumpass'];
echo $forumpass;[/code]

Share this post


Link to post
Share on other sites
In general, though, you should probably always have this in a while loop anyway.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.