Running a PHP script at a specific place in the code - php

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'); ?>

Related

how to echo out variables from my mysql database

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.

PHP show records from mysql

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.

Error in retriving data from database

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.

PHP Blog error "Unexpected End"

Hello i'm currently trying to create a blog for a school project and this is the code i've come up with.
<html>
<?php
$connection['host'] = '127.0.0.1';
$connection['user'] = 'root';
$connection['password'] = 'ascent';
$connection['webdb'] = 'login';
$connection['newstable'] = 'news';
if (isset($_GET['newsid']))
{
$id = (int)$_GET['newsid'];
connect::selectDB('webdb');
$result = mysql_query("SELECT * FROM news WHERE id='".$id."'");
$row = mysql_fetch_assoc($result); ?>
<div class='box_two_title'><?php echo $row['title']; ?></div>
<?php
Some cool way to post the "body" row here.
?>
</html>
Basicly i just want it to post the news on the website, i've loaned a bit of code from here and there and there's a couple of errors whenever i try. Help is greatly appreciated. :-)
You forgot the closing curly bracket, add <? } ?> before the closing <html> tag.
I have seen your code actually there are some errors in your code so firstly you have to connect with mysql database connection and then select DB. i am giving you the correct code so try this definitely it will help to create your blog.
<html>
<?php
$connection['host'] = '127.0.0.1';
$connection['user'] = 'root';
$connection['password'] = 'ascent';
$connection['webdb'] = 'login';
$connection['newstable'] = 'news';
if (isset($_GET['newsid']))
{
//code to set database connection
$link = mysql_connect($connection['host'], $connection['user'], $connection['password']);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// make login the current db
$db_selected = mysql_select_db($connection['webdb'], $link);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
//get news id
$id = (int)$_GET['newsid'];
$result = mysql_query("SELECT * FROM news WHERE id='".$id."'");
$row = mysql_fetch_assoc($result); ?>
<div class='box_two_title'><?php echo $row['title']; ?></div>
<?php // Some cool way to post the "body" row here. } ?> </html>
PHP is basically complaining, because you didn't close the curly brackets { of your if clause.
<html>
<?php
$connection['host'] = '127.0.0.1';
$connection['user'] = 'root';
$connection['password'] = 'ascent';
$connection['webdb'] = 'login';
$connection['newstable'] = 'news';
if (isset($_GET['newsid']))
{
$id = (int)$_GET['newsid'];
connect::selectDB('webdb');
$result = mysql_query("SELECT * FROM news WHERE id='".$id."'");
$row = mysql_fetch_assoc($result); ?>
<div class='box_two_title'><?php echo $row['title']; ?></div>
<?php
// Some cool way to post the "body" row here.
}
?>
</html>
Besides, although you're just starting with PHP, you should use PDO or mysqli to access the database. the mysql_X functions are deprecated.
When at least while developing check the error, MySQL might be returning to see why to query failed.

Using Dreamweaver, how can I populate a drop down list via PHP?

I am trying to populate a drop-down list via PHP embedded in HTML.
Here is what I have so far:
<select name="ChapterList" id="ChapterList" style="width:120px;">
<?php
$username = "xxxxxxxxxxx";
$password = "xxxxxxxxx";
$database = "xxxxxxxxxxxxxx";
$host = "xxxxxxxx.mydomainwebhost.com";
#mysql_connect($host, $username, $password) or die("Unable to connect to database");
#mysql_select_db($database) or die("Unable to select database");
$query = "SELECT * FROM Chapters ORDER BY Id";
$ListOptions = mysql_query($query);
while($row = mysql_fetch_array($ListOptions))
{
echo "<option value='".$row['Id']."'>".$row['ChapterName']."</option>"
}
?>
</select>
I know I am recieving the expected results because if I echo $row['ChapterName']; , the current values I have in the database are listed in the proper order, so why is it when I echo "<option value='".$row['Id']."'>".$row['ChapterName']."</option>" my list receives nothing at all?
You are missing a semi-colon at the end of your echo statement
while($row = mysql_fetch_array($ListOptions)) {
echo "<option value='".$row['Id']."'>".$row['ChapterName']."</option>";
}
?>
Note: Start using mysqli_() functions as mysql_() are no more maintained by PHP team..
try using this
<?php
$form='';
$link = odbc_connect ('databasename', 'username', 'password');
if (!$link)
{
die('Could not connect: ' . odbc_error());
}
echo 'Connected successfully .<br>';
//Query the database
$sql = "SELECT * FROM Chapters ORDER BY Id ";
$result = odbc_exec($link,$sql);
$selectbox='<select id=combox name=Chapters >';
while($bin =odbc_fetch_array($result))
{
$selectbox.= "<option value=\"$bin[Chapters]\">$bin[FChapters]</option>";
}
odbc_close($link);
$selectbox.='</select>';
echo "Select Name".$selectbox;
?>
this code is working perfectly for me
Ok... so I solved my own question in a way.
What I discovered was that my php was being commented out via <--! -->. I merely changed the file extension to .php as opposed to .html. The drop-down list worked immediately and was populated with the proper values.
But this raises another question... how can I get inline PHP to work? My site is hosted with MyDomain. Is there a setting I am missing somewhere?
try to use this
<select>
while($row = mysql_fetch_array($ListOptions))
{
$id=$row['Id'];
$cname=$row['ChapterName'];
echo "<option value='$id'>$cname</option>";
}
?></select>
I have correct them just look at once,
<?php
$username = "xxxxxxxxxxx";
$password = "xxxxxxxxx";
$database = "xxxxxxxxxxxxxx";
$host = "xxxxxxxx.mydomainwebhost.com";
$dbc=#mysqli_connect($host, $username, $password,$database) or die("Unable to connect to database");
?>
<select name="ChapterList" id="ChapterList" style="width:120px;">
<?php
$query = "SELECT * FROM Chapters ORDER BY Id";
$ListOptions = mysqli_query($dbc,$query);
while($row = mysqli_fetch_array($ListOptions,MYSQLI_ASSOC))
{
echo "<option value='".$row['Id']."'>".$row['ChapterName']."</option>";
}
?>
</select>

Categories