Jump to content

Transition To Mysqli -- What Am I Doing Wrong?


sf_guy

Recommended Posts

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

Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.