Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP data connection 3

Status
Not open for further replies.

LWolf

Programmer
Feb 3, 2008
77
US
Still the noob...
Why does this code bomb out?

$data = mysql_query("SELECT * FROM users WHERE username=" . $_REQUEST["username"])

or

$data = mysql_query("SELECT * FROM users WHERE username= $_REQUEST["username"])

I having been playing with this all afternoon and not getting anywhere.

Thanks.
K
 
the code is more or less fine (you are repeating the query as merlinx says though).

and the output is as expected of course.

the problem is here
Code:
$data = mysql_query($sql);

mysql_query returns a resource. not an array. to loop over the resource you then need to use a mysql_fetch* function.

Code:
$data = mysql_fetch_assoc($res);

change the relevant line and it will work.
add a
Code:
print_r($data);
to show the other fields returned
 
I used merlinx last 2 lines and it worked. What is the difference between mysql_fetch_assoc() and mysql_fetch_array() and when is it better to use one over the other?
 
*_array returns an array containing both alphanumeric keys (of the column names) and numeric keys, whereas *_assoc returns just associative keys.

it is better to _know_ what you want in advance and choose the right function.

note, you can change the behaviour of mysql_fetch_array by adding a constant as the second argument.

personally, nine times out of ten, I use mysql_fetch_object if I am not using an abstraction layer like PDO.

 
Thanks for the info.

I got another question (LOL)...

I have an image that I am trying to use for a button background for a template website where the css is done quite well. I can get the image to display when I just use it in an <img>. Here is the dir layout...

/var/
under modern-business there is
images
scripts
styles

There is a navi.css in styles where the css code for the buttons is handled. I have used "background-image: '/var/ and there is no display. There is no display when I just use the path in index.php. When I put "<img src='images/btn_bg.jpeg'>" the image displays. I am using linux.

I think the problem is how to use the path. I am also switching over from windows to linux. Any ideas on how to get the correct path for the image?
 
Hi

This question is off-topic, belongs to forum215. Also please avoid asking multiple unrelated questions in one thread. Future visitors will unlikely be interested in all of them.
LWolf said:
I have used "background-image: '/var/ and there is no display.
That looks like the path on your local filesystem. When referencing a file through HTTP, you have to use the the path visible from outside. That path's root directory is the DocumentRoot directory configured in your web server.

Regarding the CSS syntax, I suppose the lack of [tt]url()[/tt] is just a typo here in your post.

Anyway, I would use relative path :
Code:
[teal]*[/teal] [teal]{[/teal]
  [COLOR=coral]background-image:[/color] [COLOR=darkgoldenrod]url[/color]([COLOR=darkgoldenrod]..[/color]/[COLOR=darkgoldenrod]images[/color]/[COLOR=darkgoldenrod]btn_bg.jpeg[/color]);
[teal]}[/teal]
( You not specified for what kind of element you intend to set the background image, so I set it for everything. )


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top