Need php help

thenewprogrammer

New Member
Messages
45
Reaction score
0
Points
0
trying to grab data from table and than echo it out in profile but its not working or if anyone has better profile way they like to help me with

Code:
<? 
$user = $_GET['user']; 
session_start(); 
 
if(isset($_SESSION['user'])){ 
} else { 
echo " 
<script language='javascript'> 
alert('Sorry, but you must login to view the Profile area!');
</script> 
<script> 
window.location='http://thenewprogrammer.x10hosting.com'
</script> 
"; } 
 

$dbhost = "localhost"; 
$dbname = "*****"; 
$dbuser = "*****"; 
$dbpass = "*****"; 
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); 
mysql_select_db($dbname) or die(mysql_error());
$query="SELECT * FROM login WHERE username='$username'";
$result=mysql_query($query);
$row = mysql_fetch_array ( $result );
print_r($row);
?>
<html>
<head>
User Profile
</head>
<body>
<? echo $username ?>
</body>
</html>
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
What happens when you run the script? Error, blank page, ect.
 

thenewprogrammer

New Member
Messages
45
Reaction score
0
Points
0
What happens when you run the script? Error, blank page, ect.
it says User Profile like i have in head. When not loged in the java script works and redirects me to main page so nothing wrong with that but i need to get data from a table for a profile. But nothing happens when i echo it out.
 
Last edited:

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
If you are getting no errors, then that means that there is no entry in the database for that username. In this case, you used the wrong variable so the query came back true, with no rows. Look into error handling with the mysql_num_rows function.

The problem is that you put the $_GET data into a variable named user, then reference it in a variable called username. Simply replace it with $username = $_GET['user'];

Another thing I recommend is using the mysqli_* family of functions, which allows multiple database connections referenced from a resource variable (Ex. $con = mysqli_connect(...)).
 
Last edited:

nexhunter

New Member
Messages
239
Reaction score
1
Points
0
PHP:
$num_rows = mysql_num_rows($result);
if(empty($num_rows)){
echo 'Sorry this user does not exist.';
}
else{
$row = mysql_fetch_array($result);
print_r($row);
}
try using mysql_num_rows as twinkie mentioned to see if your pulling any data, which then can help track down the problem if nothing is being pulled.
 
Last edited:

zapzack

New Member
Messages
606
Reaction score
19
Points
0
I dont advise the use of javascript.. I suggest using the php header function..

Code:
header("Location: http://google.com");

Try this:

Code:
<? 
$user = $_GET['user']; 
session_start(); 
 
if(!isset($_SESSION['user'])){ 
header("Location: http://thenewprogrammer.x10hosting.com");
} 

$dbhost = "localhost"; 
$dbname = "*****"; 
$dbuser = "*****"; 
$dbpass = "*****"; 

mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect: ".mysql_error()); 
mysql_select_db($dbname) or die("Could not select the database: ".mysql_error());
$username = mysql_real_escape_string($user);
$query = "SELECT * FROM login WHERE username ='".$username."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
?>
<html>
<head>
User Profile
</head>
<body>
<?php echo($username); ?>
<br />
<?php print_r($row); ?>
</body>
</html>

I'm not sure if it will work.. I usually do trial and error.. I stripped characters to prevent SQL Injection btw.. heh
 
Last edited:
Top