php - Trouble using a reusable database connector -


i'm new php. i've been having trouble connecting , using data php. don't have information on issue, maybe i'm using out of date method, or did wrong. i've double checked , looked on website information, didn't find much. here's code below.

the error reads :

fatal error: call member function query() on boolean in c:\xampp\htdocs\website\practice\mysqli\pdo.php on line 6

that mean there problem located near the

$result = $conn->query($sql)or die(mysqli_error()); 

i entered username , password correctly. created new username , password make sure. have no other ideas why $conn isn't working, , love thoughts or ideas on issue!

connection.ini.php

function dbconnect($usertype, $connectiontype = 'pdo') {     $host = 'localhost';     $db = 'student';     if ($usertype == 'read') {         $user = 'user';         $pwd = 'pass';     }      elseif ($usertype == 'write') {         $user = 'root';         $pwd = 'password';     }     else {         exit('unrecognized connection type');     }     //connection code     if ($connectiontype ='mysqli') {         return new mysqli($host, $user, $pwd, $db) or die ('cannot open database');         }  else {          try {             return new pdo('mysql:host=$host;dbname=$db, $user, $pwd');         }         catch(pdoexecption $e) {             echo 'cannot connect database';             exit;         }     } } ?> 

mysqli.php

?php require_once('connection.inc.php'); $conn = dbconnect('read', 'pdo'); $sql = 'select * guestbook';  $result = $conn->query($sql)or die(mysqli_error()); $numrows = $result->num_rows; ?> <!doctype html> <html> <p> total of <?php echo $numrows; ?> records found.</p> </html> 

any time the...

"fatal error: call member function bind_param() on boolean"

...it because there issue query. prepare() might return false (a boolean), generic failure message doesn't leave in way of clues. how find out wrong query? ask!

first of all, make sure error reporting turned on , visible: add these 2 lines top of file(s) right after opening <?php tag:

error_reporting(e_all); ini_set('display_errors', 1); 

if error reporting has been set in php.ini won't have worry this. make sure handle errors gracefully , never reveal true cause of issues users. revealing true cause public can gold engraved invitation wanting harm sites , servers. if not want send errors browser can monitor web server error logs. log locations vary server server e.g., on ubuntu error log typically located @ /var/log/apache2/error.log. if you're examining error logs in linux environment can use tail -f /path/to/log in console window see errors occur in real-time....or make them.

once you're squared away on standard error reporting adding error checking on database connection , queries give more detail problems going on. have @ example column name incorrect. first, code returns generic fatal error message:

$sql = "select `foo` `weird_words` `definition` = ?"; $query = $mysqli->prepare($sql)); // assuming $mysqli connection $query->bind_param('s', $definition); $query->execute(); 

the error generic , not helpful in solving going on.

with couple of more lines of code can detailed information can use solve issue immediately. check prepare() statement truthiness , if can proceed on binding , executing.

$sql = "select `foo` `weird_words` `definition` = ?"; if($query = $mysqli->prepare($sql)) { // assuming $mysqli connection     $query->bind_param('s', $definition);     $query->execute();     // additional code need go here. } else {     $error = $mysqli->errno . ' ' . $mysqli->error;     echo $error; // 1054 unknown column 'foo' in 'field list' } 

if wrong can spit out error message takes directly issue. in case there no foo column in table, solving problem trivial.

if choose, can include checking in function or class , extend handling errors gracefully mentioned previously.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -