Jump to content
funbinod

not getting $stmt->error message

Recommended Posts

hello all! greetings!

i'm trying to fetch data from a prepared statement and using some variables to pass the query

        $wh = "class=? AND section=?";
        $param = "'ss', $clas, $sec";
        $stmt = $mysqli->prepare("SELECT CONCAT(fname, ' ', lname) AS name, fullid, roll FROM students WHERE $wh AND ay='$ay' AND cid='$cid'");
	if (!$stmt->bind_param($param)) {
		echo "Bind Error: " . $stmt->error;
	}
	?>

this is showing error but only "Bind Error:" not the "$stmt->error". i tried to echo $wh and $param value also and they give the same value as i intend. why this is happening?

please guide.

thanks in advance.

Share this post


Link to post
Share on other sites

An error only occurs after a statement has been executed.

The error here is not a MySQL error, it's a PHP error. You can't pass one single string as multiple arguments of a function, you have to call it like this:

$stmt->bind_param('ss', $clas, $sec)

 

  • Like 1

Share this post


Link to post
Share on other sites

@Ingolme thank u for the reply! i appreciate your suggestion. but my requirement is something different. i have multiple cases for passing variables to the prepared statement.

value for $wh and $param differ if the case is different.

like - in the above example the value of $wh and $param are set when $sec has different value. but if $sec is not set differently then $wh and $param will have only one parameter.

	if ($sec == 'ALL') {
		$wh = 'class=?';
		$param = "'s', $clas";
	} else {
		$wh = "class=? AND section=?";
		$param = "'ss', $clas, $sec";
	}

how can i solve this issue then?

Edited by funbinod

Share this post


Link to post
Share on other sites

ok! i solved this like this

	if ($sec == 'ALL') {
		$stmt = $mysqli->prepare("SELECT CONCAT(fname, ' ', lname) AS name, fullid, roll, section FROM students WHERE class=? AND ay='$ay' AND cid='$cid'");
		$stmt->bind_param('s', $clas);
	} else {
		$stmt = $mysqli->prepare("SELECT CONCAT(fname, ' ', lname) AS name, fullid, roll, section FROM students WHERE class=? AND section=? AND ay='$ay' AND cid='$cid'");
		$stmt->bind_param('ss', $clas, $sec);
	}

thank u for being with me and giving some hint!

is there any other good idea to do this?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×