Jump to content

A few quick questions...


pixy

Recommended Posts

Okay, so I'm using sessions to remember a user's username.

At the top of every page I have
session_start();
$user = $_SESSION['user'];

Since using $_SESSION['user'] messes mysql queries.

My first question is:
Why do I get errors if I do it this way:
$_SESSION['user'] = $user;

Or when I have something like this...
while ($row = mysql_fetch_array($result, MYSQL_NUM) {
  $row[0] = $name;
}
It gives me an error, but...
$name = $row[0];
is okay!

It's not a big deal, but I'm wondering. I read in a book that people can do variables backwards and it works, so I dont know.

----------------------------------------------------------------------------

My other question:

I tried to query the database with a fields called "to" and "from" and "read" and it gave me errors when I used those field names. I changed them to user_to, user_from, and is_read and it's fine. Why did it mess up when I used those names? They were inside parenthasis, so I wouldn't think it would assume that those are some properties of the table...right?
Link to comment
https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/
Share on other sites

to answer your first question:
i don't think what you saw was errors. i believe they were more like warnings. you should post your "errors"
warnings are expected your case where you don't seem to have initialized your variables which is considered good practice.

i don't see how this makes sense:
[code=php:0]
while ($row = mysql_fetch_array($result, MYSQL_NUM) {
  $row[0] = $name;
}
[/code]

wouldn't you want to do it the other way around? assigning values works from left to right.




as for the second question, reserved words can't be used in MySQL, as far as i know. so, regardless if you used it with a parenthesis to enclose them or not, it would still cough up since those are reserved words.
Link to comment
https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57390
Share on other sites

To use "to" and "from" and "read" within an SQL query you should encase them within backticks - `

like so: [b]`some_name`[/b]

Ideally it is good practise to use backticks arround your field/table names within a query, however many people don't do this.

Also this:
[code=php:0]while ($row = mysql_fetch_array($result, MYSQL_NUM) {
  $row[0] = $name;
}[/code]
Will not work as this will assign $row[0] to the value of of $name, which $name probably doesnt exist and so you get a notice error. You should do it the otherway round sit its $name = $row[0];
Link to comment
https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57393
Share on other sites

for the record, even though using backticks works, i would suggest staying away from reserved keywords regardless just for good practice.

also, the reason $_SESSION['user'] messes up your queries is likely that you're not including it properly.  when you need to echo an array value within a string, use braces to enclose it:

[code]<?php
$_SESSION['name'] = 'Someone You Wish You Knew';
$hello = "Hi, I'm {$_SESSION['name']}.";
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57454
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.