Christopher
Christopher

Reputation: 9824

MySQLI mysqli_store_result results with Malformed Packet error

I am in the process of converting some old MySQL code into MySQLI Prepared Statements and hit a snag: If I run the same SQL code as prepared statement, I get a "Malformed Package" error. This happens even with extremely simple queries like "SELECT * FROM [TableName]".

I have the creation of the connection and setting of the Report level in a Seperate file altogether. So that code must be identicaly by definition.

As specific example, this code works:

$sql = "SELECT * FROM AngebotsDB";
$result = mysqli_query($link, $sql);

But this code:

$sql = "SELECT * FROM AngebotsDB";
// $result = mysqli_query($link, $sql);

$stmt = mysqli_stmt_init($link);
mysqli_stmt_prepare($stmt,$sql);
mysqli_execute($stmt);
$resultReference = mysqli_store_result($link); //throws exception
$result = mysqli_fetch_array($resultReference);

ends in:

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 
'Malformed packet' in /home/cgroschupff/public_html/custom_code/DB 
structure.php:16 Stack trace: #0 /home/cgroschupff/public_html/custom_code/DB structure.php(16): 
mysqli_store_result(Object(mysqli)) #1 {main} thrown in 
/home/cgroschupff/public_html/custom_code/DB structure.php on line 16

All I could really find is some old information of this happening when Connecting to the DB.

Note that the used MySQLi/PHP version is rather old (5.2.17?). So this could be a "long ago fixed" bug?

Upvotes: 0

Views: 728

Answers (1)

gaurav
gaurav

Reputation: 1339

If you initialize a statement than you have to call other functions according to mysqli_stmt class so your code should be .

   $sql = "SELECT * FROM AngebotsDB";
   $stmt = mysqli_stmt_init($link);
   mysqli_stmt_prepare($stmt,$sql);
   mysqli_stmt_execute($stmt);
   $resultReference = mysqli_stmt_store_result($link); 

Now if you try var_dump($resultReference) than return true or false .

if you want to show result with mysqli_fetch_array so you have to pass mysqli_result parameter so for this you have to use mysqli_stmt_get_result .

   $sql = "SELECT * FROM AngebotsDB";
   $stmt = mysqli_stmt_init($link);
   mysqli_stmt_prepare($stmt,$sql);
   mysqli_stmt_execute($stmt);
   $result = mysqli_stmt_get_result($stmt) ;
   $output = mysqli_fetch_array($result) ; 

Now you can see var_dump($output) than you have result .

Upvotes: 1

Related Questions