This question already has answers here:
Parse error: Syntax error, unexpected end of file in my PHP code
(19 answers)
Closed 3 years ago.
I am getting the Parse error: syntax error, unexpected end of file regardless of what I change/add to my code, the error constantly tells me that there is something wrong with my last line.
My PHP file should be saving form input to the database but this error seems to be getting in the way and I cannot figure out what is causing it.
PHP Code
<?php
function so56917978_upload_callback() {
//Register variables
$adddate = $_POST['adddate'];
$addcontact = $_POST['addcontact'];
$adda = $_POST['adda'];
$addb = $_POST['addb'];
$addincome = $_POST['addincome'];
$addpayment = $_POST['adddate'];
$addsubbie = $_POST['addsubbie'];
$addcust = $POST['addcust'];
//connect with Database
$host_name = 'zzz.hosting-data.io';
$database = 'zzz';
$user_name = 'zysql_connect($host_name, $user_name, $password);
if(!$connect) {
die('Not Connected To Server');
}
//Connection to database
if(!mysql_select_db($connect, $database)) {
echo 'Database Not Selected';
}
$query = mysql_query($connect,"SELECT * FROM table WHERE adddate = '$adddate' OR addcontact = '$addcontact' OR adda= '$adda' OR addb = '$addb' OR addincome = '$addincome' OR addpayment = '$addpayment' OR addsubbie = '$addsubbie' OR addcust = '$addcust'");
$sql = "INSERT INTO table (adddate, addcontact, adda, addb, addincome, addpayment, addsubbie, addcust) VALUES ('$adddate', '$addcontact', '$adda', '$addb', '$addincome', '$addpayment', '$addsubbie', '$addcust')";
if (!mysql_query($connect,$sql)) {
die('Error: ' . mysql_error($connect));
}
echo "1 record added";
mysql_close($connect);
<?php
function so56917978_upload_callback() {
//Register variables
$adddate = $_POST['adddate'];
$addcontact = $_POST['addcontact'];
$adda = $_POST['adda'];
$addb = $_POST['addb'];
$addincome = $_POST['addincome'];
$addpayment = $_POST['adddate'];
$addsubbie = $_POST['addsubbie'];
$addcust = $POST['addcust'];
//connect with Database
$host_name = 'zzz.hosting-data.io';
$database = 'zzz';
$user_name = 'zysql_connect($host_name, $user_name, $password);
if(!$connect) {
die('Not Connected To Server');
}
//Connection to database
if(!mysql_select_db($connect, $database)) {
echo 'Database Not Selected';
}
$query = mysql_query($connect,"SELECT * FROM table WHERE adddate = '$adddate' OR addcontact = '$addcontact' OR adda= '$adda' OR addb = '$addb' OR addincome = '$addincome' OR addpayment = '$addpayment' OR addsubbie = '$addsubbie' OR addcust = '$addcust'");
$sql = "INSERT INTO table (adddate, addcontact, adda, addb, addincome, addpayment, addsubbie, addcust) VALUES ('$adddate', '$addcontact', '$adda', '$addb', '$addincome', '$addpayment', '$addsubbie', '$addcust')";
if (!mysql_query($connect,$sql)) {
die('Error: ' . mysql_error($connect));
}
echo "1 record added";
mysql_close($connect);
}
You forgot the closing }
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
Parse error: syntax error, unexpected T_VARIABLE in Z:\home\ser.ser\www\sign_up.php on line 19
Also I have error with $q = mysql_query("SELECT * FROM users WHERE (login="$login")");
Help, please.
<?php
include 'mysql_connect.php';
$login = $_POST['login'];
$password = $_POST['password'];
$b_arr['b_dd'] = $_POST['B_DD'];
$b_arr['b_mm'] = $_POST['B_MM'];
$b_arr['b_yy'] = $_POST['B_YY'];
$b_date = $b_arr['b_yy'].$$b_arr['b_mm'].$b_arr['b_dd'];
if (!isUserExist($login)) {
reg($login, $password, $b_date);
} else {
echo 'This user is exist !';
}
function reg($login, $password, $b_date) {
// NEXT LINE ERROR
$query = mysql_query("INSERT INTO users VALUES ("$login", "$password", "$b_date")");
// END ERROR
}
function isUserExist($login) {
$q = mysql_query("SELECT * FROM users WHERE (login="$login")");
$result = mysql_fetch_array($q);
if ($result) {
return true;
}
}
?>
Warning: this code is dangerous. Please read about SQL Injection and why your code is extremely problematic. In short, anything that's put into the database must be sanitized.
Now, more to your question:
You aren't handling strings correctly. If you wish to use this dangerous method of querying, you need to concatenate your values into a string. To add variables to a string you use the . operator. So, to fix this line you would need to use something like:
$qry_str = "INSERT INTO users VALUES ('" . $login . "', '" . $password . "', '" . $b_date . "')";
$query = mysql_query($qry_str);
Note: I broke it into two lines for better readability and your isUserExist() function has the same issue.
You have errors in concatenations.
Please look at here: https://www.diffnow.com/?report=jgv1m
<?php
include 'mysql_connect.php';
$login = $_POST['login'];
$password = $_POST['password'];
$b_arr['b_dd'] = $_POST['B_DD'];
$b_arr['b_mm'] = $_POST['B_MM'];
$b_arr['b_yy'] = $_POST['B_YY'];
$b_date = $b_arr['b_yy'].$$b_arr['b_mm'].$b_arr['b_dd'];
if (!isUserExist($login)) {
reg($login, $password, $b_date);
} else {
echo 'This user is exist !';
}
function reg($login, $password, $b_date) {
// NEXT LINE ERROR
$query = mysql_query("INSERT INTO users VALUES ('".$login."', '".$password."', '".$b_date."')");
// END ERROR
}
function isUserExist($login) {
$q = mysql_query("SELECT * FROM users WHERE (login='".$login."')");
$result = mysql_fetch_array($q);
if ($result) {
return true;
}
}
?>
I am using this same code `
php $postId = 41;
<!-- hidden items and variables. Elements that will not be revealed !-->
<span id="gameLength"><?php
// MySQL connect configuration
$dbname="my_db";
$host="localhost";
$user="guessthe";
$dbh=mysql_connect ($host,$user,"correctPassword?") or die ('I cannot connect to the database because: ' . mysql_error(). '');
mysql_select_db ("$dbname") or die('I cannot select the database because: ' . mysql_error());
$sql="SELECT * FROM games WHERE postId = $postId";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$gameId = $rows['id'];
$game100s = $rows['game100s'];
$gamesPlayedAllTime = $rows['gamesPlayed'];
$gamesPointsAllTime = $rows['gameScore'];
$gameLength = $rows['gameLength']; // get number of questions
$gameScore = $rows['gameScore'];
$gameType = $rows['gameType'];
$gametitle = $rows['gameSubTitle'];
echo $gameLength;
There is a value in the gameLength row! I can't get this code to pull any of the rows! Any idea what i'm doing wrong?
You're using MySQL, which is depcirated - and will be phased out. You should use MySQLi or PDO instead. Also, your $postId is defined outside a PHP-tag? Might just be a copy/paste mistake? Anyway, you can try the code below, which is in MySQLi:
<?php
$postId = 41;
?>
<!-- hidden items and variables. Elements that will not be revealed !-->
<span id="gameLength"><?php
// MySQL connect configuration
$dbname = "my_db";
$host = "localhost";
$user = "guessthe";
// Connecting to the database
$mysqli = new mysqli($host, $user, "correctPassword?", $dbname);
if ($mysqli->connect_errno) {
// If we are here, the connection failed
echo "Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
}
$sql ="SELECT * FROM games WHERE postId = $postId";
if ($result = $mysqli->query($sql)) {
// If the query was sucsessfull, we can get the rows
while ($row = $result->fetch_assoc()) {
$gameId = $row['id'];
$game100s = $row['game100s'];
$gamesPlayedAllTime = $row['gamesPlayed'];
$gamesPointsAllTime = $row['gameScore'];
$gameLength = $row['gameLength']; // get number of questions
$gameScore = $row['gameScore'];
$gameType = $row['gameType'];
$gametitle = $row['gameSubTitle'];
}
} else {
// If the query failed, do something here
}
echo $gameLength;
?>
I see some people commenting that you need to put the $postId variable inside quotes in the query, but when using double-quotes (") variables will be posted, so it's not really needed. Also note that things are case-sensitive, so if your results doesn't show, check for spelling-mistakes.
There are many errors in your code
Try this...
<?php
$postId = 41;
?>
<!-- hidden items and variables. Elements that will not be revealed !-->
<span id="gameLength">
<?php
// MySQL connect configuration
$host = "localhost";
$dbname = "my_db";
$user = "username";
$password = "password";
$dbh = mysql_connect ($host,$user,$password) or die ('I cannot connect to the database because: ' . mysql_error() . '');
mysql_select_db($dbname, $dbh) or die('I cannot select the database because: ' . mysql_error());
$sql = "SELECT * FROM games WHERE postId='$postId'";
$result = mysql_query($sql);
while($rows = mysql_fetch_array($result)){
$gameId = $rows['id'];
$game100s = $rows['game100s'];
$gamesPlayedAllTime = $rows['gamesPlayed'];
$gamesPointsAllTime = $rows['gameScore'];
$gameLength = $rows['gameLength']; // get number of questions
$gameScore = $rows['gameScore'];
$gameType = $rows['gameType'];
$gametitle = $rows['gameSubTitle'];
echo $gameLength;
}
?>
You need to fix this is your code and that should fix the error.
$sql="SELECT * FROM games WHERE postId ='".$postId."' ";
If you want all the records you can use a while loop. Here is some pseudo code.
while($row = mysql_fect_assoc($query)){
echo $row["THE THING YOU WANT"];
...
}
This question already has answers here:
MYSQL PHP No Database Selected - Can't Find Error
(2 answers)
Closed 7 years ago.
Here is my php code
<?php
error_reporting(E_ALL);
class ll{
function ll(){
include "sql.php";
}
function getUser($ID) {
$sql="select * FROM users where ID=$ID";
$res = mysql_query($sql) or die (mysql_error());
$obj = mysql_fetch_object($res);
return $obj;
}
function setOfflineALL() {
$sql="update users set online=0";
mysql_query($sql);
}
}
$services = new ll();
$services->setOfflineALL(); // THIS WORKS !
$services->getUser(1); // THIS GIVES error: No database selected
?>
and sql.php is:
<?php $hostname_con1 = "localhost";
$database_con1 = "db1";
$username_con1 = "root";
$password_con1 = "mypass";
$con1 = mysql_connect($hostname_con1, $username_con1, $password_con1) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_con1);
mysql_set_charset('utf8',$con1);
?>
sql.php is correct (no error)
setOfflineALL works fine (no error)
getUser raises "no database selected error !", however I do select one with mysql_select_db
I do use php5.4
Any clue ?
I also tried:
function getUser($ID) {
$hostname_con1 = "localhost";
$database_con1 = "db1";
$username_con1 = "root";
$password_con1 = "mypass";
$con1 = mysql_connect($hostname_con1, $username_con1, $password_con1) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_con1);
mysql_set_charset('utf8',$con1);
$sql="select * FROM users where ID=$ID";
$res = mysql_query($sql) or die (mysql_error());
$obj = mysql_fetch_object($res);
return $obj;
}
And still have error:
No database selected
You should try this:
mysql_select_db($database_con1, $con1);
And you should give your database variable to your query too:
mysql_query("query", $con1);
And I strongly advise you not to use mysql but use PDO or mysqli
PDO
Mysqli
Actually, I connected to 3 databases in different server, but I got problem in mysqli_query.
sometimes it worked fluently without any error, but sometimes it showed "PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result"..
I don't know what happen, it ocurred sometimes.
here is my connection :
connection.php
<?php
$DatabaseName1 = "db_name1";
$DbHostName1 = "host1";
$DbUserName1 = "username";
$DbPassWord1 = "password";
$DatabaseName2q = "db_name2";
$DbHostName2 = "host2";
$DbUserName2 = "username2";
$DbPassWord2 = "password2";
$DatabaseName3q = "db_name3";
$DbHostName3 = "host3";
$DbUserName3 = "username3";
$DbPassWord3 = "password3";
$mysqli1 = mysqli_connect("$DbHostName1", "$DbUserName1", "$DbPassWord1", "$DatabaseName1");
$mysqli2 = mysqli_connect("$DbHostName2", "$DbUserName2", "$DbPassWord2", "$DatabaseName2q");
$mysqli3 = mysqli_connect("$DbHostName3", "$DbUserName3", "$DbPassWord3", "$DatabaseName3q");
$server[1] = $mysqli1;
$server[2] = $mysqli2;
$server[3] = $mysqli3;
$count_db = 3;
if(!$mysqli1){
echo "error to connect server 1st";
die();
}
if(!$mysqli2){
echo "error to connect server 2nd";
die();
}
if(!$mysqli3){
echo "error to connect server 3rd";
die();
}
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
task1.php
<?php
include("connection.php")
for($i=1;$i<=$count_db;$i++){
$conn = $server[$i];
$array = mysqli_fetch_array(mysqli_query($conn,"select * from `table`"));
$task = $array["field"];
}
?>
anyone guys can help?
Do it like this
for($i=1;$i<=$count_db;$i++){
$conn = $server[$i];
$resultset = mysqli_query($conn,"select * from `table`")
if(!$resultset){
//do something
continue; //if you don't want to break it
}
$array = mysqli_fetch_array($resultset);
$task = $array["field"];
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
In db.php I have:
<?php
class connect {
private $host = "localhost";
private $user = "root";
private $pass = "";
private $database = "databasename";
private $connect = null;
function connect() {
$this->connect = mysql_connect($this->host, $this->user, $this->pass) or die("Can't connect database");
mysql_select_db($this->database, $this->connect);
}
function getData() {
$data = array();
$sql = 'Select * From test';
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)) {
$data[] = array($row['id'], $row['name']);
}
return $data;
}
}
?>
In index.php I have:
<?php
include 'db.php';
$connect = new connect();
$connect->connect();
$data = $connect->getData();
$str = '';
foreach ($data as $dt) {
$str .= $dt[1];
}
echo $str;
?>
I am getting the following error:
=> error: <b>Warning</b>: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource from db.php.
What am I doing wrong?
Try to find what is the error:
function getData() {
$data = array();
$sql = 'Select * From test';
$query = mysql_query($sql);
if(!$query)
{
echo 'Error: ' . mysql_error(); /* Check what is the error and print it */
exit;
}
while($row = mysql_fetch_array($query)) { /* Better use fetch array instead */
$data[] = array($row['id'], $row['name']);
}
return $data;
}
That error is telling you that your query executed by $query = mysql_query($sql); is returning an error. It's not returning zero results, it's returning an error which suggests that your database named 'databasename' or table within that named 'test' doesn't exist.
It sounds like no results are returned from the query or a general query error, do the columns and table in the query exist and is your connection to the database all okay?