PDA

View Full Version : Username mysql


FreshFroot
11-08-2010, 06:59 AM
So I have a small problem. I must be messing up somewhere.

I basically have a few variables, which gather some information from the "search" table.

One of those data variables is the "userid".

Now I stored this userid into a variable. And what I want to do is get the username.

So I am running the following query only to NOT get the username, but a resource code error. I assume I am missing something here.


$mrs_user = $mrs_result["userid"];

$the_username = "SELECT username FROM user WHERE userid = " . $mrs_user . "";

$mrs_username = mysql_query($the_username);



Then $mrs_username is called in a search resultbit template.

So what is it, that I am doing wrong here? I'm sure it's something stupid and yet simple.

I get an error of " Resource id #20" for where the username should be placed.

Thanks

kh99
11-08-2010, 10:44 AM
I think the problem is that the return from mysql_query is a resource, and you need to use mysql_result (http://us2.php.net/manual/en/function.mysql-result.php) to get the value.

BTW, is there a reason you're not using the vb database functions (just have to ask :))?

FreshFroot
11-08-2010, 04:57 PM
I think the problem is that the return from mysql_query is a resource, and you need to use mysql_result (http://us2.php.net/manual/en/function.mysql-result.php) to get the value.

BTW, is there a reason you're not using the vb database functions (just have to ask :))?
I'll have to try that later tonight.

As for the vB database function, which one is that? Are you refering to "$vbulletin->-db" Because if so, I tried that and got the same error.

kh99
11-08-2010, 05:13 PM
As for the vB database function, which one is that? Are you refering to "$vbulletin->-db" Because if so, I tried that and got the same error.

Yeah, that's what I meant. There probably isn't any reason you can't use mysql functions directly, but using vbulletin functions you probably had the same problem for the same reason - you needed to call $db->fetch_array() on your result. But if you know you only want one row you can use $db->query_first and what you get back will be an array with the column names as keys. So your code would be


$result = $db->query_first($the_username);
$mrs_username = $result['username'];

FreshFroot
11-09-2010, 02:16 AM
Amazing seems to work perfectly now.

So the step I was missing was

$mrs_username = $result['username'];

So I guess my question now is why do we use $result['username]?

Is it because $result is supposed to be an array? And that we can the array of username within $result?

kh99
11-09-2010, 08:07 AM
Is it because $result is supposed to be an array? And that we can the array of username within $result?

Yeah (I think). I guess you can think of what you get back as a database row that's been saved in an array using the column names as the keys, and even if you're only getting one column it's still an array (with one entry).

FreshFroot
11-09-2010, 03:35 PM
Yeah (I think). I guess you can think of what you get back as a database row that's been saved in an array using the column names as the keys, and even if you're only getting one column it's still an array (with one entry).
Wow I totally mis wrote that sentance haha.

I meant pretty much what you said. You would essentially get back rows, but each row of an array would contain multiple data. eg. userid, username and you can choose which to retrive.

Thanks a lot for your help kh99. You helped me learn something new about vB. I'm sure ill be back for something else these days. I have a few small short mods I am going to be working on. But this setup has explained a few things to me about vB.

Thanks :D