mdmartiny Posted January 28, 2012 Share Posted January 28, 2012 I am in the process of creating a like system for a personal website that I am working on. I am trying to pull information from one database and use that to place info into another database. When I do I get resource error #15. This is my code that I have so far function like_add() { $last = $_GET['l']; $first = $_GET['f']; $ip = $_SERVER['REMOTE_ADDR']; $like_sql = "Select * from `counter` where first = ' $first ' and last = '$last '"; $like_result = mysql_query($like_sql); $likes = '<p class="like_button"> Likes '; while ($like_row = mysql_fetch_assoc($like_result)) { $like = $like_row['likes']; $likes .= " $like"; } $likes .= '</p>'; echo "$likes"; echo '<form name="like_add_form" action="" method="POST" class="like_add"> <input type="submit" name="like_add" class="like_add_button" value="Like Me" /> </form>'; if (isset($_POST['like_add'])) { $page_id = "SELECT `page_id` FROM `counter` WHERE first = '$first' AND last = '$last'"; $page_id_result = mysql_query($page_id); echo $page_id_result; <----- Here I am trying to pull information from a field in my database to use as a variable and to place in the second database $voted = mysql_query("Select * FROM `liked_ip` where ip ='$ip' AND page_id='$page_id_result'"); <----- Here I want to use the information from the first table to place in the second one if (mysql_num_rows($voted) != 0) { echo "You have all ready liked this post"; } else { mysql_query("INSERT into `liked_ip`(id,page_id,ip) VALUES ('$page_id_result','$ip')"); } if (mysql_num_rows($voted) == 0) { mysql_query("UPDATE counter SET `likes` = `likes` + 1 where first = '$first' AND last = '$last'"); } } } I know that I have to be doing something so simple that I am just not seeing it this late at night. I may even be completely wrong in all of this. If someone could please help me out I would appreciate it very much. Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/ Share on other sites More sharing options...
trq Posted January 28, 2012 Share Posted January 28, 2012 mysql_query returns a result resource not an integer. Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/#findComment-1311895 Share on other sites More sharing options...
mdmartiny Posted January 28, 2012 Author Share Posted January 28, 2012 So I have to use a while loop to get the resource into an integar. Or is there some other way? Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/#findComment-1311924 Share on other sites More sharing options...
trq Posted January 28, 2012 Share Posted January 28, 2012 No, you don't need any loop. Just pass the resource to mysql_fetch_assoc which will give you an array. This array will have your data in it. Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/#findComment-1311925 Share on other sites More sharing options...
mdmartiny Posted January 28, 2012 Author Share Posted January 28, 2012 Let's see if I hot this right. From what I read from foing some searching. I run the query and it its everything in a array. Using the mysql_fetch_assoc makes it an associative one. Will that work if I only want info from one row on table Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/#findComment-1311930 Share on other sites More sharing options...
lonewolf217 Posted January 28, 2012 Share Posted January 28, 2012 you always have to push the results of the query into an array of some sort before you can read the data, whether is 1 row or 1 million rows Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/#findComment-1311944 Share on other sites More sharing options...
mdmartiny Posted January 28, 2012 Author Share Posted January 28, 2012 So then I need to assign am variable to the array. Something like $page_id = mysql_fetch_assoc($result); Right? Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/#findComment-1311950 Share on other sites More sharing options...
lonewolf217 Posted January 28, 2012 Share Posted January 28, 2012 <?php $row = mysql_fetch_array($result); $page_id = $row['page_id']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/#findComment-1311952 Share on other sites More sharing options...
mdmartiny Posted January 28, 2012 Author Share Posted January 28, 2012 The makes sense. I will have to try it when I get in front of a computer. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/255926-creating-a-like-system/#findComment-1311992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.