EvilZone
Programming and Scripting => Web Oriented Coding => : Ethereal March 17, 2013, 01:49:10 AM
-
Hello. I wanna display data from database but there is nothing on the page. Take a look in the script. Btw i try with echo $id and nothing appends in browser.
user_profile.php
<?php
require 'core.inc.php';
require 'connect.inc.php';
require 'loginform.inc.php';
$id = $_SESSION['user_id'];
$query = "SELECT * FROM `users` WHERE `id` = '$id'";
$query_run = mysql_query($query);
WHILE($row = mysql_fetch_array($query_run)):
$ime = $row['ime'];
$prezime = $row['prezime'];
endwhile;
echo $id;
?>
loginform.inc.php
<?php
if (isset($_POST['username'])&&isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password_hash = md5($password);
if (!empty($username)&&!empty($password)) {
$query = "SELECT `id` FROM `users` WHERE `username` = '".mysql_real_escape_string($username)."' AND `password` = '".mysql_real_escape_string($password)."'";
if($query_run = mysql_query($query)){
$query_num_rows = mysql_num_rows($query_run);
if($query_num_rows==0){
echo 'invalid username/password combination';
}else if ($query_num_rows == 1){
$user_id = mysql_result($query, 0, 'id');
$_SESSION['user_id']=$user_id;
header('Location: user_profile.php');
}
}
}else{
echo "<p id='warning'>'Moras popuniti sva polja'</p>";
}
}
?>
[size=78%]core.inc.php[[/size][size=78%]/b][/size]
<?php
error_reporting(E_ALL ^ E_NOTICE);
ob_start();
session_start();
$current_file = $_SERVER['SCRIPT_NAME'];
$http_referer = $_SERVER['HTTP_REFERER'];
function loggedin() {
if(isset($_SESSION['user_id'])&&!empty($_SESSION['user_id'])){
return true;
}else{
return false;
}
}
function getuserfield($field){
$query = "SELECT `$field` FROM `users` WHERE `id`='".$_SESSION['user_id']."' ";
if($query_run=mysql_query($query)){
if ($query_result = mysql_result($query_run ,0 ,$field)){
return $query_result;
}
}
}
?>
-
your while loop should be like this
while(){
}
and you need a session_start(); before you call $_SESSION['user_id']
-
I tryed but nothing....
-
try putting an or die(mysql_error()) on the end of your queries if you get an error somethings wrong with them
-
Also, where do you connect to the database? Even if its in one of your includes, btw, you have to call the connection with each mysql query.
Eg: mysql_query($query, $con)or die(mysql_error());
with $con being the connection variable
EDIT: also, its good to know that unless your using indexed results, use mysql_fetch_assoc instead of fetching an array. Its faster.
-
All working good with or die.. But i tried if i put in query `username` = me. Then display all my information. But when i put session['user_id']; there is nothing on page. when im tryed to echo my id also there are nothing on page
-
try print_r($id); and see what you get
-
Nothing on page...
-
try this
<?php
require 'core.inc.php';
require 'connect.inc.php';
require 'loginform.inc.php';
if(isset($_SESSION['user_id'])){
$id = $_SESSION['user_id'];
}else{
die("session hasnt been started");
}
$query = "SELECT * FROM `users` WHERE `id` = '$id'";
$query_run = mysql_query($query){
while($row = mysql_fetch_array($query_run)):
$ime = $row['ime'];
$prezime = $row['prezime'];
}
echo $id;
?>
-
on page output "session hasnt been started"
-
then you need to start the session session_start()
-
i tried but nothing :O
-
can you show me the updated code
-
<?php
require 'core.inc.php';
require 'connect.inc.php';
require 'loginform.inc.php';
session_start();
if(isset($_SESSION['user_id'])){
$id = $_SESSION['user_id'];
}else{
die("session hasnt been started");
}
$query = "SELECT * FROM `users` WHERE `id` = '$id'";
$query_run = mysql_query($query);
WHILE($row = mysql_fetch_array($query_run)):
$ime = $row['ime'];
$prezime = $row['prezime'];
endwhile;
echo $id;?>
i aslo tryed with session['session_id']; but nothing
-
just do a
print_r($_SESSION);
to see if you even have anything in there
and remove
ob_start();