Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why fetch_assoc() or fetch_row() giving error always

Status
Not open for further replies.

vishalonne

Technical User
Jul 29, 2012
49
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 -
Here is the error -
PHP:
<?php
$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();
    exit();
}
$count=null;
$passkey=$_GET['passkey'];

echo $passkey;  // exact passkey printed
if($stmt = $mysqli -> prepare("SELECT * FROM $tbl_name1 WHERE confirm_code=?"))
{
    $stmt -> bind_param("s", $passkey);
    $stmt -> execute();
    $stmt->store_result();
    $count=$stmt->num_rows;
    echo "\n".$count;   // getting the value 1 which is correct
    if($count==1)
    {
	while($rows = $stmt->fetch_assoc())
	{
	     $v_fname=$rows['temp_first_name'];
	     $v_lname=$rows['temp_last_name'];
	     $v_sex=$rows['temp_sex'];
	     $v_phone=$rows['temp_phone'];
	     $v_city=$rows['temp_state'];
	     $v_state=$rows['temp_city'];
	     $v_pin=$rows['temp_pin'];
	     $v_schoolname=$rows['temp_school_name'];
	     $v_class=$rows['temp_class'];
	     $v_subject=$rows['temp_computer_subject'];
	     $v_board=$rows['temp_board'];
	     $v_session=$rows['temp_session'];
	     $v_email=$rows['temp_email'];
	     $password=$rows['temp_password'];
	     $v_salt=$rows['temp_salt'];

	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;
	}
	if(!$insert_stmt->execute())
	{
		echo "Execute failed: (" . $mysqli->errno . ") " . $mysqli->error;
	}
	else
	{
		//echo "Data saved properly";
		$flag=1;
		if($flag==1)
		{
			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")) 
        	        { 
                	        $stmt->bind_param("s",$passkey);      
                        	$stmt->execute();      
	                }
		}
	}
      }
   }
}
else
{
    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>";
}
  $stmt->close(); 
?>
 
Fetch_assoc is not a method of the mysqli statement classs

That is not how you use MySQLi fetch. Read the manual or perhaps use plain MySQL or even better PDO.
 
@jpadie
Hello
I came to know about fetch_assoc() from PHP Manual
Is this maual giving wrong information or I am not understood the Manual properly???
Please guide

Regards
 
no. the manual is correct.

however you are using a mysqli statement and not a mysqli resultset. These are not the same objects. This is why I suggested reading the manual.

to get the result set, after buffering the result with store_result() (note that there is no need to do this if you do not want the number of records ab initio), you must use the get_result() method.

Code:
[COLOR=#009900]$result[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$stmnt[/color][COLOR=#990000]->[/color][b][COLOR=#000000]get_result[/color][/b][COLOR=#990000]();[/color]
[COLOR=#009900]$row[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$result[/color][COLOR=#990000]->[/color][b][COLOR=#000000]fetch_assoc[/color][/b][COLOR=#990000]();[/color]
[b][COLOR=#0000FF]while[/color][/b] [COLOR=#990000](![/color][b][COLOR=#000000]is_null[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000])):[/color]
 [b][COLOR=#0000FF]echo[/color][/b] [COLOR=#FF0000]'<pre>'[/color][COLOR=#990000].[/color] [b][COLOR=#000000]print_r[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000],[/color] true[COLOR=#990000])[/color] [COLOR=#990000].[/color] [COLOR=#FF0000]'</pre>'[/color][COLOR=#990000];[/color]
 [COLOR=#009900]$row[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$result[/color][COLOR=#990000]->[/color][b][COLOR=#000000]fetch_assoc[/color][/b][COLOR=#990000]();[/color]
[b][COLOR=#0000FF]endwhile[/color][/b][COLOR=#990000];[/color]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top