WatsonN Posted September 17, 2010 Share Posted September 17, 2010 I have a Database with two tables "users" and "Logins" and I'm trying to figure out how to check the cookie ID and use a mysql query to check if ID from the cookie matches UID in the "Logins" table and get all the data with a matching ID and UID. How exactly would I go about doing this? -edit- Fixed spelling -edit- Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/ Share on other sites More sharing options...
schilly Posted September 17, 2010 Share Posted September 17, 2010 You mean something like this? //check login table against cookie if(mysql_result(mysql_query(sprintf("SELECT count(id) FROM Logins WHERE UID = %d", $_COOKIE['ID'])),0) > 0){ //login record exists //load user data here } Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112282 Share on other sites More sharing options...
WatsonN Posted September 17, 2010 Author Share Posted September 17, 2010 Thanks schilly, That is exactly what i'm meaning. But now how do I get print/echo all the data in the table? Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112300 Share on other sites More sharing options...
schilly Posted September 17, 2010 Share Posted September 17, 2010 //check login table against cookie if(mysql_result(mysql_query(sprintf("SELECT count(id) FROM Logins WHERE UID = %d", $_COOKIE['ID'])),0) > 0){ //login record exists. get record $sql = sprintf("SELECT * FROM users WHERE id = %d", $_COOKIE['id']); $results = mysql_query($sql); extract(mysql_fetch_array($results)); //output table with user data in it echo "<table>"; echo "<tr>"; echo "<td></td>"; //etc etc as much as you need echo "</tr>"; echo "</table>"; } Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112306 Share on other sites More sharing options...
WatsonN Posted September 18, 2010 Author Share Posted September 18, 2010 I put it in the file and if(mysql_result(mysql_query(sprintf("SELECT count(id) FROM Logins WHERE UID = %d", $_COOKIE['UID'])),0) > 0) Isn't working i echoed something inside the brackets and it isn't showing up and I'm not sure what to change Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112358 Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 I put it in the file and if(mysql_result(mysql_query(sprintf("SELECT count(id) FROM Logins WHERE UID = %d", $_COOKIE['UID'])),0) > 0) Isn't working i echoed something inside the brackets and it isn't showing up and I'm not sure what to change It doesn't work like that, to do that I think you would change $_COOKIE['UID'] to $row[0] or something of the such. Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112377 Share on other sites More sharing options...
WatsonN Posted September 18, 2010 Author Share Posted September 18, 2010 So Username how would that write out PHP + MySQL is not my thing Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112457 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Author Share Posted September 19, 2010 I've googled it and cant find anything to help Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112709 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Author Share Posted September 19, 2010 I've got this so far but I'm getting an error Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/n/a/t/nathanwatson/html/admin/LOGINS.php on line 11 The code: // Connects to your Database mysql_connect("**", "**", "**") or die(mysql_error()); mysql_select_db("loginwatsonn") or die(mysql_error()); echo "Start"; echo "<br>"; //check login table against cookie if(mysql_result(mysql_query(sprintf("SELECT count(id) FROM Logins WHERE UID = %d", $_COOKIE['UID_WatsonN'])),0) > 0){ $num=mysql_numrows($result); mysql_close(); ?> <table border="0" cellspacing="2" cellpadding="2"> <tr> <th>ID</th> <th>UID</th> <th>Site</th> <th>Uname</th> <th>Pass</th> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"ID"); $f2=mysql_result($result,$i,"UID"); $f3=mysql_result($result,$i,"Site"); $f4=mysql_result($result,$i,"Uname"); $f5=mysql_result($result,$i,"Pass"); ?> <tr> <td><?php echo $f1; ?></td> <td><?php echo $f2; ?></td> <td><?php echo $f3; ?></td> <td><?php echo $f4; ?></td> <td><?php echo $f5; ?></td> </tr> <?php $i++; } } ?> It isn't properly indented but I will fix it as soon as I can. Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112844 Share on other sites More sharing options...
sasa Posted September 19, 2010 Share Posted September 19, 2010 try <?php // Connects to your Database mysql_connect("**", "**", "**") or die(mysql_error()); mysql_select_db("loginwatsonn") or die(mysql_error()); echo "Start"; echo "<br>"; $result = mysql_query(sprintf("SELECT * FROM Logins WHERE UID = %d", $_COOKIE['UID_WatsonN'])); //check login table against cookie if(($num = mysql_num_rows($result)) > 0){ mysql_close(); ?> <table border="0" cellspacing="2" cellpadding="2"> <tr> <th>ID</th> <th>UID</th> <th>Site</th> <th>Uname</th> <th>Pass</th> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"ID"); $f2=mysql_result($result,$i,"UID"); $f3=mysql_result($result,$i,"Site"); $f4=mysql_result($result,$i,"Uname"); $f5=mysql_result($result,$i,"Pass"); ?> <tr> <td><?php echo $f1; ?></td> <td><?php echo $f2; ?></td> <td><?php echo $f3; ?></td> <td><?php echo $f4; ?></td> <td><?php echo $f5; ?></td> </tr> <?php $i++; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112849 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Author Share Posted September 19, 2010 Thanks sasa! That works perfectly Now is there a way to take each row and pull the data from columns in that row and put them in a variable? Or am I just being out there? Quote Link to comment https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112861 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.