Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Problem query'ing mysql database. Please help

Codes here !

Moderators: macek, egami, gesf

Problem query'ing mysql database. Please help

Postby Unforgiven » Wed Jul 09, 2003 6:59 am

$weapon = mysql_query("SELECT Weapons.Name FROM Weapons, Members WHERE Members.Weapon='Weapons.id' and Members.Username = '$Username';");
That query returns Resource ID #5, when it should be returning a string. I was just wondering if anybody had experienced this problem before, or if someone knows what is going wrong. Thanks in advance.

~Unforgiven
Unforgiven
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Jul 09, 2003 6:56 am

Postby liquedus » Wed Jul 09, 2003 10:03 am

well you are on the right track.. it will contain a resource id and now you need a function to extract that information out of that resource... try looking at php.net's mysql functions :wink:
liquedus
php-forum Active User
php-forum Active User
 
Posts: 266
Joined: Tue Apr 08, 2003 5:18 am
Location: Ottawa, Canada

Re: Problem query'ing mysql database. Please help

Postby swirlee » Wed Jul 09, 2003 1:56 pm

Unforgiven wrote:$weapon = mysql_query("SELECT Weapons.Name FROM Weapons, Members WHERE Members.Weapon='Weapons.id' and Members.Username = '$Username';");
That query returns Resource ID #5, when it should be returning a string.


Your analysis that it "should be" returning a string is incorrect. According to the docs, mysql_query() "should be" returning a resource, and that appears to be the case. Now you need to fetch the result of that query by passing the name of the resource to mysql_fetch_array() or mysql_fetch_assoc() like this:

Code: Select all
<?
   $weapon = mysql_query('SELECT Weapons.Name FROM Weapons, Members WHERE Members.Weapon=Weapons.id and Members.Username = \'' . $Username. '\'');
   $weapon_array = mysql_fetch_assoc($weapon);
?>


This will populate $weapon_array with the returned row. Note that if you expect the query to return more than one row, you'll want to place mysql_fetch_assoc() a while() loop or similar. Refer to the docs for help with the usage. Also note that I cleaned up your syntax a bit. I could lecture people on syntax, legibility, optimization, conventions, etc., but for your sake I won't. Enjoy.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Unforgiven » Wed Jul 09, 2003 4:33 pm

Thanks for your help, I got it working :)
Unforgiven
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Jul 09, 2003 6:56 am


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.