My name is a... number?

Post here if you need help with SQL.
Post Reply
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

My name is a... number?

Post by JelvinJS7 »

I'm trying to add a very simple feature: if a user is logged in, say hello (i don't know why I'm wasting my time on this right now. Not important really—but now it's frustrating!). I'm using the MySQL class from the BetterPHP library.
for some reason, it outputs "Hello 1". I understand why 1--it's my user id. I don't understand why it's not outputting "Jonathan" or "James" (I don't know what name I used). Here's my code:
[syntax=php]
<?php
If (isset($_SESSION['uid'])){
     include("includes/mysql.class.inc.php");
     $mysql = new mysql("mysql**.000webhost.com", "******", "*****^", "*******");
    $q = $mysql->query("SELECT `name` FROM `users` WHERE `id` = {$_SESSION['uid']}");
    $name = $mysql->fetch_cell($q);
?>
<p style="float:center">Hello <?php echo $name; ?></p>
[/syntax]
My table `users` has 5 columns: name, username, password, email, and user_id (not in order) Witt the usual settings (unique email/username, A_I/primary id, etc.) can someone explain what's wrong?
And yes, it is connecting correctly.
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: My name is a... number?

Post by bowersbros »

well if you have user_id as your field, then change id to user_id

Screenshot of the table will help :)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: My name is a... number?

Post by JelvinJS7 »

bowersbros wrote:well if you have user_id as your field, then change id to user_id

Screenshot of the table will help :)

No, it's id in the table, but uid in the session. `user_id` results in an error.
The table is exactly as I said: id, name, username, password, email. I'll post a pic later; can't right now.
The thing worked for me before..... In a different part of Ye website...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: My name is a... number?

Post by jacek »

The 1 is probably true. I think you are meant to do

[syntax=php]$mysql = new mysql("mysql**.000webhost.com", "******", "*****^", "*******");
$mysql->query("SELECT `name` FROM `users` WHERE `id` = {$_SESSION['uid']}");
$mysql->fetch_cell($name);[/syntax]
been a while since I looked at the code though ;)
Image
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: My name is a... number?

Post by JelvinJS7 »

Doesn't work.
Here's the code:
[syntax=php]
public function fetch_cell(&$result){
$result = mysql_result($this->result, 0);

return ($result !== false);
}
[/syntax]

I also tried this minor edit:
[syntax=php]
public function fetch_cell(&$result, $cell = null){
$result = mysql_result($this->result, 0, $cell);

return ($result !== false);
}
[/syntax]
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: My name is a... number?

Post by JelvinJS7 »

here's a screen shot of the table structure
`users` table
`users` table
Screen Shot 2011-12-19 at 8.12.34 PM.png (92.49 KiB) Viewed 2643 times
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: My name is a... number?

Post by jacek »

It should be used as I said above :?

Try this and see what the exact value you are getting is

[syntax=php] $mysql = new mysql("mysql**.000webhost.com", "******", "*****^", "*******");
$mysql->query("SELECT `name` FROM `users` WHERE `id` = {$_SESSION['uid']}");
$mysql->fetch_cell($name);

var_dump($name);
[/syntax]
Image
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: My name is a... number?

Post by JelvinJS7 »

jacek wrote:It should be used as I said above :?

Nope :(
jacek wrote:
Try this and see what the exact value you are getting is

[syntax=php] $mysql = new mysql("mysql**.000webhost.com", "******", "*****^", "*******");
$mysql->query("SELECT `name` FROM `users` WHERE `id` = {$_SESSION['uid']}");
$mysql->fetch_cell($name);

var_dump($name);
[/syntax]

[syntax=text]
string(8) "Jonathan"
[/syntax]
I don't really understand the var_dump function, but that just adds to confusion; the result.
However, $name was never defined, so how'd that work?
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: My name is a... number?

Post by JelvinJS7 »

Alright! It's working now, but I'm extremely confused!
Code:
[syntax=php]
 <?php
If (isset($_SESSION['uid'])){
     include_once("includes/mysql.class.inc.php");
     $mysql = new mysql("mysql**.000webhost.com", "a********_^^", "****%}^", "a**^+^%^_#%#^");
    $mysql->query("SELECT `name` FROM `users` WHERE `id` = {$_SESSION['uid']}");
    $mysql->fetch_cell($name); //not assigned to a value
?>
<p style="float:center">Hello, <?php echo $name; ?>!</p>
<?php
}
?>
[/syntax]
Why does this work now? I'm kinda confused. I know it's at the part I commented, but still.... :?:
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: My name is a... number?

Post by jacek »

The variable is passed by reference to the fetch_cell method, basically this means that anything you do to the variable inside of the function is also done outside of it. so that's not it's defined.
Image
Post Reply