Jump to content

[SOLVED] Problems with index.php code


JoelRocks

Recommended Posts

Despite help, still having issues with this code, can anyone help me...

 

Gives an error i think its the SQL PHP functions...

 

<?php

$hostname= "localhost";
$user= "remotepa_framewo";
$password = "-";

$conn = @mysql_connect(  $hostname, $user, $password )
or die ("Could not connect to server");

$db = @mysql_select_db("remotepa_framework", $conn)
or die ("Could not connect to database");

$sql = "SELECT * FROM users WHERE username=\"$_SESSION['username']\"";

$result = @mysql_query( $sql, $conn)
or die ("Could not execute query");

$output = mysql_fetch_assoc($sql);
session_start();
session_id($_GET['PHPSESSID']);//set php session id from URL
if(isset($_SESSION['username']))
{	
if($output['active'] < 1)	
{		
echo ("Please reset your details");
echo ("Hello " .$_SESSION['username']);
echo ("<br />");
echo ("You are logged in successfully");
}
}
else
{
echo ("Sorry you are not logged in");
exit;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/66545-solved-problems-with-indexphp-code/
Share on other sites

Though it is best if you post the error so people know what to look for, I don't think you should be using double quotes in the sql query.

Change:

$sql = "SELECT * FROM users WHERE username=\"$_SESSION['username']\"";
[code]

to:
[code]
$sql = "SELECT * FROM users WHERE username='$_SESSION['username']'";

[/code][/code]

Further errors:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/remotepa/public_html/Joel/index.php on line 16

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/remotepa/public_html/Joel/index.php:16) in /home/remotepa/public_html/Joel/index.php on line 25

Try:

 

<?php
session_start();
session_id($_GET['PHPSESSID']);//set php session id from URL
$hostname= "localhost";
$user= "remotepa_framewo";
$password = "-";

$conn = @mysql_connect(  $hostname, $user, $password )
or die ("Could not connect to server");

$db = @mysql_select_db("remotepa_framework", $conn)
or die ("Could not connect to database");

$sql = "SELECT * FROM users WHERE username='".$_SESSION['username']."'";

$result = mysql_query( $sql, $conn) or die ("Could not execute query");

$output = mysql_fetch_assoc($result);
if(isset($_SESSION['username']))
{	
if($output['active'] < 1)	
{		
echo ("Please reset your details");
echo ("Hello " .$_SESSION['username']);
echo ("<br />");
echo ("You are logged in successfully");
}
}
else
{
echo ("Sorry you are not logged in");
exit;
}
?>

 

You need to start the session before any output. You also need to start it before making use of the $_SESSION array.

 

The reason for the mysql_fetch_assoc error is because there are no results being returned by your query, because $_SESSION['username'] is undefined without starting the session previously

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.