How to remove the fatal error when fetching an assoc array
The variable $stmt is of type mysqli_stmt, not mysqli_result. The mysqli_stmt class doesn't have a method "fetch_assoc()" defined for it.
You can get a mysqli_result object from your mysqli_stmt object by calling its get_result() method. For this you need the mysqlInd driver installed!
$result = $stmt->get_result();
row = $result->fetch_assoc();
If you don't have the driver installed you can fetch your results like this:
$stmt->bind_result($dbUser, $dbEmail);
while ($stmt->fetch()) {
printf("%s %s\n", $dbUser, $dbEmail);
}
So your code should become:
$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute();
// bind variables to result
$stmt->bind_result($dbUser, $dbEmail);
//fetch the first result row, this pumps the result values in the bound variables
if($stmt->fetch()){
echo 'result is ' . dbEmail;
}
Fatal error: Call to a member function fetch_assoc() on a non-object while converting result to array
You need to fetch the results of your query; fetch(PDO::FETCH_ASSOC)
will return each result as an associative array:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
array_push($results, $row);
}
Getting fatal error on mysqli query
I think this should work just remove $result = $con->query($query2);
$query2 = $con->query("SELECT * FROM journeys WHERE origin = $origin AND destination = $destination AND date = $date AND hour = $hour AND minute = $minute AND id != $id");
$count = mysqli_num_rows($query2);
if($count = 0) {
echo "Sorry, there are no other InterRailer's making this journey, try searching for individual legs if your journey contains more than one leg.";
}
else{
//$result = $con->query($query2);
while ( $row = $query2->fetch_assoc() ) {
$companion[] = "<form id='companionresult' method='post' action='accountview.php'><input type='hidden' name='id' value='{$row['id']}'>{$row['firstname']}, {$row['age']}<br>{$row['nationality']}<br>Speaks: {$row['language1']}{$row['language2']}{$row['language3']}{$row['language4']}{$row['language5']}<br> <input id='submit3' type='submit' name='submit' value='View Profile'><br>";
foreach($companion as $info)
echo $info;
}
}
Edit: Actually the problem is in your query, so print your query and run it in phpmyadmin or sql and see if any error occurs. here is what I tried and it worked on my computer.
<?php $con= mysqli_connect("localhost", "root", "admin","demo");
$id=2;
$origin='india';
$destination='delhi';
$date='2014-09-23';
$hour='1';
$minute='10';
//for better error detection print your query here
/*echo "SELECT * FROM journeys WHERE origin = '$origin'
AND destination = '$destination' AND date ='$date' AND hour ='$hour'
AND minute ='$minute' AND id !='$id'";*/
//Quote values in single quote for string or date values because if they will be blank your query will go wrong, it will mix with and like where origin= AND destination= which will produce error.
$query2 = $con->query("SELECT * FROM journeys WHERE origin = '$origin'
AND destination = '$destination' AND date ='$date' AND hour ='$hour'
AND minute ='$minute' AND id !='$id'");
$count = mysqli_num_rows($query2);
$companion=array();
if($count = 0) {
echo "Sorry, there are no other InterRailer's making this journey, try searching for individual legs if your journey contains more than one leg.";
}
else{
//$result = $con->query($query2);
while ( $row = $query2->fetch_assoc() ) {
$companion[] = "<form id='companionresult' method='post' action='accountview.php'><input type='hidden' name='id' value='{$row['id']}'>{$row['firstname']}, {$row['age']}<br>{$row['nationality']}<br>Speaks: {$row['language1']}{$row['language2']}{$row['language3']}{$row['language4']}{$row['language5']}<br> <input id='submit3' type='submit' name='submit' value='View Profile'><br>";
}
//print your foreach outside while loop.
foreach($companion as $info)
echo $info;
}
?>
Undefined class constant 'FECTH_ASSOC' , unable to fetch selected row in an associative array
it's a Typo on your part please, check the spelling, it should be
PDO::FETCH_ASSOC
Related Topics
How to Enable Put Requests in Azure
Is There a Limit When Using PHP Mail Function
Data Transfer from JavaScript to PHP
Admin Product Pages Custom Field Displayed in Cart and Checkout
How to Visualize a Bunch of Functions in Uml
How to Create a .Gz File Using PHP
Starting with Laravel on Ubuntu
Is Header('Content-Type:Text/Plain'); Necessary at All
How to Have a Stable Sort in PHP with Arsort()
Mysqli_Fetch_Array Returning Only One Result
MySQL Pdo Name-Value Prepared Statement Using Last Parameter Only
Display an Array in a Readable/Hierarchical Format
How to Log in to PHPmyadmin with Wamp, What Is the Username and Password