0

i have this script to connect my DB and i get error 500 when using it.

I tried to track where the error is and i think its at this line: while ($row = $stmt->fetch_assoc())

But i looked over examples and its the same is mine.

Here is my code thanks for helping:

<?php


    $mysqli = new mysqli(*MY DB DETAILS*);

    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
   }

     $sql = "SELECT * FROM comments WHERE workout_name =? AND user =?"; 

    $stmt = $mysqli->prepare($sql) or trigger_error($mysqli->error."[$sql]");

    $stmt->bind_param('ss', $workout_name, $user);

    $workout_name = $_GET['workout_name'];
    $user = $_GET['user'];

    $stmt->execute();

    if ($stmt) 
    {
        while ($row = $stmt->fetch_assoc()) 
    {
        $response["name"] = $row["workout_name"];
        echo json_encode($response);
    }

            $response["success"] = 1;

            // echoing JSON response
            echo json_encode($response);
    }

    else
    {
        $response["success"] = 2;

            // echoing JSON response
         echo json_encode($response);
    }

?>

UPDATE:

I had to change $row = $stmt->fetch_assoc()

as described here:

How to remove the fatal error when fetching an assoc array

Community
  • 1
  • 1
dasdasd
  • 1,971
  • 10
  • 45
  • 72

1 Answers1

0

Before you do this line

$stmt->bind_param('ss', $workout_name, $user);

You have to set $workout_name and $user to contain a value. eg

$workout_name = 'pumping iron';
$user = 'Arnold Schwarzenegger';
$stmt->bind_param('ss', $workout_name, $user);

In your case as you have not even created these variables yet. I assume you are getting the 500 error because the mysqli code goes BANG trying to find NON-EXISTANT variables to load data from.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    @Crontab ??? You corrected my spelling of Arnies family name, which is quite irrelevant to the answer, but left a misspelling of `veriables and a missing full stop between yet and I. Are you for real?? – RiggsFolly Jul 30 '13 at 20:28
  • That's what I get for leaving my machine unlocked for 5 minutes at work. Apologies. – WWW Jul 30 '13 at 22:08
  • @Crontab No problem it made me laugh or rather LOL. – RiggsFolly Jul 30 '13 at 22:12