note 116489 added to mysqli.prepare

From: Date: Sun, 11 Jan 2015 13:23:28 +0000
Subject: note 116489 added to mysqli.prepare
Groups: php.notes 
Request: Send a blank email to php-notes+get-201403@lists.php.net to get a copy of this message
When executing a prepared MySQL, by default if there's an error then you'll simply get FALSE returned from your call to prepare(). To get the full MySQL error back create a statement object before preparing your query as such: <?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $city = "Amersfoort"; /* create a prepared statement */ $statement = $mysqli->stmt_init(); if ($statement->prepare("SELECT District FROM City WHERE Name=?")) { /* bind parameters for marker(s) */ $statement->bind_param("s", $city); /* execute query */ if (!$statement->execute()) { trigger_error('Error executing MySQL query: ' . $statement->error); } /* bind result variables */ $statement->bind_result($district); /* fetch value */ $statement->fetch(); printf("%s is in district %s\n", $city, $district); /* close statement */ $statement->close(); } /* close connection */ $mysqli->close(); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 31.54.63.64 ---- Manual Page -- http://php.net/manual/en/mysqli.prepare.php Edit -- https://master.php.net/note/edit/116489 Del: integrated -- https://master.php.net/note/delete/116489/integrated Del: useless -- https://master.php.net/note/delete/116489/useless Del: bad code -- https://master.php.net/note/delete/116489/bad+code Del: spam -- https://master.php.net/note/delete/116489/spam Del: non-english -- https://master.php.net/note/delete/116489/non-english Del: in docs -- https://master.php.net/note/delete/116489/in+docs Del: other reasons-- https://master.php.net/note/delete/116489 Reject -- https://master.php.net/note/reject/116489 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201403) next »