Parse error: Unexpected end of file? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've double-checked and everything looks closed to me, so I can't find the error. I just want to create a table to display mySQL data.
EDIT: I don't know why the closing tag was above the rest of the code, but I still get the error when it's in the correct place.
<?php
$servername = "localhost";
$username = “x”;
$password = “x”;
$dbname = “x”;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM Classroom”;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo “<tr><th>Building</th><th>Floor</th><th>Room</th><th>Instructional</th><th>Type<th>Size</th>
<th>Seating</th><th>Decking</th><th>Access</th><th>Whiteboard</th><th>Chalkboard</th></tr>”;
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo “<tr><td>”.$row[“building”].”</td></tr>”;
}
} else {
echo (“0 results”);
}
mysqli_close($conn);
}
?>

Edit: As per your original post https://stackoverflow.com/revisions/27974352/1
This should go at the very bottom:
?>
In fact, it isn't even required unless you're going to put some pure HTML after it. So leaving it out completely might save you headaches in the future.
However, several of your double-quotes look funky pasted here. You might check that they are just double-quotes, and not special characters.
These curly/smart quotes “ ” should be replaced by regular double quotes " throughout your code.
Those alone will break its functionality and cause a parse/syntax error.
Edit: As per your edit: You need to remove the last } in your file, the one just after mysqli_close($conn);. The number of braces do not match.

This works!
<?php
mb_internal_encoding('UTF-8');
$servername = "localhost";
$username = "x";
$password = "x";
$dbname = "x";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM Classroom";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<tr><th>Building</th><th>Floor</th><th>Room</th><th>Instructional</th><th>Type<th>Size</th>
<th>Seating</th><th>Decking</th><th>Access</th><th>Whiteboard</th><th>Chalkboard</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row["building"]."</td></tr>";
}
}else{
echo("0 results");
}
mysqli_close($conn);
?>

Remove ?> from all your documents, as it's unneeded, as PHP self closes at the end of a file.

Related

PHP connecting to database fails without error [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 3 years ago.
Improve this question
I am trying to connect to databse on my server by using this code:
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But code after $conn = new mysqli($servername, $username, $password); is not executing. When i try to echo anything after it I do not get output. Code before that line works as expected. I do not get any errors from php.
I am not sure what is problem. Could it be something server related? I did try to add:
ini_set('display_errors',1);
error_reporting(E_ALL);
but it didn't help, no errors have been displayed.
I have been trying many things from stackoverflow (and other places) but they didnt help, some examples:
Connecting to a mysql database from php, error but no error shown?
Connection of MySQL with PHP not working
There are multiple ways to handle errors
Simplest of all, use try catch while connecting
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
try {
$conn = new mysqli($servername, $username, $password); } catch(\Exception $e) { var_dump ('oopss... this is the error', $e)}
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected

php SELECT query giving error in while loop for fetch_assoc in mysqli object oriented [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 years ago.
I am writing a code which compares the user passwords using password verify and outputting the result accordingly. Also, I'm printing a hashed password from database before comparing. But, I am getting the error Fatal error: Call to a member function fetch_assoc() on boolean Here is the DbConnect.php file included in main file
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "task_manager";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Here is the main file with an error
<?php
include "DbConnect.php";
$email = "joshiashish191#gmail.com";
$user_password = "123456";
$sql = "SELECT password from users WHERE email = $email";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["password"];
}
if(password_verify($user_password, $result)){
echo "password matches!";
}
else
echo "Passwords do not match.";
?>
Whats wrong with this can anyone tell please?
email field expects a string, escaping quotes are missing in your query.
$sql = "SELECT password from users WHERE email = \"$email\"";

Call MySQL stored procedure from PHP

I have looked at several examples on how to call a MySQL stored procedure from PHP but none have helped me. The stored procedure works when run inside PHPMyAdmin but I am having trouble calling it from the web.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"CALL standings_build()");
if (mysqli_query($conn,$sql))
header('refresh:1; url=schedule_main_scores.php');
else
echo "failed";
?>
There's 2 problems here.
You're querying twice and using the wrong variable, being $sql instead of $result.
$result = mysqli_query($conn,"CALL standings_build()");
if (mysqli_query($conn,$sql))
^^^^^^^^^^^^ calling the query twice
^^^^ wrong variable, undefined
all that needs to be done is this:
if ($result)
and an else to handle the (possible) errors.
Error reporting and mysqli_error($conn) would have been your true friends.
http://php.net/manual/en/function.error-reporting.php
http://php.net/mysqli_error
Side note: You really should use proper bracing techniques though, such as:
if ($result){
echo "Success";
}
else {
echo "The query failed because of: " . mysqli_error($conn);
}
It helps during coding also and with an editor for pair matching.

A Database is connected but select query doesn't work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
My database is on dream host. And its unable to connect .
MY code is
<?php
$hostname = "mysql.demos.smartmobe.com"; // eg. mysql.yourdomain.com (unique)
$username = "nayacinema"; // the username specified when setting-up the database
$password = "****"; // the password specified when setting-up the database
$database = "nayacinema"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$databse);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM TblUsers");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
?>
it gives error like this
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/demo_smartmobe/demos.smartmobe.com/nayacinema/test.php on line 20
what may be the problem?
You have a spelling mistake.
$con=mysqli_connect($hostname,$username,$password,$databse);
Should be
$con=mysqli_connect($hostname,$username,$password,$database);
(database spelt wrong)
i have tested your code there was no connectivity.. because of spelling mistake here is correct code , i have tested on my local machine
<?php
$hostname = "localhost"; // eg. mysql.yourdomain.com (unique)
$username = "root"; // the username specified when setting-up the database
$password = ""; // the password specified when setting-up the database
$database = "test"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM user");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['user_name'];
echo "<br>";
}
?>
Thanks

PHP echo in footer instead of body [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Any one knows why my $result echo in footer instead of body? Is it an html maybe div problem or PHP?
Here is a something similar to what I have:
<?php
// 1. Create a database connection
$dbhost = "localhost";
$dbuser = "tester";
$dbpass = "12345";
$dbname = "practice";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
// 2. Perform database query
$SQLstring = "SELECT *FROM user ORDER BY username";
// Test if there was a query error
$QueryResult = #mysqli_query($connection, $SQLstring)
Or die("<p>Unable to execute query.</p>"
. "<p>Error code " . mysqli_errno($connection)
. ": " . mysqli_error($connection)) . "</p>";
?>
<!DOCTYPE>
<html>
<head>
</head>
<body>
<?php
//3. uses the mysqli_assoc() to print table
echo "<table>";
echo "<tr><th>Name</th><th>Address</th><th bgcolor='#0099FF'
align='left'>City</th><th bgcolor='#0099FF' align='left'>State</th></tr>";
$num_results = $QueryResult->num_rows;
for ($i=0; $i <$num_results; $i++) {
// 3. Use returned data (if any)
$Row = mysqli_fetch_assoc($QueryResult); //associative array
// output data from each row
echo "<tr><td>{$Row['username']}</td>";
echo "<td>{$Row['address']}</td>";
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
</tr>";
}
// 4. Release returned data - close query
mysqli_free_result($QueryResult);
?>
</body>
</html>
<?php
// 5. Close database connection
mysqli_close($connection);
?>
Any ideas? The table prints out fine no problem just not in the correct place on the site
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
</tr>"; //You got an error here!
}
I think you forgot to echo this line.
you should get parse error but i could not point that
just try change this lines
// output data from each row
echo "<tr><td>{$Row['username']}</td>";
echo "<td>{$Row['address']}</td>";
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
echo "</tr>";
}

Categories