-1

I am running a PHP script, and keep getting errors like:

Problem 1

 204   if(isset($campaign)){
 205      if(strlen($search)>0){
 206         $campaign_sql_string="and campaign_id='$campaign'";
 207    } else {
 208       $campaign_sql_string="where campaign_id='$campaign'";
 209    }
 210    }   else {
 211    $campaign_sql_string="";
 212    }
 213    if(!empty($starting) > 0 ){
 214    $starting2=$starting-1;
 215    $starting_string="$starting2,";
 216    }
 217    $sql_count="select count(*) as count from {$_SESSION['table']} $search $campaign_sql_string";
 218    $sql="select * from {$_SESSION['table']} $search $campaign_sql_string order by $column desc limit $starting_string {$_SESSION['limit']}";

Error:

Notice: Undefined variable: starting_string PAGE in LINE: 218


Problem 2
 13     $conn = mysqli_connect($servername, $username, $password, $dbname);
 14     if (!$conn) {
 15     die("Connection failed: " . mysqli_connect_error());
 16     }
 17     $sql = "SELECT * FROM ayarlar";
 18       $result = mysqli_query($conn, $sql);
 19       if ($result->num_rows > 0) {
 20         while($row = $result->fetch_assoc()) {
 24         $mevcut = $row["AppVersiyon"];
 22         }
 23       }

Error:

Warning: mysqli_query(): Couldn't fetch mysqli in 18

and

Notice: Trying to get property of non-object in 19


Problem 3
if( (isset($_REQUEST['starting']) && $_REQUEST['starting']<=0) || $ignore_starting) {

Error:

Notice: Undefined variable: ignore_starting

What do these errors mean?
What do I need to do to fix them?

chris85
  • 23,846
  • 7
  • 34
  • 51
John Lock
  • 9
  • 3
  • 2
    Not sure if this correct: `if(!empty($starting) > 0 ){` (line 213). You can either check for `empty` or for `>0` but not for both the way you are doing. – Maximus2012 Jun 02 '16 at 16:31
  • It is possible that the control is not going to the line where you are setting your `$starting_string` variable hence the error in the first case. – Maximus2012 Jun 02 '16 at 16:32
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST` or `$_GET` data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Jun 02 '16 at 17:14

2 Answers2

1

The Undefined variable notices from problems 1 and 3 simply mean you are attempting to use a variable without first defining its value anywhere. This is consistent with your posted code for 3. You should do a search in the rest of your code to find where $ignore_starting is used in order to troubleshoot why it's not getting set as you expect.

As for 1, if(!empty($starting) > 0 ) is invalid, or at least not clean since you're implicitly attempting to cast false as 0. empty returns true/false, so it should simply be if(!empty($starting)) else then set the value euqal to the empty string '' to avoid the notice later on. Moreover, since it's a number, I like to check if it's numeric and then if greater than zero like you tried.

 213    if( !empty($starting) && is_numeric($starting) && ($starting > 0) ){
 214    $starting2=$starting-1;
 215    $starting_string="$starting2,";
 216    }
        else $starting_string="";

The problems from 2 are that your SQL query isn't working. Trying to get property of non-object means you're trying to fetch a result from a value of FALSE set by an error from $result = mysqli_query($conn, $sql); which is indicated by the warning Warning: mysqli_query(): Couldn't fetch mysqli

In order to get more helpful information about the query, you should check the specific error:

 17     $sql = "SELECT * FROM ayarlar";
 18       $result = mysqli_query($conn, $sql);

          if ($result === false)
             printf("Error: %s\n", mysqli_error($conn));

 19       if ($result->num_rows > 0) {
 20         while($row = $result->fetch_assoc()) {
 24         $mevcut = $row["AppVersiyon"];
 22         }
 23       }
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
0
  1. Notice: Undefined variable means your variable is not defined when php tries to execute it.

  2. Notice: Trying to get property of non-object in 19 - this happens because of the issue on line 18 - not sure why it complains in line 18

  3. ignore_starting- again you get a notice because it is probably not declared with any value before you call it in this line.

Danny Ogen
  • 279
  • 1
  • 5