View Full Version : Why fetch_assoc() or fetch_row() giving error always

10-07-2012, 07:09 PM
Hi Every Body

I am facing problem in retreving the data from my mysql table I want to use prepared statement with mysqli for security reason. Here is my code Please give a guidance -

$host="localhost"; // Host name
$username="**********"; // Mysql username
$password="**********"; // Mysql password
$db_name="**********"; // Database name
$tbl_name1="**********"; // tem Table name
$tbl_name2="**********"; // registered user table

$mysqli = new mysqli($localhost, $username, $password, $db_name);
if ($mysqli->connect_errno)
echo "Connection Failed: " . mysqli_connect_errno();

echo $passkey; // exact passkey printed
if($stmt = $mysqli -> prepare("SELECT * FROM $tbl_name1 WHERE confirm_code=?"))
$stmt -> bind_param("s", $passkey);
$stmt -> execute();
echo "\n".$count; // getting the value 1 which is correct
while($rows = $stmt->fetch_assoc())

if (!($insert_stmt = $mysqli->prepare("INSERT INTO $tbl_name2 (first_name,last_name,sex,phone,state,city,pin,school_name,class,computer_subject,board, session,email,password,salt) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")))
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
if(!$insert_stmt->bind_param('sssiiisssssssss',$v_fname,$v_lname,$v_sex,$v_phone,$v_city,$v_state,$v_pin, $v_schoolname,$v_class,$v_subject,$v_board,$v_session,$v_email, $password,$v_salt))
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
echo "Execute failed: (" . $mysqli->errno . ") " . $mysqli->error;
//echo "Data saved properly";
echo "<body bgcolor='#FFFF99'>";
echo "<p align='center'><font color='#008000' size='6' face='Verdana'>";
echo "Congratulation...!! </font>";
echo "</br>";
echo "<font color='e80005' size='5'>Your account has been activated</font>";

if ($stmt = $mysqli->prepare("DELETE FROM $tbl_name1 WHERE confirm_code = ? LIMIT 1"))
echo "Select Failed: (" . $mysqli->errno . ") " . $mysqli->error;
echo "<body bgcolor='#FFFF99'>";
echo "<p align='center'><font color='#e80005' size='6' face='Verdana'>";
echo "SORRY...! </font>";
echo "</br>";
echo "<font color='#e80005' size='5'>Your Confirmation code is not correct</font>";


Soory I forgot to add the error -
Here is the error I'm getting -
Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in /home/cbsecpsn/public_html/confirmation.php on line 38

10-07-2012, 10:08 PM
Just as the error says, there is no method fetch_assoc on a statement object.
You must bind the result and fetch in a loop. See the api here for an example: http://ca1.php.net/manual/en/mysqli-stmt.fetch.php

10-08-2012, 11:25 AM
another option is to convert the mysqli statement into a mysqli result via mysqli_stmt::get_result() (http://ca1.php.net/manual/en/mysqli-stmt.get-result.php).

10-08-2012, 06:04 PM
another option is to convert the mysqli statement into a mysqli result via mysqli_stmt::get_result() (http://ca1.php.net/manual/en/mysqli-stmt.get-result.php).

Yes, that is true, and then you can call fetch_assoc on the mysqli_result object.
Personally I find binding the results to be far faster though. It does have one huge advantage, as of 5.4 it implements Traversable so you can use a foreach on it. For this reason, I may suggest that mysqli_result best be reserved for single field names as it can be treated much closer to the old mysql_result function.
Although, for this particular code here, the mysqli_result may be easier to work with as then you can extract the $row from it. That would make the code rewrite only a couple of lines versus the change to the binding, so the OP really can go either way.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum