Simple mysql query not working!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
guycrossley
Forum Newbie
Posts: 9
Joined: Mon May 12, 2003 8:46 pm

Simple mysql query not working!

Post by guycrossley »

Hi all! I orginally put this up in the General Discussion forum and realised this was probably a better spot for it.

trying to debug a record insertion form for my blog page.

What i want to happen is:
-User Enters their Nickname into a form
-Then I determine the User Id based on this nickname (since they are both in the 'users' table)
-Insert the blog with user id to the 'posts' table

Pretty simple eh? So what am i doing wrong?

<?php
$username = $HTTP_POST_VARS['nickname'];
$query_1 = "SELECT USER_ID FROM users WHERE NICK_NAME = $username";
$userid = mysql_query($query_1);
?>

Note that $userid is a number. If i manually set $userid = 4, my insertion code (not shown) works great. The problem is that $userid is not being set to a valid integer, infact it remains NULL... Whats happening!

Thx in advance!
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

MySQL on php.net

Code: Select all

$query = "SELECT * FROM `table`";
$result = mysql_query($query);

// $result is now a resource handler, not a value.

$value = mysql_fetch_array($result);

// $value is not the value
User avatar
coolpravin
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 12:56 am
Location: India

the solution

Post by coolpravin »

Hey use this one

<?php
$username = $HTTP_POST_VARS['nickname'];
$query_1 = "SELECT USER_ID FROM users WHERE NICK_NAME = $username";
$result_1 = mysql_query($query_1);
$row_1=mysql_fetch_array($result_1);
$userid=$row_1["USER_ID "];
?>
Is it works?
I know it will.
-------------BeCool
guycrossley
Forum Newbie
Posts: 9
Joined: Mon May 12, 2003 8:46 pm

Post by guycrossley »

thx guys,

great help. I found that a lot of my problems where actually in my code layout. That is, the order of program execution of the page was all stuffed up.

All fixed now!

Cheers!

--Guy
Post Reply