Fatal Error: Cannot Use Object of Type MySQLi_Result

Fatal error: Cannot use object of type mysqli_result


Cannot use object of type mysqli_result as array

Use mysqli_fetch_assoc or mysqli_fetch_array to fetch a result row as an associative array.

$query = "SELECT 1";
$result = $mysqli->query($query);
$followingdata = $result->fetch_assoc()

or

$followingdata = $result->fetch_array(MYSQLI_ASSOC);

Fatal error: Cannot use object of type mysqli_result as array in C:\xampp\

Problem is you are trying to over-write your mysqli_result variable$location_result inside while() loop.

You need to take a separate array variable to get all the values.

Do like below (and please read comments inside code carefully ):-

 if(isset($_POST['list'])){ 
$location_type =$_POST['list'];

$arr = explode(' ',trim($location_type));

$listing=$arr[0];

$all_location ="select * from tbl_master_property where pg_address like '%$listing%'";

$all_location_result = [];//create an array variable
$location_result=$conn->query($all_location);
while($row=$location_result->fetch_assoc()){ // try not to mix oop way with procedural way, not a good practic
$all_location_result[] = $row['pg_address']; // assign values to array variable
}
echo "<pre>";print_r($all_location_result);// print array variable
}

Note:- Youe query code is wide-open for SQL INJECTION so try to use prepared statements

Reference:-

mysqli::prepare

PDO::prepare

Fatal error: Cannot use object of type mysqli_result as array

You're using the same variable $result for both the result of $stmt->get_result() and also the array of values that you're fetching. So on the second iteration of the loop, when it does

$row = $result->fetch_array()

$result no longer contains the mysqli result, it contains the array, and you can't call fetch_array() on an array. Use different variable names for them.

$output = array();
while ($row = $result->fetch_array()) {
$output[] = $row;
}
$objSmarty->assign("result", $output);

Also, $row is already an array, you probably don't want to nest it in another array before pushing it onto the $output array.

PHP: Uncaught Error Cannot use object of type mysqli_result as array

you should fecth a row (at least)

if (mysqli_connect_errno())   {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$result = mysqli_query($conn,
"SELECT sum(SumofNoOfProjects) as sum_projects, sum(SumofTotalBudgetValue) as sum_value
FROM `meed`
WHERE Countries = '$countries'");

while( $row=mysqli_fetch_array($result,MYSQLI_ASSOC);) {
echo json_encode([ $row['sum_projects'], $row['sum_value'] ] );
exit;
}

for the multiple countries

Asuming you $_POST['countries'] contains "'Egypt','Algerie'"
then you could use a query as

"SELECT sum(SumofNoOfProjects) as sum_projects, sum(SumofTotalBudgetValue) as sum_value 
FROM `meed`
WHERE Countries IN (" . $_POST['countries'] . ");"

Fatal error: Cannot use object of type mysqli_result as array in...on line 97

Your problem is right here:

$sql = sprintf ("INSERT INTO useradvert (ID, name2, color2, hobby2) VALUES (%d, '%s', '%s', '%s')",  (int)
// V- here
$result['ID'], // <- here
// ^- here
mysqli_real_escape_string($conn, $name2),
mysqli_real_escape_string($conn, $color2),
mysqli_real_escape_string($conn, $hobby2));

As the error message says, you can't access a mysqli_result as an array - you have to fetch() each row into an array first. Also, even if it were valid syntax I can't see where the value for ID would be coming from, since it's not one of the columns queried to produce the result.

You really should be using prepared statements for this. Aside from preventing SQL injection attacks, a prepared statement handles all the quoting and type conversions for the query parameters. Your first query (the select) would be written as

$query = "SELECT name, username, telno
FROM users
WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_array();

Your second would look like

$userid = $_POST['ID'];
$query = "INSERT INTO useradvert
(id, name2, color2, hobby2)
VALUES
(?, ?, ?, ?)";
$stmt = $conn->prepare($query);
$stmt->bind_params('isss', $userid, $name2, $color2, $hobby2);
$stmt->execute();

(Note that in both cases I have omitted any sort of error handling, which I prefer to leave as an exercise for the reader.)

As for primary keys, you can use whichever column (or set of columns) will uniquely identify the row and will not change during the lifetime of the row.

Cannot use object of type mysqli_result as array error

As its says: "Cannot use object of type mysqli_result as array error". You have to create an array from result.

while($result = mysqli_fetch_array($hasil)){
echo "nama : $result[nama] <br>
umur : $result[umur] <br>
kelamin : $result[kelamin] <br>";
}

Uncaught Error: Cannot use object of type mysqli_result as array

After running the query

$result = $con->query($sth);

$result is not an Array, it is usually a Resource or TRUE or FALSE. You can check your result for error like this

if (!$result){
// This is an error
}

If you want to check for number of rows you can do it like this

if (mysql_num_rows ($result) > 0){
// No or rows greater than 0
}

I strongly suggest to check out the PHP manual

http://php.net/manual/en/function.mysql-query.php

Or even better use mysqli

http://php.net/manual/en/book.mysqli.php

Which you should really be using by now...

How do classes help you manage large applications?

Encapsulation theory provides one objective reason why classes are better than having no classes at all.

The International Organisation for Standardization defines encapsulation as, 'The property that the information contained in an object is accessible only through interactions at the interfaces supported by the object.'

Thus, as some information is accessible via these interfaces, some information must be hidden and inaccessible within the object. The property such information exhibits is called information hiding, which Parnas defined by arguing that modules should be designed to hide both difficult decisions and decisions that are likely to change.

Note that word: change. Information hiding concerns potential events, such as the changing of difficult design decisions in the future.

Consider a class with two methods: method a() which is information hidden within the class, and method b() which is public and thus accessible directly by other classes.

There is a certain probability that a future change to method a() will require changes in methods in other classes. There is also a certain probability that a future change to method b() will require changes in methods in other classes. The probability that such ripple changes will occur for method a(), however, will usually be lower than that for method b() simply because method b() may be depended upon by more classes.

This reduced probability of ripple impacts is a key benefit of encapsulation.

Consider the maximum potential number of source code dependencies (MPE - the acronym is from graph theory) in any program. Extrapolating from the definitions above, we can say that, given two programs delivering identical functionality to users, the program with the lowest MPE is better encapsulated, and that statistically the more well-encapsulated program will be cheaper to maintain and develop, because the cost of the maximum potential change to it will be lower than the maximum potential change to the less well-encapsulated systém.

Consider, furthermore, a language with just methods and no classes and hence no means of information hiding methods from one another. Let's say our program has 1000 methods. What is the MPE of this program?

Encapsulation theory tells us that, given a system of n public nodes, the MPE of this system is n(n-1). Thus the MPE of our 1000 public methods is 999,000.

Now let's break that systém into two classes, each having 500 methods. As we now have classes, we can choose to have some methods public and some methods private. This will be the case unless every method is actually dependent on every other method (which is unlikely). Let's say that 50 methods in each class is public. What would the MPE of the systém be?

Encapsulation theory tells us it's: n((n/r) -1 + (r-1)p) where r is the number of classes, and p is the number of public methods per class. This would give our two-class systém an MPE of 499,000. Thus the maximum potential cost of a change in this two-class systém is already substantially lower than that of the unencapsulated systém.

Let's say you break your systém into 3 classes, each having 333 classes (well, one will have 334), and again each with 50 public methods. What's the MPE? Using the above equation again, the MPE would be approximately 482,000.

If the systém is broken into 4 classes of 250 methods each, the MPE will would be 449,000.

If may seem that increasing the number of classes in our systém will always decrease its MPE, but this is not so. Encapsulation theory shows that the number of classes into which the systém should be decomposed to minimise MPE is: r = sqrt(n/p), which for our systém is actually 4. A systém with 6 classes, for example, would have an MPE of 465,666.



Related Topics



Leave a reply



Submit