I have a checklogin page and followin codes;
$sql = "select * from users where username = '$username' and password = '$password'";
$result = mysql_query($sql) or die ( mysql_error() );
if(mysql_num_rows($result)>0)
{
$line = mysql_fetch_assoc($result);
$count++;
}
if ($count == 1)
{
$_SESSION['login'] = "true";
$_SESSION['username'] = $username;
$_SESSION['usertype'] = $line['type'];
}
And the $_SESSION['usertype'] is always 1 even though it is different in database, what am I doing wrong
I'd suggest you do a little debugging through the entire process.
Verify that $username and $password are what you expect them to be before you run the query:
echo "Username: {$username} Password: {$password}<br/>";
Verify that your query is returning the right values:
$line = mysql_fetch_assoc($result);
echo "<pre>";
var_dump($line);
echo "</pre>";
If both of those tests return the expected values, then you may have to investigate your session - is it being started properly? Is it being reset somewhere?
$line is returning 1 because it is a boolean yes/no result. A result was found hence it returns 1.
You have to iterate through $line or access result set #1 directly with $line[0]['type'];
Or just fetch a single row:
Taken from the manual:
**Example #1 Fetching one row with mysql_fetch_row()**
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
In you case, if you changed your select to "select type from users .... " instead of "select *"
$line[0]
Related
Hi i have two arrays built by using mysql_fetch_array() i want to use them both in one while loop :
$username =$_SESSION["username"];
$password =$_SESSION["password"];
$query1= "SELECT * FROM subs WHERE username='$username' AND password='$password' ";
$res1 = mysql_query($query1);
$row_cnt1 = mysql_num_rows($res1);
if($res1 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$row1 = mysql_fetch_array($res1);
if($row_cnt1 == 1){
$sid = $row1["id"];
}else{
die("there is more than one user with the same username and password");
}
$query2= "SELECT * FROM bookreservations ";
$res2 = mysql_query($query2);
$row_cnt2 = mysql_num_rows($res2);
if($res2 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$row2 = mysql_fetch_array($res2) ;
$query= "SELECT * FROM books";
$res = mysql_query($query);
$row_cnt = mysql_num_rows($res);
if($res === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while ($row = mysql_fetch_array($res)){
if($row2["sid"] == $sid && $row2["bid"] == $row["id"]){
continue;
}
$temp = $row["id"];
$temp1 = $row["title"];
echo "Title : ".$temp1."<br><br>";
$temp1 = $row["authors"];
echo "Authors : ".$temp1."<br><br>";
$temp1 = $row["copies"];
echo "Copies : ".$temp1."<br><br><br><br>";
echo "<div class=\"re\"><input type=\"radio\" value=\"reserve\" name=\"".$temp ."\">reserve</div> <br><br><br><br><br><br>" ;
}
I want to extract things from row array but i don't want them to have some values from row2 array so I have to use them both in one while loop ,THE SUMMARY row array is ok it jumps to the next row in each loop but row2 array is stuck it doesn't move to the next row what to do ?
Although I don't really understand your task well, I however, would recommend the following edits
My assumption: You are trying to fetch all rows from $res for each $res2.
Although inefficient technique, but I'll edit your code as below.
while($row2 = mysql_fetch_array($res2) ){
while ( $row = mysql_fetch_array($res) ){ //Both $row & $row2 will have equal incremented rows.
}
}//Outer loop
I have only just started writing the PHP script to power the backend of my android app. What i'm currently trying to do is run a PHP script that goes into my database called send_friendreq and the table called pending_req and gets the row toUser and adds it to an array. The only problem i'm currently having is the fact that I cannot get the PHP script to run correctly. Any help would be appreciated. Here is my code for the PHP script that I currently have. Thank you very much for the help!
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql="SELECT fromUser FROM pending_req WHERE toUser='&username'";
$result = mysqli_query($con, $sql);
$array = array();
while ($row = pg_fetch_array($result))
{
$i++;
}
for($x=0;$x<$i;$x++)
{
echo $array[$x];
}
if(!$result)
{
echo 'Failed';
}
else
{
echo json_encode($array[$x]);
echo "<br>";
}
If you have suggestions on something that would work better / more efficiently / safer, please let me know!
If I am not wrong, you are trying to get json of results from your query. try this code.
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql = 'SELECT fromUser FROM pending_req WHERE toUser='. $username ;
$result = mysqli_query($con, $sql);
if(!$result) {
echo 'Failed';
}elseif($result){
$myArray = array();
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}
you query syntax seems to be not correct plz modify as :
$sql="SELECT fromUser FROM pending_req WHERE toUser='$username'";
Got a issue where if the rows are equal to 0 the else statement is not called. if i type in the correct details on the site, the first condition is met and the xml is displayed, however if the incorrect details are entered, the error xml is not displayed.
echo "<users>";
$result = mysql_query("SELECT * FROM `numbers` WHERE `email` = '".$email."' AND `password` = '".$password."'") or die('Invalid query: ' . mysql_error());
if (!$result) die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if(mysql_num_rows($result) != 0)
{
echo "<usercallback>";
echo "<id>".$row['id']."</id>";
//echo "<number>".$row2['number']."</number>";
//echo "<gender>".$row2['gender']."</gender>";
//echo "<relationship>".$row2['relationship']."</relationship>";
echo "</usercallback>";
}
else
{
echo "<usercallback>";
echo "<id>error</id>";
echo "</usercallback>";
}
}
echo "</users>";
actually you have to put the if mysql_num_rows check outside the while block
echo "<users>";
$result = mysql_query("SELECT * FROM `numbers` WHERE `email` = '".$email."' AND `password` = '".$password."'") or die('Invalid query: ' . mysql_error());
if(mysql_num_rows($result) != 0)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<usercallback>";
echo "<id>".$row['id']."</id>";
//echo "<number>".$row2['number']."</number>";
//echo "<gender>".$row2['gender']."</gender>";
//echo "<relationship>".$row2['relationship']."</relationship>";
echo "</usercallback>";
}
}
else
{
echo "<usercallback>";
echo "<id>error</id>";
echo "</usercallback>";
}
echo "</users>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
If there are no rows then $row will be false and it will never enter the above while loop.
So you can not get error message
You check for rows while performing the loop, you should check for the number of rows BEFORE the while loop.
It looks like you're expecting a zero-row response from MySQL to result in an undef $result. That's not what happens. mysql_query() returns null if there was an error. You can test the "truth" of its return value to see if the query ran correctly. If the query results in 0 rows of response, it ran correctly. The value to test, then is mysql_num_rows($result).
However, it can be done simpler. I usually write this like:
$res = mysql_query("SELECT 1");
if (!$res) {
//do whatever error reporting if the SQL was bad
//though you should probably deal with any condition that ends up
//here before you go into production!
}
if (mysql_num_rows($res) == 0) {
//put out my "no results found" message
}
while ($row = mysql_fetch_assoc($res)) {
//do my per-row business
}
mysql_fetch_assoc() returns false when there are no (or no more) results to return. So if it's empty, that while loop will never run, so it doesn't need to be inside an else.
I've got a php script with this statement:
$query = "SELECT type
FROM users
WHERE username = '$username'";
$result = $database->query($query);
if($result == 1) {
echo "whatever";
continue;
}
The problem is that the if never runs and when I created a print statement to print $result before the if runs, it prints a Reference ID#. So result is never == 1 because it is being assigned the reference ID #.
What the heck am I doing wrong? How do I assign the value of 'type' which is an INT, instead of it's contents Reference ID#?
you have to fetch that line first ...
$query = "SELECT type FROM users WHERE username = '$username'";
$result = $database->query($query);
$row = mysql_fetch_assoc($result);
if($row['type'] == 1)
{
echo "whatever";
}
You need to use mysql_fetch_assoc() or mysql_fetch_array() to get the result into an array:
$query = $database->query("SELECT type FROM users WHERE username = '$username'");
$result = mysql_fetch_assoc($query);
if($result['type'] == 1)
{
echo "whatever";
continue;
}
try
<?php
$result = mysql_fetch_assoc($database->query($query));
$id = $result['type'];
if($type == 1)
{
Yes. You have to check it like:
$query = "SELECT type FROM users WHERE username = '$username'";
$result = $database->query($query);
if($result)
{
echo "whatever";
continue;
}
This is a Boolean check, but... In php we have weak types - everything is true, except on false, 0, '' (empty string), null, etc.
If you want to make a good Boolean check including type, then you have:
if(bool_val === true)
{
..
}
// or
if(bool_val && is_bool(bool_val))
{
..
}
This query object returns a result resource in php. And you probably receive #Resource ID, which is some kind of pointer in php code.
If you receive something like this (#Resource ID) - this means your query has passed correctly and no error occured.
I am trying to see if the logged in user is verified. But there is an error:
Warning: mysql_fetch_array(): supplied
argument is not a valid MySQL result
resource in
/home/psmcouk/public_html/colemansystems/verify.php
on line 332
Here is the PHP code:
$user1 = $_SESSION['usr'];
$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1");
while($row = mysql_fetch_array($result)) //LINE 332
{
$valid = $row['valid'];
}
if($valid == "1"){
echo "$user1, you're account is currently verified.";
}
I just can not see what is wrong with this code.
Thanks!
All the answers above are lame.
$user1 = mysql_real_escape_string($_SESSION['usr']);
$query = "SELECT valid FROM phpbb_members WHERE memberName='$user1' and valid=1";
$result = mysql_query($query) or trigger_error(mysql_error()." in ".$query);
$valid = mysql_num_rows($result);
if($valid){
echo "$user1, your account is currently verified.";
}
You probably have an SQL error. Try
if (!$result) {
echo 'Invalid query: ' . mysql_error() . "\n";
}
I guess $user should be quoted:
$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName='$user1'");
You can always see whats wrong my placing echo mysql_error(); after the query
As already posted, you just have to put the user name in single quotations marks:
$query = "SELECT * FROM phpbb_members WHERE memberName = '".$user1."'";
Assuming, that the user name column is varchar. The code you used is only valid if you compare numbers, e.g. integers.
A general remark: Depending on the size of the columns of your database, it might be resonable to select specific rows rather than all using *. For instance:
$query = "SELCT memberName, valid FROM phpbb_members";
Try to use:
$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName='$user1'")
or die(mysql_error()); // to get if any error exists
$user1 = $_SESSION['usr'];
$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1");
while($row = mysql_fetch_field($result)) //LINE 332
{
$valid = $row['valid'];
}
if($valid == "1"){
echo "$user1, you're account is currently verified.";
}
try this.
You should test the result of mysql_query before using it, if you follow the examples from php.net :
$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1");
if (!$result) {
die('Request problem : ' . msql_error());
}
while($row = mysql_fetch_array($result)) //LINE 332
...