mysql_result quick problem...help appreciated

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

mysql_result quick problem...help appreciated

Post by scarface222 »

Hey guys quick question. I normally use mysql_fetch_assoc but I was unable to this time so I used mysql_result. Anyhow the value I want is echoed but when the insert query is used $ip turns up blank. Anyone know why?


$ipcheck="SELECT ip FROM users WHERE username='$user'";
$ipresult = mysql_query($ipcheck);
echo $ip=mysql_result($ipresult,0);
$q = "INSERT INTO banned_users VALUES ('test', 0,'$ip')";
$qresult = mysql_query($q);
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: mysql_result quick problem...help appreciated

Post by Darhazer »

What is the schema of the banned_users table? What is the datatype of ip field in the users table?
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: mysql_result quick problem...help appreciated

Post by scarface222 »

when you say schema I take it you mean structure. The headings are as follows username, timestamp, ip in the banned-users table. ip is varchar datatype in both tables.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: mysql_result quick problem...help appreciated

Post by Darhazer »

I meant exactly 'structure'.
Is this the exact structure? Doesn't the table have a primary key?
Your code works find with this exact structure. Try specifying the column names in the query:

Code: Select all

$q = "INSERT INTO banned_users(username, timestamp, ip) VALUES ('test', 0,'$ip')";
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: mysql_result quick problem...help appreciated

Post by scarface222 »

Yeah that worked man. Thanks appreciate it.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: mysql_result quick problem...help appreciated

Post by jackpf »

Use mysql_error().
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: mysql_result quick problem...solved

Post by scarface222 »

Yeah that is a good practice to get into. I will from now on. Thanks man.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: mysql_result quick problem...help appreciated

Post by jackpf »

Oh, sorry, I didnt see your last post saying Darhazer solved it.

But yeah...it's always useful if a query is failing and you don't know what's going on.
Post Reply