The expected behaviour of the code is
dynamically assign value to array (this is done a few times) and then print it
Code:
$PLLinks = array();
$sql245 = "SELECT `Username`, `Password`, `MacAddress` FROM `Users` WHERE `Username` LIKE '$guestUsername' AND `Password` LIKE '$guestPassword' AND `MacAddress` LIKE '$guestMacAddress'";
$result4 = mysql_query($sql245);
if (mysql_num_rows($result4) == 1) {
for ($i = 0; $i<count($Checknames); $i++)
{
if ($PLNames[$i] == 'Valid' && $PLChecked[$i] == 'Invalid'){
$result = mysql_query("SELECT `Link` FROM `Checksums` WHERE `Name` LIKE '$ValidNames[$i]' LIMIT 1");
$value = mysql_fetch_object($result);
$PLChecked[] = $value->{'Link'};
echo $value->{'Link'};
}
else if ($PLNames[$i] == 'Valid' && $PLChecked[$i] != 'Invalid') {
$PLLinks[] = 'noUpd';
}
else if ($PLNames[$i] != 'Valid') {
$PLLinks[] = 'Remove';
}
}
echo implode(',', $PLLinks);
// this should print "a,b,c" but instead it prints "abc"
}
If the solution is simple, please hint it only.
Thanks
Related
To explain my issue, here is a simple code at first:
public function sql($data) {
if (is_array($data)) {
$cells = $data['cells'];
$from = $data['from'];
$where = $data['where'];
$joins = $data['joins'];
$order_by = $data['order_by'];
$o_type = $data['order_by_type'];
$limit = $data['limit'];
/*****************************/
if ($cells == '') { $cells = "*"; }
if ($where != '') { $where = "where ".$where; }
if ($oredr_by != '') { $order_by = "order by ".$order_by." ".$o_type; }
if ($limit != '') { $limit = "limit ".$limit; }
//
$sql = "select ".$cells." from ".$from." ".$joins." ".$where." ".$order_by." ".$limit;
$run = mysqli_query($_SESSION['con'], $sql);
}else{
$run = mysqli_query($_SESSION['con'], $data);
}
}
When I start using this method, I pass a multidimensional array as a parameter, like this:
$sql = $class->sql([ "from" => "table", "order_by" => "id", "order_by_type" => "asc" ]);
/* This will generate and run this query: select * from table order by id asc */
// Notice that I've only used 3 keys, not the all above.
In Apache server, it works OK perfectly when I just use some of the keys of array, but in XAMPP it doesn't because it says that I have to pass all the parameters (cells, from, where, joins, ...) even if they are empty.
Please help me to resolve this, and thanks.
You can use isset to check if an array key is present, then get it's value like.
public function sql($data) {
if (is_array($data)) {
$cells = '';
if(isset($data['cells']) {
$cells = $data['cells'];
}
....
/*****************************/
if ($cells == '') { $cells = "*"; }
if ($where != '') { $where = "where ".$where; }
if ($oredrby != '') { $orderby = "order by ".$orderby." ".$od_type; }
if ($limit != '') { $limit = "limit ".$limit; }
$sql = "select ".$cells." from ".$table." ".$joins." ".$where." ".$orderby." ".$limit;
$run = mysqli_query($_SESSION['con'], $sql);
}else{
$run = mysqli_query($_SESSION['con'], $data);
}
}
Or simply just do error_reporting(1) before calling this function or in your index.php.
The problem is this.
$arr = ["a"];
echo $arr["b"];
You will get an error notice.
Notice: Undefined index: b
If you want to avoid this use it in this way.
$arr = ["a"];
$arr = ["b"] = "";
echo $arr["b"];
Change $from to $table, you have not $table variable
$from = $data['from'];
to
$table = $data['from'];
Also you have spelling mistake biggest spelling mistake which is very difficult to find.orderby and oredrby
The below works as long as the two fields are selected. If neither are selected it works, however my issue is when only one of the fields is selected, it doesn't work. It throws the unbound parameters issue.
I've tried setting a false value of 0 to both of the variables, however that won't work because then the query would be select from where = 0.
Ideas?
public static function searchProfile($status, $fundamt)
{
$database = DatabaseFactory::getFactory()->getConnection();
$sql = "SELECT profile_id, profile_name, profile_url, finance_fundingtype, finance_equitypercent, finance_loanrate, finance_loanlength, finance_fundingamount, info_tradingstatus, info_elevatorpitch, info_patentable, info_industry, info_industry1, info_industry2, info_industry3, info_industry4, seeker_logo_url FROM profile_seeker WHERE profile_status = '1' ";
if ($status) {
$sql .= "AND info_tradingstatus IN (:status) ";
}
if ($fundamt) {
$sql .= "AND finance_fundingamount <= :fundamt ";
}
$query = $database->prepare($sql);
$query->execute(array(':status' => $status, ':fundamt' => $fundamt));
$profiles = array();
$profiles[$profile->profile_id] = new stdClass();
$profiles[$profile->profile_id]->profile_id = $profile->profile_id;
$profiles[$profile->profile_id]->profile_name = $profile->profile_name;
$profiles[$profile->profile_id]->profile_url = $profile->profile_url;
$profiles[$profile->profile_id]->finance_fundingtype = $profile->finance_fundingtype;
$profiles[$profile->profile_id]->finance_equitypercent = $profile->finance_equitypercent;
$profiles[$profile->profile_id]->finance_loanrate = $profile->finance_loanrate;
$profiles[$profile->profile_id]->finance_loanlength = $profile->finance_loanlength;
$profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
$profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;
$profiles[$profile->profile_id]->info_elevatorpitch = $profile->info_elevatorpitch;
$profiles[$profile->profile_id]->info_patentable = $profile->info_patentable;
$profiles[$profile->profile_id]->info_industry = $profile->info_industry;
$profiles[$profile->profile_id]->info_industry1 = $profile->info_industry1;
$profiles[$profile->profile_id]->info_industry2 = $profile->info_industry2;
$profiles[$profile->profile_id]->info_industry3 = $profile->info_industry3;
$profiles[$profile->profile_id]->info_industry4 = $profile->info_industry4;
$profiles[$profile->profile_id]->seeker_logo_url = $profile->seeker_logo_url;
}
return $profiles;
You could try to check if the variables are set and that they hold a value that is not 0 and this has a string length longer than 0:
if (isset($status) && $status !== 0 && strlen($status) > 0) {
$sql .= "AND info_tradingstatus IN (:status) ";
}
if (isset($fundamt) && $fundamt!== 0 && strlen($fundamt) > 0) {
$sql .= "AND finance_fundingamount <= :fundamt ";
}
You could also try to bind the parameters manually:
$query = $database->prepare($sql);
if (isset($status) && $status !== 0 && strlen($status) > 0) {
$query ->bindParam(':status',$status);
}
if (isset($fundamt) && $status !== 0 && strlen($fundamt) > 0) {
$query ->bindParam(':fundamt',$fundamt);
}
$query->execute();
Your binding error comes because you forgot to use the same if exists statement on your bindings.
Change this...
if ($status) {
$sql .= "AND info_tradingstatus IN (:status) ";
}
if ($fundamt) {
$sql .= "AND finance_fundingamount <= :fundamt ";
}
and
$profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
$profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;
To this...
if (isset($status) && $status != '') {
$sql .= "AND info_tradingstatus IN (:status) ";
}
if (isste($fundamt) && $fundamt != '') {
$sql .= "AND finance_fundingamount <= :fundamt ";
}
and
if (isset($status) && $status != '') {
$profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
}
if (isste($fundamt) && $fundamt != '') {
$profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;
}
This will stop the binding if one or both of the text boxes are empty.
Edit: I've changed the query to this version but I'm still not getting any
results even when I should be.
if (isset($_POST['schbttn'])) {
$breed1 = $_POST['schbreed1'];
$breed2 = $_POST['schbreed2'];
$sex = $_POST['schsex'];
$colour = $_POST['schcolour'];
$age = $_POST['schage'];
include ('inc/dbconn.php');
// If breed2 NULL, search with this query
if ($breed2 == "NULL") {
$search = mysqli_query($dbconn, "SELECT * FROM `lstfnd` WHERE `doglf_stat` = 'Lost' AND `doglf_breed1` = '$breed1' AND `doglf_breed2` IS NULL AND `doglf_sex` = '$sex' AND `doglf_colour` = '$colour' AND `doglf_age` = '$age'");
// Else search with this query
} else {
$search = mysqli_query($dbconn, "SELECT * FROM `lstfnd` WHERE `doglf_stat` = 'Lost' AND `doglf_breed1` = '$breed1' AND `doglf_breed2` = '$breed2' AND `doglf_sex` = '$sex' AND `doglf_colour` = '$colour' AND `doglf_age` = '$age'");
}
$schrow = mysqli_fetch_assoc($search);
}
I'm trying to create a simple search function where a user can search by multiple fields.
I've taken the entries of each field
$breed1 = $_POST['breed1'];
$breed2 = $_POST['breed2'];
$sex = $_POST['sex'];
$colour = $_POST['colour'];
$age = $_POST['age'];
and built the query through if loops
$query = "SELECT * FROM `table` WHERE `stat` = 'Lost'";
// If breed1 is not ALL, add to search
if ($breed1 != "ALL") {
$query = $query." AND `breed1` = '$breed1'";
}
// If breed2 is not ALL, add to search
if ($breed2 != "ALL") {
if ($breed2 == "NULL") {
$query = $query." AND `breed2` IS NULL";
} else {
$query = $query." AND `breed2` = '$breed2'";
}
}
// If sex is not ALL, add to search
if ($sex != "ALL") {
$query = $query." AND `sex` = '$sex'";
}
// If colour is not ALL, add to search
if ($colour != "ALL") {
$query = $query." AND `colour` = '$colour'";
}
// If age is not ALL, add to search
if ($age != "ALL") {
$query = $query." AND `age` = '$age'";
}
$query = $query.";";
and placed the query in a PHP variable to use when running the query.
include ('inc/dbconn.php');
$search = mysqli_query($dbconn, "'.$query.'");
$schrow = mysqli_fetch_assoc($search);
However, when I try to display the results of the search, I get an error code.
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given...
So is what I am attempting possible to accomplish using this method? And if not, any suggestions for alternative methods?
change this line
$search = mysqli_query($dbconn, "'.$query.'");
to
$search = mysqli_query($dbconn, $query);
$query is variable, do not use that as string.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is the Code, from where error is coming :)
I hope that I'll get my answer quick!!!
function getSubs($id, $type, $start = null) {
if($type == 0) {
if(is_numeric($start)) {
if($start == 0) {
$start = '';
} else {
$start = 'AND `relations`.`id` < \''.$this->db->real_escape_string($start).'\'';
}
$limit = 'LIMIT '.($this->s_per_page + 1);
}
$query = sprintf("SELECT * FROM `relations`, `wp_users` WHERE `relations`.`subscriber` = '%s' AND `relations`.`leader` = `wp_users`.`ID` $start ORDER BY `relations`.`id` DESC $limit", $this->db->real_escape_string($id));
} else {
if(is_numeric($start)) {
if($start == 0) {
$start = '';
} else {
$start = 'AND `relations`.`id` < \''.$this->db->real_escape_string($start).'\'';
}
$limit = 'LIMIT '.($this->s_per_page + 1);
}
$query = sprintf("SELECT * FROM `relations`, `wp_users` WHERE `relations`.`leader` = '%s' AND `relations`.`subscriber` = `wp_users`.`ID` $start ORDER BY `relations`.`id` DESC $limit", $this->db->real_escape_string($id));
}
$result = $this->db->query($query);
while($row = $result->fetch_assoc()) {
$array [] = $row;
}
return array($array, $total = $result->num_rows);
}
function getActions($id, $likes = null, $type = null) {
global $LNG;
if($type == 1) {
$verify = $this->verifyLike($id);
$result = $this->db->query(sprintf("SELECT * FROM `messages`, `wp_users` WHERE `id` = '%s' AND `messages`.`uid` = `wp_users`.`ID`", $this->db->real_escape_string($id)));
if($result->num_rows == 0) {
return $LNG['like_message_not_exist'];
}
if(!$verify) {
$stmt = $this->db->prepare("INSERT INTO `likes` (`post`, `by`) VALUES ('{$this->db->real_escape_string($id)}', '{$this->db->real_escape_string($this->id)}')");
$stmt->execute();
$affected = $stmt->affected_rows;
$stmt->close();
if($affected) {
$this->db->query("UPDATE `messages` SET `likes` = `likes` + 1, `time` = `time` WHERE id = '{$this->db->real_escape_string($id)}'");
$user = $result->fetch_assoc();
$insertNotification = $this->db->query(sprintf("INSERT INTO `notifications` (`from`, `to`, `parent`, `type`, `read`) VALUES ('%s', '%s', '%s', '2', '0')", $this->db->real_escape_string($this->id), $user['uid'], $user['id']));
if($this->email_like) {
if($user['email_like'] && ($this->id !== $user['ID'])) {
sendMail($user['user_email'], sprintf($LNG['ttl_like_email'], $this->username), sprintf($LNG['like_email'], realName($user['user_login'], $user['first_name'], $user['last_name']), $this->url.'/index.php?a=profile&u='.$this->username, $this->username, $this->url.'/index.php?a=post&m='.$id, $this->title, $this->url.'/index.php?a=settings&b=notifications'), $this->email);
}
}
}
} else {
$x = 'already_liked';
}
} elseif($type == 2) {
$verify = $this->verifyLike($id);
$result = $this->db->query(sprintf("SELECT `id` FROM `messages` WHERE `id` = '%s'", $this->db->real_escape_string($id)));
if($result->num_rows == 0) {
return $LNG['like_message_not_exist'];
}
if($verify) {
$stmt = $this->db->prepare("DELETE FROM `likes` WHERE `post` = '{$this->db->real_escape_string($id)}' AND `by` = '{$this->db->real_escape_string($this->id)}'");
$stmt->execute();
$affected = $stmt->affected_rows;
$stmt->close();
if($affected) {
$this->db->query("UPDATE `messages` SET `likes` = `likes` - 1, `time` = `time` WHERE id = '{$this->db->real_escape_string($id)}'");
$this->db->query("DELETE FROM `notifications` WHERE `parent` = '{$this->db->real_escape_string($id)}' AND `type` = '2' AND `from` = '{$this->db->real_escape_string($this->id)}'");
}
} else {
$x = 'already_disliked';
}
}
if($likes == null) {
$query = sprintf("SELECT `likes` FROM `messages` WHERE `id` = '%s'", $this->db->real_escape_string($id));
$result = $this->db->query($query);
$get = $result->fetch_row();
$likes = $get[0];
}
$verify = $this->verifyLike($id);
if($verify) {
$state = $LNG['dislike'];
$y = 2;
} else {
$state = $LNG['like'];
$y = 1;
}
if($this->l_per_post) {
$query = sprintf("SELECT * FROM `likes`,`wp_users` WHERE `post` = '%s' and `likes`.`by` = `wp_users`.`ID` ORDER BY `likes`.`id` DESC LIMIT %s", $this->db->real_escape_string($id), $this->db->real_escape_string($this->l_per_post));
$result = $this->db->query($query);
while($row = $result->fetch_assoc()) {
$array[] = $row;
}
$people = '';
foreach($array as $row) {
$people .= '<img src="'.$this->url.'/thumb.php?src='.$row['image'].'&w=25&h=25&t=a" title="'.realName($row['user_login'], $row['first_name'], $row['last_name']).' '.$LNG['liked_this'].'" /> ';
}
}
$actions = '<a onclick="doLike('.$id.', '.$y.')" id="doLike'.$id.'">'.$state.'</a> - <a onclick="focus_form('.$id.')">'.$LNG['comment'].'</a> - <a onclick="share('.$id.')">'.$LNG['share'].'</a> <div class="like_btn" id="like_btn'.$id.'"> '.$people.$likes.'</div>';
if(empty($this->id)) {
$actions = ''.$LNG['login_to_lcs'].' <div class="like_btn"> '.$people.$likes.'</div>';
}
if(isset($x)) {
return $LNG["$x"].' <div class="like_btn"> '.$likes.'</div>';
}
return $actions;
}
The notice "Trying to get property of non-object" couldn't be more clear. You are trying to read a property of something that is not an object.
For example:
$object = false;
echo $object->property; // Notice: Trying to get property of non-object
Probably it is happening because you are assuming every SQL query you are doing is succeeding. But if a SQL query has errors and fails to run, the call returns false instead of a results object.
Sinse you didn't even say what line you are having this problem it's not worth the effort of helping you any further here. Check if your queries are suceeding before trying to use the result and read the MySQL error message from $this->db->error to figure out whats going wrong.
Here's my problem:
$q = 'SELECT * FROM s_stats WHERE srv_id='.$sid.' ORDER BY date DESC LIMIT 5';
$result = mysql_query($q) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
if ($row[percent] == null) // don't work
$procent[] = 1;
else
$procent[] = $row[percent];
}
$procent[] = implode('-', $procent);
Try: if ($row["percent"] == null || $row["percent"] == "")
try
if ($row[percent] === null)
When using the non-strict == operator, 0 == null and '' == null will evaluate to true as well, which is probably not desirable.
$q = 'SELECT * FROM s_stats WHERE srv_id='.$sid.' ORDER BY date DESC LIMIT 5';
$result = mysql_query($q) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo '*', $row['percent'], '*<br/>';
if (!isset($row["percent"]))
$procent[] = 1;
else
$procent[] = $row[percent];
}
$procent[] = implode('-', $procent);
and print:
12
4
66
Maybe if (! isset($row['percent'])) instead of if ($row['percent'] == null)