Page 1 of 1

Selecting field from query

Posted: Mon Aug 02, 2004 9:19 am
by dwfait
Hi, in my code, i have a query, using $_POST values as WHERE values. If there is a match, how do i get the value from another field in the match?

Here is the code:

Code: Select all

<?php
$ssppluser = $_POST&#1111;"user"];
$sspplpass = $_POST&#1111;"pass"];
    $link = mysql_connect("localhost", "root", "")
        or die("Could not connect");


mysql_select_db("stormst_sspp")
        or exit("Could not select database");

$result = mysql_query("SELECT user AND pass FROM sspp WHERE user='$ssppluser' AND pass='$ssppluser'")

    or die ("Invalid query");
$num_rows = mysql_num_rows($result);
If ("$num_rows"==1) &#123;
echo "Login Complete.";
&#125; else &#123;
echo "Bad username / password.";
&#125;
    mysql_close($link);
?>
There will only be one correct search result, as it is a login scrupt. So how would i get the field value 'id' from the row that was returned by the query, if there was a row returned?

Posted: Mon Aug 02, 2004 10:39 am
by nigma
Make your query something like:

Code: Select all

select id from sspp where user='user' and pass='pass'
Then later on:

Code: Select all

if (mysql_num_rows($query_result) == 1) {
  $row = mysql_fetch_array($query_result);
  $id = $row['id'];
  echo "Your id is $id";
}
else {
  // output other message
}
Let me know if you need clearification

Posted: Mon Aug 02, 2004 10:58 am
by dwfait
hmm, when i incorporate this into my code, the variables seem to be null. Here is the code:

Code: Select all

<?php 
if (is_null($action)) {
?> 
<form action="index.php?action=login" method="POST"> 
Your Username: <input type="text" name="user" /> 
Your Password: <input type="text" name="pass" /> 
<input type="submit"> 
</form> 

<?php 
} else if ($action=='login') { 
$ssppluser = $_POST["user"]; 
$sspplpass = $_POST["pass"]; 
    $link = mysql_connect("localhost", "root", "") 
        or die("Could not connect"); 


mysql_select_db("stormst_sspp") 
        or exit("Could not select database"); 

$result = mysql_query("SELECT user AND pass FROM sspp WHERE user='$ssppluser' AND pass='$ssppluser'") 

    or die ("Invalid query"); 
$num_rows = mysql_num_rows($result); 
If ($num_rows==1) {
$row = mysql_fetch_array($result); 
$id = $row['id'];
$staff = $row['staff'];
echo "Login Complete. User ID: $id , Staff: $staff";
?>
<br>
 <a href="index.php?action=loginc&user=$ssppluser">Click here to continue</a>
<?php
} else { 
echo "Bad username / password."; 
} 
    mysql_close($link); 
 
?> 

<?php
} else if ($action=='loginc') {
echo "Welcome $user";
}
?>

Posted: Mon Aug 02, 2004 11:08 am
by Weirdan

Code: Select all

//......skipped....
mysql_query("select id, staff from sspp WHERE user='$ssppluser' AND pass='$ssppluser'");
if(mysql_num_rows()>0) { // sucessful login
  list($id, $staff) = mysql_fetch_row();
  echo "ID: $id, Staff: $staff";
}

Posted: Mon Aug 02, 2004 11:35 am
by dwfait
Unfortunatly, that code did not work, but with a litle tinkering i got it to work by doing this:

Code: Select all

$result=mysql_query("select id, staff from sspp WHERE user='$ssppluser' AND pass='$ssppluser'"); 

if (mysql_num_rows($result)>0) &#123; 
  list($id, $staff) = mysql_fetch_row($result); 
  echo "ID: $id, Staff: $staff";
Thanks for your help!

Posted: Mon Aug 02, 2004 12:02 pm
by nigma
dwfait: the reason it didn't work is because you never altered your query so that it would return the fields you were trying to access (id and staff).