sf_guy Posted November 1, 2012 Share Posted November 1, 2012 I've been put in charge of writing a fairly large database app for my company using PHP and I know very little about it, but my boss assumes because I know Access that I can figure out PHP, and I have to a large extent. I've got my queries working fine in terms of SELECT but am having a hard time with INSERT, UPDATE and DELETES Here's a sample INSERT that throws three warnings, and fails to update the table, but doesn't cause an error. <?php $host="localhost"; $port=3306; $socket=""; $user="<database user name here>"; $pw="<database password here>"; $dbname="safety"; // Get the user ID and password from sign-in page $id = $_SESSION['userid']; echo 'debug' . $id; // DISPLAYS CORRECTLY $pass = $_REQUEST['pw']; echo 'debug' . $pass // DISPLAYS CORRECTLY $mysqli = new mysqli($host, $port, $socket, $user, $pw, $dbname); // line 62 /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed, please report this error to I.T.: %s\n", mysqli_connect_error()); exit(); } $query = "UPDATE userlist SET pw = '$pass' WHERE userid = '$id'"; $mysqli->query($query); $mysqli->close(); ?> This code generates 3 warnings, and the database is left unchanged. I have no idea what I'm doing wrong, since I've seen examples like this on other web sites. Help for a newbie is much appreciated! The warnings are: Warning: mysqli::mysqli() expects parameter 5 to be long, string given in C:\inetpub\wwwroot\safetymonitor\changesuccess.php on line 62 Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli in C:\inetpub\wwwroot\safetymonitor\changesuccess.php on line 71 // I'm not trying to fetch, just write! Warning: mysqli::close() [mysqli.close]: Couldn't fetch mysqli in C:\inetpub\wwwroot\safetymonitor\changesuccess.php on line 74 Quote Link to comment https://forums.phpfreaks.com/topic/270177-transition-to-mysqli-what-am-i-doing-wrong/ Share on other sites More sharing options...
Barand Posted November 1, 2012 Share Posted November 1, 2012 http://uk1.php.net/manual/en/mysqli.query.php First param of mysqli_query is the connection Quote Link to comment https://forums.phpfreaks.com/topic/270177-transition-to-mysqli-what-am-i-doing-wrong/#findComment-1389442 Share on other sites More sharing options...
kicken Posted November 1, 2012 Share Posted November 1, 2012 (edited) You argument order for mysqli_connect is wrong. If you check the manual page for mysqli_connect, you'll see it is defined as follows (spacing added for clarity) mysqli::__construct ( [ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] ) If you check your call to the function, you have (again, spacing added for clarity): new mysqli( $host , $port , $socket , $user , $pw , $dbname ); As you can clearly see, you have the port where the username should be, the socket where the password should be, etc. You need to re-order the arguments so they match the signature. On an additional note, unless you actually need to specify different port or socket, which based on the code you don't, you should just leave those parameters out and let them be the defaults. Edited November 1, 2012 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/270177-transition-to-mysqli-what-am-i-doing-wrong/#findComment-1389443 Share on other sites More sharing options...
sf_guy Posted November 1, 2012 Author Share Posted November 1, 2012 Thanks, I knew it was something obvious. Had guessed I could probably leave off socket, but didn't know I could leave off port. That means 3306 must be the default? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/270177-transition-to-mysqli-what-am-i-doing-wrong/#findComment-1389447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.