I'm trying to retrive my first name and last name for viewprofile.php but i'm getting resource ID#5 . I am CREATING session in Login page after successful authentication. And I am trying to use it here. I'm trying to use a session which has been created to fetch the data from the database.
<html>
<h1> My Profile</h1>
<?php
session_start();
require "config.php";
$con = mysql_connect("localhost", $db_user, $db_pass);
if(!$con)
{
die('cound not connect: '. mysql_error());
}
mysql_select_db($db_name, $con);
# include 'new.php';
echo $_SESSION['username'];
#$usname = $_SESSION['username'];
#echo 'local var: ', $usname;
$sql1 = "SELECT * FROM register WHERE uname='".$_SESSION['username']."'" ;
#echo $sql1 ;
$result= mysql_query($sql1)or die(mysql_error());
#echo "res: " . $result;
while($row = mysql_fetch_array($result))
{
echo $row['fname']." ".$row['lname'];
echo"</br>";
}
mysql_close($con);
?>
</html>
I'm getting resource ID#5. Searched numerous places no luck. Kindly help
Unless I'm mistaken mysql_fetch_array will give you values that can be expressed as $row[0], $row[1] etc whereas mysql_fetch_assoc is what you're trying to use to get $row['fname'] etc
Change your code to
while($row = mysql_fetch_assoc($result))
In either case a print_r($row) within your while loop will you how it's made up.
Related
I am using mysql_insert_id() on the first page (test2.php) to insert the same id on other tables that is submitted from different forms. The id is inserted perfectly however, when i want to insert the data from other forms (e.g: test.php), the data cannot be updated, I have to inform before this i was using insert and it is not systematic because it will insert a new data with new id. As reference, below are my php codings for page test2.php and test.php:
test2.php:
<?php
session_start();
include("auth.php");
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('tempahperalatan');
if(isset($_POST['submit'])){
$noID = $_POST['noID'];
$pemohon = $_POST['pemohon'];
$trkhMula = $_POST['trkhMula'];
$trkhAkhir = $_POST['trkhAkhir'];
$n_program = $_POST['n_program'];
$lokasi = $_POST['lokasi'];
$n_anjuran = $_POST['n_anjuran'];
$catatan = $_POST['catatan'];
mysql_query("INSERT INTO daftartempah (noID, pemohon, trkhMula, trkhAkhir, n_program, lokasi, n_anjuran, catatan) values ('$noID','$pemohon','$trkhMula','$trkhAkhir','$n_program','$lokasi','$n_anjuran','$catatan')");
mysql_query("INSERT INTO pasystems (noID) values ('$noID')");
mysql_query("INSERT INTO ict_1 (noID) values ('$noID')");
mysql_query("INSERT INTO pejabat (noID) values ('$noID')");
printf("Last inserted record has id %d\n", mysql_insert_id());
}
?>
For page test.php, there is an echo of variables is because I was testing whether it reads the variables, and surprisingly it reads because it appear at the top of the page of the data that I entered however, the noID does not appear and that means it does not fetch the same id from the previous page (test2.php) and I think that is the problem why my data was not updated. Can anyone help me about this problem because I'm a student studying web programming and some guidance would be so helpful, thank you in advance.
test.php:
<?php
session_start();
include("auth.php");
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('tempahperalatan');
if(isset($_POST['submit'])){
$noID = $_POST['noID'];
$microphones = $_POST['microphones'];
$amplifiers = $_POST['amplifiers'];
$loudspeakers = $_POST['loudspeakers'];
$mixers = $_POST['mixers'];
$catatan = $_POST['catatan'];
?>
<p><?php echo $microphones;echo $amplifiers; echo $loudspeakers; echo $mixers; echo $catatan; echo $noID; ?></p>
<?php
mysql_query ("UPDATE pasystems SET microphones='$microphones', amplifiers='$amplifiers', loudspeakers='$loudspeakers', catatan='$catatan' WHERE noID = '$noID'");
}
Please ask if my question is not clear for you to help me, I will try to help you understand my problem/question.
here is what i have try so far
<?php
//Convert the email to variable
$Tnumber2 = "{$_SESSION['Tnumber2']}";
// Connect to the database
$db = mysql_connect("$Sname","$Uname","$Pname") or die("Could not connect to the Database.");
$select = mysql_select_db("$Dname") or die("Could not select the Database.");
$sql="SELECT * FROM $Tname WHERE Tnumber2='".$Tnumber2."'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
and here is the codes i use to echo out the variables from my mysql database
<?php echo $rows['Tnumber2']; ?>
<?php echo $rows['Idate']; ?>
<?php echo $rows['Iaddress']; ?>
thanks any help will be appricated.
try
$sql="SELECT * FROM `$Tname` WHERE Tnumber2='".$Tnumber2."'";
$result=mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo $rows['Tnumber2'];
echo $rows['Idate'];
echo $rows['Iaddress'];
}
You should change the line
$result=mysql_query($sql);
to
$result=mysql_query($db,$sql)
But also, you haven't set the variable $Tname, or started a session with session_start();. Plus, there are a bunch of security holes.
My login code:
<?php
session_start();
$f_usr= $_POST["userid"];
$f_pswd= $_POST["password"];
$_SESSION['user']=$f_usr;
$con=mysql_connect("localhost","root","");
if(! $con)
{
die('Connection Failed'.mysql_error());
}
mysql_select_db("finaltest",$con);
$result=mysql_query("select * from user");
while($row=mysql_fetch_array($result))
{
if($row["username"]==$f_usr && $row["password"]==$f_pswd)
header('Location: selectdata.php');
else
echo"Sorry : $f_usr";
}
?>
selectdata.php
<?php
session_start();
$s= $_SESSION['user'];
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("finaltest") or die(mysql_error());
$select="temperature".$s;
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM $select")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>username</th> <th>password</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['username'];
echo "</td><td>";
echo $row['password'];
echo "</td></tr>";
}
echo "</table>";
?>
actually the session varibale is not getting parsed i am getting an error:
Notice: Undefined index: user in C:\xampp\htdocs\bars\selectdata.php on line 3
and i have an other problem i want to select a database named "temperaturexyz" where temperature i want to store in a string and xyz is variable that i am getting via the session i want to combine the both so that i can get a variable which i can use in the query
Regarding your session: It appears your session variable $_SESSION['user'] which you attempt to set in your login code is not correctly setting the variable as expected.
try:
<?php
session_start();
$f_usr= $_POST["userid"];
$f_pswd= $_POST["password"];
$_SESSION['user']=$f_usr;
echo $_SESSION['user'] //<-------- see if this echo's out the value you are expecting
As for your variable:
I assume you mean you want a variable name $temperaturexyz but you are creating the variable name dynamically. so
$select="temperature".$s;
$$select = /* Insert whatever value you need here*/ //<-- you can then call this variable like this (ie. $temperaturexyz)
Im blocked at following part of code...
i have config.php page, with following code inside:
<?php
// Start the session (pretty important!)
session_start();
// Establish a link to the database
$dbLink = mysql_connect('localhost', 'USER', 'PASS');
if (!$dbLink) die('Can\'t establish a connection to the database: ' . mysql_error());
$dbSelected = mysql_select_db('DATABASE', $dbLink);
if (!$dbSelected) die ('We\'re connected, but can\'t use the table: ' . mysql_error());
$isUserLoggedIn = false;
$query = 'SELECT * FROM users WHERE session_id = "' . session_id() . '" LIMIT 1';
$userResult = mysql_query($query);
if(mysql_num_rows($userResult) == 1){
$_SESSION['user'] = mysql_fetch_assoc($userResult);
$isUserLoggedIn = true;
}else{
if(basename($_SERVER['PHP_SELF']) != 'index.php'){
header('Location: index.php');
exit;
}
}
?>
Now i have another page, with following code inside:
<?php include_once('config.php'); ?>
<?php foreach($_SESSION['user'] as $key => $value){ ?>
<li><?php echo $key; ?> <strong><?php echo $value; ?></strong></li>
<?php } ?>
That code show me all informations stored in database, one by one..
I want to show informations, individualy in my page, something like:
<?php echo $email; ?>
etcetera..
Can someone explain me how to do that?
Thank you
You should output it like
<?php echo $_SESSION['user']['email'] ?>
assuming that 'email' is the column name in the users table.
Just address the relevant field in the associative array:
<?php echo $value['email';?>
Use this in the config file:
$_SESSION['user']['email'] = mysql_fetch_assoc($userResult);
instead of this
$_SESSION['user'] = mysql_fetch_assoc($userResult);
in your code. You can pass as many columns you want to pass from your table.
And in another page use
foreach($_SESSION['user']['email'] as $key => $value)
instead of
foreach($_SESSION['user'] as $key => $value)
in your code.
Remember number of columns you pass in config file, all those columns should be called in this page.
Ok, So I have a external php script that get data from a DB and displays it in a table. I want to run it in a specific div in my html so the data gets echoed out in the right place?
Any ideas how to do that?
Html div
<div id="statsContent">
<?php include('updatestats.php'); ?>
</div>
Heres the PHP code.
<?php
//Start session
session_start();
//Make sure user is logged in
require_once('auth.php');
//Include database connection details
require_once('config.php');
//Connect to DB
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Create Querys
$query = "SELECT * FROM stats WHERE member_id='" . $_SESSION['SESS_MEMBER_ID'] . "' ";
$result = mysql_query($query);
//Gather the whole row into an array
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$money = $row['money'];
$bank_money = $row['bank_money'];
$ap = $row['ap'];
$exp = $row['exp'];
}
//Now create a table to display the data to the user
echo "<table>
<tr>
<td>Money $$money</td>
<td>Action Points $ap</td>
<td>Experience $exp</td>
</tr>";
?>
you can include PHP script in any tag by calling
include("path_to/myscript.php") or require("path_to/myscript.php")
<div>
<?php include("path_to/myscript.php"); ?>
</div>
<div><?php *whatever you want to do inside the div*?></div>
just include it inside your div by using:
<?php include('filename.php'); ?>