Jump to content

[SOLVED] Redirect and query problem


surochek

Recommended Posts

It only works if there's already a row in the table.

 

Here's the little code that has me stuck:

<?php 
session_start();
ob_start(); 
//connect with database
include("connect.php");

$user=$_SESSION['username'];
  
    $sql = "SELECT * FROM traits WHERE username = '$user'";
    $query = mysql_query($sql) or die (mysql_error(). " with query : ". $query."<br>");
	$num=mysql_num_rows($query)or die (mysql_error(). " with query : ". $num."<br>");
      
    if ($num<1){
header("Location: http://www.mashaholl.com/plotting/charactertraits_a.php");
    }
else{
header("Location: http://www.mashaholl.com/plotting/charactertraits_b.php");   
    }
    exit;
ob_end_flush(); 
?> 

 

If there are no rows that correspond to $user=$_SESSION['username'], I get this browser output:

 

with query : 0

 

Which makes sense. But not.

 

So why won't it redirect to the proper page?

Link to comment
https://forums.phpfreaks.com/topic/59468-solved-redirect-and-query-problem/
Share on other sites

I would not do the or die on mysql_num_rows, not necessary.

 

Basically check the data in the database for the username you are entering. Also I would use $sql instead of $query in the first or die portion.

 

Give that a try and see what happens.

 

I would also print out $user to make sure it is what you expected.

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.