Page 1 of 1
My name is a... number?
Posted: Mon Dec 19, 2011 4:38 am
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:
<?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>
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.
Re: My name is a... number?
Posted: Mon Dec 19, 2011 7:29 am
by bowersbros
well if you have user_id as your field, then change id to user_id
Screenshot of the table will help
Re: My name is a... number?
Posted: Mon Dec 19, 2011 11:52 am
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...
Re: My name is a... number?
Posted: Mon Dec 19, 2011 1:53 pm
by jacek
The 1 is probably true. I think you are meant to do
$mysql = new mysql("mysql**.000webhost.com", "******", "*****^", "*******");
$mysql->query("SELECT `name` FROM `users` WHERE `id` = {$_SESSION['uid']}");
$mysql->fetch_cell($name);
been a while since I looked at the code though
Re: My name is a... number?
Posted: Mon Dec 19, 2011 8:16 pm
by JelvinJS7
Doesn't work.
Here's the code:
public function fetch_cell(&$result){
$result = mysql_result($this->result, 0);
return ($result !== false);
}
I also tried this minor edit:
public function fetch_cell(&$result, $cell = null){
$result = mysql_result($this->result, 0, $cell);
return ($result !== false);
}
Re: My name is a... number?
Posted: Tue Dec 20, 2011 1:16 am
by JelvinJS7
here's a screen shot of the table structure
- `users` table
- Screen Shot 2011-12-19 at 8.12.34 PM.png (92.49 KiB) Viewed 4317 times
Re: My name is a... number?
Posted: Wed Dec 21, 2011 12:39 am
by jacek
It should be used as I said above
Try this and see what the exact value you are getting is
$mysql = new mysql("mysql**.000webhost.com", "******", "*****^", "*******");
$mysql->query("SELECT `name` FROM `users` WHERE `id` = {$_SESSION['uid']}");
$mysql->fetch_cell($name);
var_dump($name);
Re: My name is a... number?
Posted: Wed Dec 21, 2011 3:34 am
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
$mysql = new mysql("mysql**.000webhost.com", "******", "*****^", "*******");
$mysql->query("SELECT `name` FROM `users` WHERE `id` = {$_SESSION['uid']}");
$mysql->fetch_cell($name);
var_dump($name);
string(8) "Jonathan"
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?
Re: My name is a... number?
Posted: Wed Dec 21, 2011 3:43 am
by JelvinJS7
Alright! It's working now, but I'm extremely confused!
Code:
<?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
}
?>
Why does this work now? I'm kinda confused. I know it's at the part I commented, but still....
Re: My name is a... number?
Posted: Wed Dec 21, 2011 4:05 pm
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.