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.
My name is a... number?
-
- Posts: 534
- Joined: Thu May 05, 2011 8:19 pm
Re: My name is a... number?
well if you have user_id as your field, then change id to user_id
Screenshot of the table will help
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
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
Re: My name is a... number?
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?
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
[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
Re: My name is a... number?
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]
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]
Re: My name is a... number?
here's a screen shot of the table structure
Re: My name is a... number?
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]
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]
Re: My name is a... number?
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?
Re: My name is a... number?
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....
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....
Re: My name is a... number?
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.