PHP Script: syntax 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 am getting the following error in my browser when trying to run this script:
Parse error: syntax error, unexpected end of file in C:\wamp\www\server2.php on line 35
Note: Line 35 is the last line in the script. Sorry I am very new to PHP
<?php
// get the command
$command = $_REQUEST['command'];
// determine which command will be run
if($command == "getAnimalList") {
// return a list of animals
echo "bird,dog,cat,cow,sheep";
} else if($command == "getAnimalSound") {
// get the animal parameter and send the right response
$animal = $_REQUEST['cat'];
// fetch the sound of the animal from the database
$username = "root"; $password = ""; $hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("rosslocalhost1",$dbhandle)
or die("Could not select examples");
//execute the SQL query and try to return a record
$result = mysql_query("SELECT sound FROM animalsounds WHERE name='$animal'");
if (!$result) {
echo 'Dont know' .mysql_error();
exit;
}
$row = mysql_fetch_array($result);
echo $row['sound'];
?>

You've missed the closing bracket in the elseif statement of if($command == "getAnimalList")
<?php
// get the command
$command = $_REQUEST['command'];
// determine which command will be run
if($command == "getAnimalList") {
// return a list of animals
echo "bird,dog,cat,cow,sheep";
} else if($command == "getAnimalSound") {
// get the animal parameter and send the right response
$animal = $_REQUEST['cat'];
// fetch the sound of the animal from the database
$username = "root"; $password = ""; $hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("rosslocalhost1",$dbhandle)
or die("Could not select examples");
//execute the SQL query and try to return a record
$result = mysql_query("SELECT sound FROM animalsounds WHERE name='$animal'");
if (!$result) {
echo 'Dont know' .mysql_error();
exit;
}
$row = mysql_fetch_array($result);
echo $row['sound'];
}
?>

Here is your code properly indented. You did not properly close else if ($command == "getAnimalSound")
<?php
// get the command
$command = $_REQUEST['command'];
// determine which command will be run
if ($command == "getAnimalList") {
// return a list of animals
echo "bird,dog,cat,cow,sheep";
} else if ($command == "getAnimalSound") {
// get the animal parameter and send the right response
$animal = $_REQUEST['cat'];
}
// fetch the sound of the animal from the database
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("rosslocalhost1", $dbhandle) or die("Could not select examples");
//execute the SQL query and try to return a record
$result = mysql_query("SELECT sound FROM animalsounds WHERE name='$animal'");
if (!$result) {
echo 'Dont know' . mysql_error();
exit;
}
$row = mysql_fetch_array($result);
echo $row['sound'];
?>

Related

Retrieving value from database and checking it with PHP [duplicate]

This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 5 months ago.
I am trying to retrieve some value from a column in the database.
The idea is to retrieve the IP of the person, then check their country, after that check the database if the country is blacklisted or not. There seems to be something wrong in the code, may you please give an advise?
This is the code:
<?php
//Get IP and country name
$ip=$_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("https://get.geojs.io/v1/ip/country/$ip.json"));
$country=$details->name;
$servername = "localhost";
$username = "My_Username";
$password = "My_password";
$dbname = "Database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT country_name FROM uni_country";
$result = $conn->query($sql);
if (strpos($result, $country) == true) {
echo $country ." " . "is banned";
} else {
echo "Your country is not banned";
}
$conn->close();
?>
When I run it, it shows "Your country is not banned" - when it should actually say that my country is banned (I added a country to the database to test this).
The issue seems to be with reading the data from the database.
If I do echo $country; --> It actually shows my country, so it is retrieving it correctly (from geojs.io). But the code is not pulling the data from the database and verifying it.
Update:
If I do echo $result; the page returns Error 500.
Update
tried this new code, now it's saying that the country is not in the database, although it is actually there.
<?php
$ip=$_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("https://get.geojs.io/v1/ip/country/$ip.json"));
$country=$details->name;
$conn = mysqli_connect("localhost", "database", "password", "username");
$query = mysqli_query("SELECT * FROM `uni_country` WHERE country_name = '$country'");
if(mysqli_num_rows($query)>0) {
echo "Country Is in the database";
}
else {
echo "Country is not in the database";
}
?>
After trying for hours, this is the correct code:
<?php
$ip=$_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("https://get.geojs.io/v1/ip/country/$ip.json"));
$country=$details->name;
// Create connection
$servername = "localhost";
$username = "MyUsername";
$password = "MyPassword";
$database = "MyDatabase";
$conn = new mysqli($servername, $username, $password, $database);
$sql = "SELECT * FROM uni_country WHERE country_name ='$country'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "You country is currently banned.";
}
} else {
echo "Your country is not banned!";
}
?>

Checking already existing username or not MySQL, PHP

I Cannot Check whether the username already exist in database. I gone through existing questions that were answered here. None of them solved my problem. When i executes, it displays "Cannot select username from table", which i given inside die block. Code Is given below.
<?php
$username = $_POST['user_name'];
$password = $_POST['pass_word'];
$host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "my_db";
//create connection
$conn = #new mysqli($host, $db_username, $db_password, $db_name);
if (isset($_POST["submit"]))
{
# code...
//check connection established or not
if ($conn->connect_error)
{
die("Not Connected to DB");
}
else
{
$query = "SELECT 'usernamedb' FROM 'registration' WHERE usernamedb='$username'";
$result = mysqli_query($conn, $query) or die('Cannot select username from table');
if (mysqli_num_rows($result)>0)
{
$msg.="This username already exist. try Another !!";
}
else
{
$insert = "INSERT INTO 'registration'('id', 'usernamedb', 'password') VALUES ([$username],[$password])";
$insert_result = mysqli_query($conn,$insert) or die('INSERTION ERROR');
}
}
$conn->close();
}
?>
Hope someone will answer me.
First of all you should not use those unescaped queries.
But regarding your question you have an SQL error on your queries. You quoted table name. "FROM 'registration'" should be "FROM registration".

Seems like SQL's WHERE clause and JOIN do not work together

I am trying to make a basic forum, and I am having trouble printing just one row in SQL. Here is my PHP:
<?php
ob_start();
$host = "localhost";
$user = "root";
$pass = "MYPASSWORD";
$db = "MYDB";
$conn = mysqli_connect($host, $user, $pass, $db) or die("cannot connect to database.");
$sql = "SELECT * FROM forum WHERE fid = '{$fid}' JOIN user ON forum.creator=user.id;";
$result = mysqli_query($conn, $sql);
if ($result == true) {
while ($row = mysqli_fetch_assoc($result)) {
print "<h1>{$title}</h1>";
}
}
else {
print "failed to reach post.";
}
ob_flush();
?>
To help out, I believe everything works except for $sql. I have enabled ini_set('display_errors',1); but I am getting no error messages (excpet for my own that I made for the else statement).
Try this query
SELECT * FROM forum JOIN user ON forum.creator=user.id WHERE fid = '{$fid}' ;

php mysql script not working

I got my script working for one column of data but I am trying to send it other data to a second column in mysql table. Here's my php code:
<?php
function db_connect()
{
$hostname = '127.0.0.1';
$db_user = 'root';
$db_password = '';
$db_name = 'hit';
mysql_connect ($hostname, $db_user, $db_password) or die (mysql_error());
echo "Success.. Connected to MySQL...<br />";
mysql_select_db($db_name) or die(mysql_error());
echo "Success.. Connected to Database...<br /> ";
}
function insertData($DATA)
{
function insterData($DATA2)
{
db_connect();
$requete = "INSERT INTO data SET col_Data='".$DATA."'";
if(!mysql_query($requete))
echo mysql_error();
else
echo 'data accepted.';
$requete2 = "INSERT INTO data SET col_Data2='".$DATA2."'";
if(!mysql_query($requete2))
echo mysql_error();
else
echo 'data accepted.';
}
if(isset($_GET['DATA']))
if(isset($_GET['DATA2']))
}
insertData($_GET['DATA']);
insertData($_GET['DATA2']);
}
else
{
echo 'Nop';
}
?>
This is how I send the post data
http://localhost/hit.php?DATA=iamwicked&DATA2=iamcool
This then suppose to send DATA=iamwicked goes into database hit table data column col_data
This then suppose to send DATA2=iamcool goes into database hit table data column col_data2
But I get this error,
but there are errors can someone help me debug.
Here is a working script:
<?php
function db_connect()
{
$hostname = '127.0.0.1';
$db_user = 'root';
$db_password = '';
$db_name = 'hit';
mysql_connect ($hostname, $db_user, $db_password) or die (mysql_error());
echo "Success.. Connected to MySQL...<br />";
mysql_select_db($db_name) or die(mysql_error());
echo "Success.. Connected to Database...<br /> ";
}
function insertData($DATA)
{
db_connect();
$requete = "INSERT INTO data SET col_Data='".$DATA."'";
if(!mysql_query($requete))
echo mysql_error();
else
echo 'data accepted.';
}
if(isset($_GET['DATA']))
{
insertData($_GET['DATA']);
}
else
{
echo 'Nop';
}
?>
this is a working script when I use this url to post data
localhost/hit.php?DATA=iamwicked
When I use this it save iamwicked in database hit table data column col_data
so how do I fix my script to send more data to col_data2 and so forth
Return $conn connection resource #id from function
<?php
function db_connect()
{
$hostname = '127.0.0.1';
$db_user = 'root';
$db_password = '';
$db_name = 'hit';
$conn = mysql_connect ($hostname, $db_user, $db_password) or
die (mysql_error());
echo "Success.. Connected to MySQL...<br />";
mysql_select_db($db_name) or die(mysql_error());
echo "Success.. Connected to Database...<br /> ";
return $conn;
}
$conn = db_connect();
To insert single field
function insertData($DATA)
{
$requete = "INSERT INTO data SET col_Data='".$DATA."'";
mysql_query($requete) or die(mysql_error());
}
if(isset($_GET['DATA'])) {
insertData($_GET['DATA']);
}
if(isset($_GET['DATA2'])) {
insertData($_GET['DATA2']);
}
UPDATE
To insert multiple fields
function insertData($DATA, $DATA2)
{
$requete = "INSERT INTO data SET col_Data='".$DATA."', col_Data2='".$DATA2."'";
mysql_query($requete) or die(mysql_error());
}
if(isset($_GET['DATA']) && isset($_GET['DATA2'])) {
insertData($_GET['DATA'], $_GET['DATA2']);
}
?>
I think you have an wrong spelling here:
function insertData($DATA2) instead of function insterData($DATA2);
There are indeed two problems here.
function insertData($DATA)
{
function insterData($DATA2)
{
What are you trying to achieve here? Declaring a function inside another function is totally useless (and generates errors since it's not allowed). If you want to call a function inside another one you must declare them separately and then call them, f.e.
function insertData($DATA)
{
insterData($somevariable);
//Rest of the operations
}
This should be clear enough. There is another error though.
if(isset($_GET['DATA']))
if(isset($_GET['DATA2']))
}
insertData($_GET['DATA']);
insertData($_GET['DATA2']);
}
else
{
echo 'Nop';
}
I suppose there is a typo here, and you meant
if(isset($_GET['DATA2']))
{

php script to execute sql statements inside a dump file

I have a php script that is supposed to execute several mysql statements, everything works if there are no comments /* */ and no breaklines...
Could you please help me add this functionality, also ignore -- comments
<?
$sqlFileToExecute = 'mysql_dump.sql';
$hostname = 'localhost';
$db_user = 'root';
$db_password = '';
$database_name = 'db_';
$link = mysql_connect($hostname, $db_user, $db_password);
if (!$link) {
die ("error connecting MySQL");
}
mysql_select_db($database_name, $link) or die ("wrong DB");
$f = fopen($sqlFileToExecute,"r+");
$sqlFile = fread($f, filesize($sqlFileToExecute));
$sqlArray = explode(';',$sqlFile);
foreach ($sqlArray as $stmt) {
//THIS SEEMS NOT TO WORK
if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') {
$result = mysql_query($stmt);
if (!$result) {
$sqlErrorCode = mysql_errno();
$sqlErrorText = mysql_error();
$sqlStmt = $stmt;
break;
}
}
}
if ($sqlErrorCode == 0) {
echo "SETUP COMPLETED ;)";
} else {
echo "FAIL!<br/>";
echo "Error code: $sqlErrorCode<br/>";
echo "Error text: $sqlErrorText<br/>";
echo "Statement:<br/> $sqlStmt<br/>";
}
?>
Are you sure you need to run it this manner:
You can also use
$mysql -pxxx -u username db_name -vvv < sourcefile.sql >/tmp/outfile.log
Enable verbose mode to check the stats, else you can omit -vvv option
OR
mysql > source "sourcefile.sql"
You can see more options here

Categories