Webmaster Forum


Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

   

Reply
 
LinkBack Thread Tools Display Modes
Old 03-17-2005, 06:32 PM   #1 (permalink)
Contributing Member
 
bytech's Avatar
 
Join Date: 10-13-03
Location: Manitoba, Canada
Posts: 573
iTrader: 0 / 0%
Latest Blog:
None

bytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nice
Send a message via Skype™ to bytech
Trouble with using variable

Hi everyone,

Why won't this update the DB?

Code:
$sqlcode = "SELECT pid FROM vote_stats ORDER BY pid DESC LIMIT 1"; $sqlresult = mysql_query($sqlcode, $s_dbid); $updatecode = "UPDATE vote_stats SET lastvote=$now WHERE pid=$sqlresult"; $updatedb = mysql_query($updatecode, $s_dbid);
The mySQL queries work when I enter them in phpmyadmin. The php code works when I replace pid=$sqlresult with pid='3' (in my case the highest PID for now). Using the two together, no update is made to the DB. What am I missing?

Thanks in advance.
__________________
Canada based Web Design Forums. Join us in the fun today!
bytech is offline  
Add Post to del.icio.us
Reply With Quote
Old 03-17-2005, 08:12 PM   #2 (permalink)
Inactive
 
TeKBuzz's Avatar
 
Join Date: 11-12-03
Location: Ohio
Posts: 558
iTrader: 0 / 0%
Latest Blog:
None

TeKBuzz is liked by somebodyTeKBuzz is liked by somebodyTeKBuzz is liked by somebodyTeKBuzz is liked by somebody
Send a message via ICQ to TeKBuzz Send a message via AIM to TeKBuzz Send a message via MSN to TeKBuzz Send a message via Yahoo to TeKBuzz
pid=$sqlresult means to set where pid = result reference (or something like that) you need to get the info out of $sqlresult and into an int, this may not be the best way to type it but it would work, i'm sure you'll get the idea and speed it up:
Code:
$sqlresult = mysql_query($query); while ( $pidnum = mysql_fetch_array($sqlresult) ) { $updatecode = "UPDATE vote_stats SET lastvote=$now WHERE pid=$pidnum[pid]"; $updatedb = mysql_query($updatecode, $s_dbid); }
Altho this would seem like a useful way only when there is more than one update, you have to understand that by giving it the mysql result you aren't giving it a value it can compare to the pid field.
TeKBuzz is offline  
Add Post to del.icio.us
Reply With Quote
Old 03-17-2005, 11:31 PM   #3 (permalink)
Contributing Member
 
bytech's Avatar
 
Join Date: 10-13-03
Location: Manitoba, Canada
Posts: 573
iTrader: 0 / 0%
Latest Blog:
None

bytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nicebytech is just really nice
Send a message via Skype™ to bytech
Thank you! That worked, although I am not exactlly sure what/how this does. I understand the concept of what I was doing wrong, but not sure how you solved it. Any words of further wisdom?
__________________
Canada based Web Design Forums. Join us in the fun today!
bytech is offline  
Add Post to del.icio.us
Reply With Quote
Old 03-17-2005, 11:57 PM   #4 (permalink)
Inactive
 
TeKBuzz's Avatar
 
Join Date: 11-12-03
Location: Ohio
Posts: 558
iTrader: 0 / 0%
Latest Blog:
None

TeKBuzz is liked by somebodyTeKBuzz is liked by somebodyTeKBuzz is liked by somebodyTeKBuzz is liked by somebody
Send a message via ICQ to TeKBuzz Send a message via AIM to TeKBuzz Send a message via MSN to TeKBuzz Send a message via Yahoo to TeKBuzz
Code:
$sqlresult = mysql_query($query); while ( $pidnum = mysql_fetch_array($sqlresult) ) { //Take the results of the query and insert into an array $updatecode = "UPDATE vote_stats SET lastvote=$now WHERE pid=$pidnum[pid]"; //Set query to update where pid is equal to the pid in the current offset of the array $updatedb = mysql_query($updatecode, $s_dbid); }
I added comments but basically what happens if when you run a mysql_query, it returns a resource ID aka tells the protocol where to look for the information that was returned. By calling mysql_fetch_array() on this result, you then take that information and put it into an array where the values are defined by the field name given in the mysql table. So now you have an array which contains the id your looking for, and you then take that array and go to the offset with the id and make it the check on your condition.

So what's going on is you get information from mysql, you put that information into something accessable, you find what you need and use it. If this doesn't make sense please try to elaborate on what doesn't make sense, or if it does feel free to ask something else. BTW if your query looking for the ID only returns ONE result (aka one ID, not the ID of more than one thing) you can always get rid of the while statement and do the same thing just get rid of while(){}, leave everything else in (IE leave the $pidnum = mysql_fetch_array($sqlresult); in)
TeKBuzz is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
is it possible to pass a php variable to asp code white10 Coding Forum 4 04-20-2007 06:11 AM
Searching for a specific phrase in a variable? Izzmo Coding Forum 9 11-27-2006 12:50 PM
Need a Simple Variable Script sixty6 Coding Forum 4 03-24-2006 09:03 AM
How to grab a variable from the URL Pipeline-Webdesign Coding Forum 3 08-31-2004 11:25 AM
1 variable, mod rewrite help MrMean Coding Forum 6 07-07-2004 04:02 PM


Sponsor Links
Get exposure! Get exposure! Find Scripts Web Hosting Directory Get exposure! SEO Blog


All times are GMT -7. The time now is 02:59 AM.
© Copyright 2008 V7 Inc