PHP short tags not working as expected [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to make my php page working, by having html code combined with it. Here is a source code:
<?
$username="username";
$password="password";
$db_name="db_name";
mysql_connect('localhost',$username,$password) or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$userstable='profile';
$query = "SELECT * FROM $userstable order by id";
$result = mysql_query($query) or die(mysql_error());
?>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DayZSC | Showlist</title>
<meta charset="utf-8" />
<link type="text/css" rel="stylesheet" href="styles.css" />
<link rel="shortcut icon" href="favicon.ico" />
</head>
<body>
<div class="container">
<div id="logo">
<img src="img/logo.png" />
</div>
<?
while ($row=mysql_fetch_array($result)) {
?>
<div class='p_content'>
<h3>
<?
echo ".$row['name'].";
?>
</h3>
<p style="text-align: justify;">
<table align="center"><tbody><tr>
<td>Copies Sold (Week)</td>
<td>11152</td>
</tr>
<tr>
<td>Copies pirated (Week)</td>
<td>124214</td>
</tr>
</tbody></table>
</p>
<h2>Comments</h2><br /><br />
</div>
<?
}
?>
<div id="footer">All rights reserved &copy 2014 | Designed by BerdyevCreations</div>
</div>
</body>
</html>
So whenever I try to run this page, it gives me just a blank. Cannot find error log either. Please help?

I think you are using the PHP short tags wring, try and use this:
<?= $row['name'] ?>
Instead of your
<?
echo ".$row['name'].";
?

I think your problems is with php tags, you are curently using short ones while your php is probably not configured to accept them so change short tags
<?
To long tags
<?php

You need to activate the short code directive in PHP.INI if you have php version 5.3 and earlier
since 5.4 This directive is enabled by default
like this
php.ini
short_open_tag=On

Related

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\PhpDatabase\index.php on line 68 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 1 year ago.
I have no idea what's going on, I have looked over the code and it just wont pull up the webpage. I need help figuring out what is going on with this code.
<?php
include ('config.php');
require('db.php');
$query = "SELECT * FROM salerecords";
$result = mysqli_query($conn, $query);
echo $result;
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
mysqli_free_result($result);
mysqli_close($conn);
?>
<html>
<?php include('header.php'); ?>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" type="text/css"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<title>PHP Stores</title>
</head>
<main>
<div id="user_select">
<div id="options">
<div id="view">
<a href="displayRecs.php"><input type="submit" value="View" class="menu-
chosen">
</a>
</div>
</div>
</div>
</main>
<body>
<header>Manage Sales Records</header>
<h1>salesrecords</h1>
<?php foreach($posts as $vaule) : ?>
<div class="well">
<h3><?php echo $vaule['size']; ?>
</h3>
<small>Created on <?php echo $vaule['color'];?> by <?php echo $vaule['price']; ?>
</small>
<p><?php echo $vaule['Description']; ?>
</p>
</div>
</body>
</html>
The error "unexpected end of file" means that when PHP was reading through your code, it was expecting to get to something, but never did. Often, it means you have mismatched brackets, quote marks, etc - you've indicated the start of something, and PHP is waiting for you to indicate the end.
In this case, you have a foreach(): which needs to be paired with an endforeach;

php session issue on Hostgator server

I have a problem with the Hostgator DB server. first thing first, I am a new baby on this.
So, DB connects and I can log in but it doesn't allow me to save new user details and to pull out any info from DB. Everything works perfectly on localhost. So I realise that it's because of HostGator DB or $_SESSION. The code -
include 'connection.php';
session_start();
$id=$_SESSION['id'];
$query=mysqli_query($db,"SELECT * FROM users where user_id='$id'")or die(mysqli_error());
$row=mysqli_fetch_array($query);
And if I manually change $id to The No of user-id example-(1) everything works perfectly so I can save it and any changes appear on DB same as on a webpage. So seems it is not recognising Session id for the user who changes his user information.
The code -
include 'connection.php';
session_start();
$id=$_SESSION['id'];
$query=mysqli_query($db,"SELECT * FROM users where user_id='1'")or die(mysqli_error());
$row=mysqli_fetch_array($query);
Does anyone know what is the issue???
I don't care about security issues on the code. Just need to be able to run the PHP code on the HostGator host and DB.
The full code after I edited regarding suggestions -
<?php
session_start();
include('connection.php');
$id=$_SESSION['id'];
$query=mysqli_query($db,"SELECT * FROM users where user_id=$id");
$row=mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>IT SourceCode</title>
<link rel="stylesheet" href="libs/css/bootstrap.min.css">
<link rel="stylesheet" href="libs/style.css">
<link rel="stylesheet" href="main.css">
<style>
img {
border-radius: 50%;
}
</style>
</head>
<h1>Profile</h1>
<div class="profile-input-field">
<form method="post" action="#" >
<div class="form-group">
<td> <img src="<?php echo 'images/' . $row['profile_image'] ?>" width="100%" height="100%" alt="" class="button" value="<?php echo $row['profile_image']; ?>"> </td>
</div>
<div class="button" value="<?php echo $row['full_name']; ?>">
<h1><?php echo $row['full_name']; ?></h1>
</div>
<div class="form-group">
Facebook Page
</div>
<div class="form-group">
Send Email
</div>
<center>
Edit Profile
</center>
<center>
Log out
</center>
</form>
</div>
</html>
Got in Error log - PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given on line 6

PHP For Loop in code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am having trouble getting my for loop to work in php. I am trying to make my code loop the time ten times with my css formating
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="clockloop.css">
</head>
<body>
<div id="bodycontainer">
<h1> Clock Loop </h1><hr>
<?php for($i=0;$i<=10;$i++){
<div id="border">
<span id = "font">
<?php
echo date("G:i:s")
?>
</span>
</div>
<h3> Today is
<?php
echo date("F,j,Y")
?>
</h3>
}
?>
</div>
</body>
</html>
You messed up your php tags opening and closing in the wrong places
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="clockloop.css">
</head>
<body>
<div id="bodycontainer">
<h1> Clock Loop </h1><hr>
<?php for($i=0;$i<=10;$i++){?>
<div id="border">
<span id = "font">
<?php
echo date("G:i:s")
?>
</span>
</div>
<h3> Today is
<?php
echo date("F,j,Y")
?>
</h3>
<?php
}
?>
</div>
</body>
</html>
CONVERSELY .. You said you wanted 10 times .. This will output 11, as 0 is still a quantifiable number ..
You cannot just output HTML in PHP like that. You can echo or you can jump in and out like this:
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="clockloop.css">
</head>
<body>
<div id="bodycontainer">
<h1> Clock Loop </h1><hr>
<?php for($i=0;$i<=9;$i++){ ?><!-- note the closing PHP tag -->
<div id="border">
<span id = "font">
<?php
echo date("G:i:s")
?>
</span>
</div>
<h3> Today is
<?php
echo date("F,j,Y")
?>
</h3>
<?php } ?><!-- note the opening PHP tag -->
</div>
</body>
</html>
If you want 10 repeats you should end your count at 9 because 0 will be your first record. You could also start with $i = 1; and use 10 as the count to number.
Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1);this will reveal when you have errors in your syntax and alert you on where you should start debugging.
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="clockloop.css">
</head>
<body>
<div id="bodycontainer">
<h1> Clock Loop </h1><hr>
<?php for($i=0;$i<10;$i++){ ?> => look at this line
<div id="border">
<span id = "font">
<?php
echo date("G:i:s")
?>
</span>
</div>
<h3> Today is
<?php
echo date("F,j,Y")
?>
</h3>
<?php => look at this line
}
?>
</div>

The PHP code stops after the > in the if statement

For some reason my html stops recognizing the code for php after the first and second >, at the point marked with the comment code stops here.
The meaning for this that I need to get a picture from the database and then show it on the website as a header. I am very new to PHP and HTML. I am using phpmyadmin and sublimetext. the database is called stickerdome with the table images. in images there are 3 columns: id, name, link.
This is my code:
<head>
<?php
//Database Connect
$connect = mysql_connect("localhost","root","");
//Database Select
$db = mysql_select_db("stickerdome",$connect);
?>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title> StickerDome </title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="banner">
<?php
//Query
$query1 = mysql_query("SELECT * FROM images WHERE id = 1");
$num = mysql_num_rows($query1);
if ($num > 0) // code stops here right after the >
{
while ($output1 = mysql_fetch_assoc($query1))
{
$src = $output1['link'];
echo "<img src='".$src."' alt='Image1'>"; //code stops here right after the > for the second time
}
}
else
{
echo "No picture found.";
}
?>
</div>
This is the file in total:
<!DOCTYPE html>
<html>
<head>
<?php
//Database Connect
$connect = mysql_connect("localhost","root","");
//Database Select
$db = mysql_select_db("stickerdome",$connect);
?>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title> StickerDome </title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="banner">
<?php
//Query
$query1 = mysql_query("SELECT * FROM images WHERE id = 1");
$num = mysql_num_rows($query1);
if ($num IS NOT NULL)
{
while ($output1 = mysql_fetch_assoc($query1))
{
$src = $output1['link'];
echo "<img src='".$src."' alt='Image1'>";
}
}
else
{
echo "No picture found.";
}
?>
</div>
<div id="navigation">
<ul>
<li>
<a class="first" href="index.html">StickerDome</a>
</li>
<li>
<a class="second" href="samplepack.html">Sample Pack</a>
</li>
<li>
<a class="second" href="contact.html">Contact</a>
</li>
</ul>
</div>
<div id="pagename"><img src="Stickedomepagename.png"></div>
<div id="text">
<h1>Welcome to Stickerdome</h1>
<p class="text">Stickerdome is a website that gives you the best stickers you could ever imagine. It gives you <br>
- the best quality for every sticker <br>
- the best endurance that is usable outside <br>
- the biggest assortment <br>
<br>
For several years stickers started to became more popular. Everyone starting loving stickers. You can place them almost everywhere imaginable. But there is still one thing that nobody had and that is their own stickers. Yes, your own stickers! You can design your own stickers, send them to us, and we make sure that your stickers will be on your doormat within 5 days! Every design is possible, from round to square to with a border or without. You say it we make it!
</p>
</div>
</body>
<footer>
<p class="footer"> Stickerdome 2014 | Netherlands</p>
</footer>
</html>
you could try a different method or debug your sql.
Since i see your dutch im providing you with a link to a tutorial which greatly helped me the first time i worked with sql. maybe if you follow it you can fix your problem.
http://www.phphulp.nl/php/tutorial/overig/pdo-verbinden-met-verschillende-databases/534/inleiding/1364/

Can't use $_GET values properly when using php based css templates

If anyone can think of a better title, please let me know.
Right now I'm using a technique from this tutorial to get the width and height of the user's viewing window. It works, but only on the index.php page. I'm using a php based css file.
In the php based css file, everything would normally work fine, except that the first line at the top *$width_div_center = $GET['width']0.8; thinks the width is in string form. (Or something like that.) As a result, the $width_div_center variable is set to zero which causes a lot of issues. What am I doing wrong, or how can I get the php based css file to do a multiplication on *$GET['width']0.8 properly? Thank you for your assistance.
<html>
<head>
<title>Taylor Love</title>
<!--
<link rel="stylesheet" media="only screen and (color)" href="main.css" />
<link rel="stylesheet" href="main.css"/>
-->
<link rel="stylesheet" type="text/css" href="css/style.php" />
<?php
$content = "null";
include_once('content.php');
?>
</head>
<body class="body">
<!-- top header -->
<div class="div-center decorated-white">
<div class="header-background">
<div style="margin:10px;">
<font color="#AAA" >
hello, universe!
<?php
echo $_GET['width'] *.8;
echo "<h1>Screen Resolution:</h1>";
echo "Width : ".$_GET['width']."<br>";
echo "Height : ".$_GET['height']."<br>";
?>
</font>
</div>
</div>
</div><!-- div-center-->
<div class="div-center" style="margin-top:10px;">
<?php
include('sidenav.php');
?>
<div id="div-content" class = "decorated-white">
<?php echo $content; ?>
</div><!-- div-content-->
</div><!-- div-center-->
<div class="clear"></div>
<!-- top header
<div class="div-center decorated-white" style="margin-top:10px">
<?php
for ($i = 0; $i < 5; $i++){
echo "</br>";
}
?>
</div>-->
</body>
</html>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
I appear to be having issues separating two different pages of code.
You seem to be calling style.php without any $_GET parameters.
The $_GET parameters you use in index.php are not automatically passed to the style.php. script.
Try temporary hard-coding the 'width' parameter to see if it makes any difference
<link rel="stylesheet" type="text/css" href="css/style.php?width=100" />
This is because the request for the stylesheet is a separate GET request which has no idea about the referrer's get parameters.
this is probably because $_GET variables are usually strings. That is probably the cause of your problem. You could use the function floatval() to convert it to a float.
The first line should be $width_div_center = floatval($_GET['width'])*0.8;

Categories