PHP5.4 code Runs on server but not on local - php

I got a php code (index.php) from a friend, something like this:
<?
if (!isset($_SESSION['stdtype']))
{
$datas = mysql_query($query);
$data = mysql_fetch_assoc($datas);
if ($data) {
?><li>Acc Letter</li><?
}
else {
?><li>Acc Letter</li><?
}
}
$datas = mysql_query($query2);
$data = mysql_fetch_assoc($datas);
if ($data)code
{
?>
<li>Upload</li>
<?php
}
$datas = mysql_query($query3);
$data = mysql_fetch_assoc($datas);
if ($data) {
if ($data['rank'] <> 6 && $data['rank'] <> 0) {
?>
the error is:
::1:56706 [500]: /index.php - syntax error, unexpected '}' in /home/asd/phpT_T/index.php on line 333
line 333 from above snippet is:
} <-- this one
$datas = mysql_query($query3);
is there anything on php.ini that I should add to make the exact copied code from server works on my computer? because my computer already has the same php version (5.4.45)

go up a couple of lines. You will find this:
if ($data)code
{
this should be
if ($data)
{
Also replace <? with <?php everywhere in your code
for your code to work

Related

How to fix Array overwritten after foreach loop php

I am having issue data Array overwritten in foreach loop. Result I am getting like this wrongRight together .Right answer is showing but also wrong for example ZucchiniCauliflower.Please help
CODE 1
$data = array();
$dis_07= null;
$dis_03 = null;
if (is_array($row)) {
foreach ($row as $value) {
$gccat_id = $value->gccat_id;
$ccat_id = $value->ccat_id;
$cat = $value->cat_id;
if (isset($gccat_id) && $gccat_id == $id) {
$dis_07 = $value->category;
$dis_02 = $value->child_id;
}
if (isset($ccat_id) && $ccat_id == $id) {
$dis_03 = $value->category;
$dis_02 = $value->parent_id;
}
}
}
$data['Dis_03'] = $dis_03;
$data['Dis_07'] = $dis_07;
if (isset($data['Dis_03'])) {
echo $data['Dis_03'];
}
if (isset($data['Dis_07'])) {
echo $data['Dis_07'];
}
First I tried this way But In one I was getting right in second link I am getting right So Tried the code previous one .In the prvious I am getting correct and wrong one together EExample ZucchiniCauliflower
CODE 2
if (isset($id)) {
$db = Database::newInstance();
$data = array();
$data['cat_status'] = 1;
$sql = "SELECT * FROM category WHERE cat_status=:cat_status ";
$row = $db->read($sql,$data);
$data['id'] = $crypt->decryptId($id);
echo $data['id'];
$id=$data['id'];
if (is_array($row)) {
foreach ($row as $value) {
$gccat_id=$value->gccat_id;
$ccat_id = $value->ccat_id;
$cat = $value->cat_id;
if (isset($gccat_id) && $gccat_id == $id) {
$data['Dis_03']=$value->category;
}
if (isset($ccat_id) && $ccat_id == $id) {
$data['Dis_03'] = $value->category;
break;
}
}
}
}
--------------------------READ FROM HERE------------------------
Here is a link one when I click on this link
$id=$value11->gccat_id;
$title
I expected the output is
Home>Raspberry
Here is a link Second link when I click on this link
Here id is ($value11->gccat_id)
window.open('<?= BASEURL ?>ap/'+id,'_self');
I expected the output is
Home>Cauliflower
1. WHEN I Use the Code 2 (Added break in this condition
(isset($ccat_id) && $ccat_id == $id)) Then click on link second
it gives output Home>Cauliflower which I was expecting. It is
correct.
2. But this time as I added the break in (isset($ccat_id) && $ccat_id == $id). I click on link one It gives wrong output which I was not expecting. Home>Squash which is wrong.
In one link I was expecting
Home>Cauliflower
ERROR NOTE If I add Break; then link Second gives correct output but when I remove Break; then link one give correct. I wanted Both link should give correct output.
Problem was with cat_id,ccat_id ,gccat_id.
I provided 8 digit unique number with the following output,now I am getting the correct output.
function generateUniqueNumber() {
return sprintf('%08d', mt_rand(1, 99999999));
}

PHP Foreach with mysql_fetch_array server status

Im trying to create a cronjob script, which checks the status of a few servers.
Somehow it only uses the first ip in my table.
$aQ = db::aQuery("SELECT hostIp FROM `gh_server_hosts`");
$a = mysql_fetch_array($aQ);
foreach ($a as $value) {
$timeout = "10";
$ssh= #fsockopen("$value","22",$timeout);
if($ssh) {
$status = 1;
}
else {
$status = 0;
}
$time = time();
if(db::aQuery("UPDATE `gh_server_hosts` SET hostLastScanned='".$time."', hostLastStatus='".$status."' WHERE hostIp = '".$value."'")) {
echo "scanned";
} else {
echo "error";
}
}
You have to put mysql_fetch_array in loop to get all records like this:
$aQ = db::aQuery("SELECT hostIp FROM `gh_server_hosts`");
while ($value = mysql_fetch_assoc($aQ)) {
$timeout = "10";
$ssh= #fsockopen($value['hostIp'],"22",$timeout);
// Rest of your code
}
I have used mysql_fetch_assoc instead of mysql_fetch_array to limit the output array. Also, as mentioned by others mysql_* functions are deprecated so kindly update your code before running this code on PHP 5.5+...
mysql_fetch_assoc() will work instead of mysql_fetch_array() but do note about the deprecation warning with mysql. (Aside from the security concerns, your code will fail if PHP is upgraded to 7.0).

PHP Fatal Error Cannot Use String Offset As Array

I keep getting a fatal error when a script is ran on my php server,
This web app was built ten years ago and I am currently clearing errors so we can start updating it. Current version PHP 5.2.17
Fatal error: Cannot use string offset as an array in /home/user/public_html/admin/script.php on line 1418
This is the line the error is on,
$name = $d["#"]["_Name"];
This is the full function,
if (isset($b["_BORROWER"]["EMPLOYER"])) {
if (!isset($b["_BORROWER"]["EMPLOYER"][0])) {
$temp = $b["_BORROWER"]["EMPLOYER"];
unset($b["_BORROWER"]["EMPLOYER"]);
$b["_BORROWER"]["EMPLOYER"][0] = $temp;
}
foreach($b["_BORROWER"]["EMPLOYER"] as $c => $d) {
$pid = '0';
// Finish up.
$item["type"] = "Personal";
$name = $d["#"]["_Name"];
//check for files in other bureaus
$query = doquery("SELECT name,id FROM <<myitems>> WHERE cid='".$client["id"]."' AND type='Personal'");
$results = dorow($query);
if($results){
if(isset($results['name'])){
$temp = $results;
unset($results);
$results[0] = array($temp);
}
foreach($results as $c){
if(isset($c['name'])){
if($address == decrypt_string($c['name'])) {
$pid = $c['id'];
break;
};
}
}
}
Does anyone understand what is triggering this error and how to fix it?
You can use isset() to check the array value exists or not like,
$name = isset($d["#"]["_Name"]) ? $d["#"]["_Name"] : "";

i am getting unexpected 'function' (T_FUNCTION) error

it is very frustrating , i am getting error - >
Parse error: syntax error, unexpected 'function' (T_FUNCTION) in
C:\xampp\htdocs\chat\inc\chat.func.php on line 6
in the following code and unable to find any solution. and my php version is 5.5.1 so i don't any point of 'older' version.
here is the code
<?php
include( 'connect.inc.php')
function get_msg() {
$que = "SELECT sender , message FROM chat ";
$run = mysqli_query($conncetion,$que)
$messages = array();
while ($message = mysql_fetch_assoc($run)) {
$messages[] = array{'sender'=>$message['sender'],'message'=>$message['message']};
}
return $messages;
}
function send_msg($sender,$message) {
if( !empty($sender) && !empty($message)) {
$sender = mysql_real_escape_string($sender);
$message = mysql_real_escape_string($message);
$query = "INSERT INTO chat VALUES('','$sender','$message') ";
if($run = mysqli_query($con,$query)) {
retur n true;
} else {
return flase;
}
} else {
return flase;
}
}
?>
You're missing a semicolon from the end of your include line at the top. I saw this when I tried to edit your post and saw all of the code. Tried formatting it for you but someone else has submitted an edit.
#user4141363, There are couple of errors in your code.Please check below code & replace with your original code.
//Below Semi-column is not there in your code.
include_once( 'connect.inc.php');
// Semicolon is missing in below line so please add semicolon as below.
$run = mysqli_query($conncetion,$que);
//Array braces wrong in your code so replace this line in your code.
$messages[] = array('sender'=>$message['sender'],'message'=>$message['message']);
//Space between return true code so it's cause syntax error.
return true;
//Return false spell mistake in your code so replace below line.
return false;
I have edited your code in your question, please copy that code & check it your end.

A simple recursive function in PHP doesn't work

I'm trying to get a parent category for a given sub category using the following function in PHP.
require_once("Connection.php");
$flag=true;
function get_parent_id($cat_id, $parent_id)
{
if ($parent_id==0)
{
return($cat_id);
}
else if ($flag==true)
{
$data1=mysql_query("select parent_id from category where cat_id=" + $cat_id);
while($row = mysql_fetch_assoc($data1))
{
$parent_id=$row['parent_id'];
}
$flag = false;
}
else if ($flag==false)
{
$data2=mysql_query("select cat_id from category where cat_id=" + $parent_id);
while($row = mysql_fetch_assoc($data2)) //The warning comes from here.
{
$cat_id=$row['cat_id'];
}
$flag = true;
}
$cat_id = get_parent_id($cat_id, $parent_id);
return($cat_id);
}
}
echo get_parent_id($ed_id, $parent_id); //Call the above function.
It always prompts the following warning.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
result resource in C:\wamp\www\zoom\Category.php on line 492
Even though there is no error in SQL. The included file Connection.php also works fine on all other pages. I didn't understand at all why this happens.
Strings are concatenated with ., not +.
String Operators

Categories