0

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.

4
  • This will be difference in the error_reporting level of your php settings. In your apache it will not be that strict as it is in your xampp. Commented Jul 20, 2016 at 10:54
  • OK that's clear like sunrise. But could you guide me how to solve it if my host in internet is XAMPP? How to avoid this issue? You know, a huge class, a lot of methods. Commented Jul 20, 2016 at 10:59
  • @nael_d : Change $from to $table, you have not $table variable Commented Jul 20, 2016 at 11:00
  • Changed. It's my fault from speed typing. This is not my issue at all :) Commented Jul 20, 2016 at 11:07

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That works with me. Thanks !! :-) But Does it work if I used this way $cells = $data['cells'] = "*"; ? I mean: Will they accept my parameters puts the different values if I didn't pass anything?
no problem.. what you are asking will simply assign "*" to $cells and $data['cells'].. You could look into using a ternary operator for that.
0

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"];

Comments

0

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

3 Comments

Done. Sorry I didn't mean it. It's from speed typing.
@nael_d : Also you have spelling mistake biggest spelling mistake which is very difficult to find.orderby and oredrby
I know I'm sorry, I've written them immediately just to explain my issue. The original code is very similar to this but corrected and verified well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.