1

I have read about 15 posts on here so far regarding issues like this none of which have been able to help me.

I have the following code:

$stmt3 = $mysqli->prepare("SELECT cc.name, cc.company, cc.email FROM (`case_contacts` cc) WHERE cc.case_id=? AND cc.end_date IS NULL");
if (is_object($stmt3)) {
    $stmt3 -> bind_param("i", $case_id);
} else {
    die('Connection issue');
}
$stmt3 -> execute();
$stmt3 -> bind_result($name, $company, $email);
while ($stmt3->fetch()) {

When I execute it advises "Connection issue". However just before $stmt3 I can var_dump $mysqli quite happily.

I have manually entered the query into both phpmyadmin and an online query checker and both confirm that it is valid.

I have checked the permissions of the user, they are all fine. In fact I am in the process of re-writing queries from mysql to mysqli and wantedto use prepared statements. So the database user is the same. I have had no issues with other prepared statements more complex that this so I am really at a loss to what I am missing.

I have even overkilled the '`' quote wrap on the statement as I know I should on every query!

Any advice, debugging tips would be great. I have had a look around and the ones I have found are not providing an output.

1 Answer 1

1

mysqli_stmt::prepare ( string $query ) return TRUE or FALSE read more at:

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

You can use this example:

<?php

$mysqli = mysqli_connect('localhost', 'root', 'admin', 'test');

$case_id = 603;

$sentence =  $mysqli->stmt_init();
if($sentence->prepare("SELECT name, company, email FROM case_contacts WHERE case_id = ? AND end_date IS NULL")) {

    $sentence->bind_param("i", $case_id);
    $sentence->execute();
    $sentence->bind_result($name, $company, $email);

    while ($sentence->fetch()) {
        echo $name . ' - ' . $company . ' - ' . $email;
    }   
}   
7
  • But my current statement in testing should return true but returns false - if I do this won't it always be false. Apr 9, 2015 at 19:10
  • @n00bstacker when you replace the ? by a value and test the query in phpmyadmin the sql is ok? Apr 9, 2015 at 19:12
  • yup Showing rows 0 - 1 (2 total, Query took 0.0001 sec) Apr 9, 2015 at 19:16
  • @n00bstacker edit the question with more code and add the sql of your table, to make a test in my server, let my know Apr 9, 2015 at 19:23
  • That will be like 1000 lines of code - do you know of any ways I can debug prior to pasting all that code? Apr 9, 2015 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.