Jump to content

MySQL if uid=#


WatsonN

Recommended Posts

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-

Link to comment
https://forums.phpfreaks.com/topic/213694-mysql-if-uid/
Share on other sites

//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>";
}

Link to comment
https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112306
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112377
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112844
Share on other sites

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++;
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/213694-mysql-if-uid/#findComment-1112849
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.