i trying create connection web form mysql database using php. code follows:
connection.php:
<?php $link = mysqli_connect("", "", "", ""); if (!$link) { echo "error: unable connect mysql." . php_eol; echo "debugging errno: " . mysqli_connect_errno() . php_eol; echo "debugging error: " . mysqli_connect_error() . php_eol; exit; } echo "success: proper connection mysql made! my_db database great." . php_eol; echo "host information: " . mysqli_get_host_info($link) . php_eol; phpinfo();
this part seems work.. connects thank you.php:
<?php function connect() { $conn = mysqli_connect("", "", "", ""); } ini_set('display_errors', 1); error_reporting(~0); require 'connection.php'; $conn = connect(); $query = mysqli_query($conn,"insert members (username) values('1111username')"); $success = $conn->query($query); if (!$success) { die("couldn't enter data: ".$conn->error); } echo "thank contacting <br>"; mysqli_close($conn); ?>
i getting following errors:
warning: mysqli_query() expects parameter 1 mysqli, null given in /var/sites/i/site/public_html/contactform/thankyou.php on line 15
fatal error: call member function query() on null in /var/sites/i/site/public_html/contactform/thankyou.php on line 16
there several issues script:
1: need return connection likes o:
function connect() { return mysqli_connect("", "", "", ""); }
2: when run
$query = mysqli_query($conn,"insert members (username) values('1821username')");
you have executed sql db, not need $success = $conn->query($query);
so code should be:
<?php function connect() { return mysqli_connect("", "", "", ""); } ini_set('display_errors', 1); error_reporting(~0); require 'connection.php'; $conn = connect(); $query = mysqli_query($conn,"insert members (username) values('1821username')"); if (!$query) { die("couldn't enter data: ".$conn->error); } echo "thank contacting <br>"; mysqli_close($conn); ?>
Comments
Post a Comment