2

I am a beginner in php and I learn creating registration form from some code samples. My problem is that I do not know how to replace the following lines in old mysql function with mysqli.

$query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
Qirel
  • 25,449
  • 7
  • 45
  • 62
Thoufeeq PP
  • 31
  • 2
  • 5
  • https://www.w3schools.com/php/php_mysql_connect.asp – Prabal Thakur Mar 07 '17 at 11:12
  • study from w3school – Prabal Thakur Mar 07 '17 at 11:13
  • If you can send code I can tell you what to change but without that it tough to judge what you are doing – Prabal Thakur Mar 07 '17 at 11:14
  • This isn't the right place to ask for converting of code. I recommend you check the PHP manual for each function and look at the new syntax. That being said, instead of using direct queries, you should use prepared statements for all queries that aren't static (that has variables in them). – Qirel Mar 07 '17 at 11:21

3 Answers3

5

Use mysqli_query - syntax almost the same, but if your use procedural style, first parameter - database link:

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$query = mysqli_query($link, "INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");

Or object style:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = $mysqli->query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");

Also you should not use variables direct in the sql query, use parameters binding:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$stmt = $mysqli->prepare("INSERT INTO users VALUES ('',?,?,?,?,?,?,'0')")

$stmt->bind_param('ssssss', $un, $fn, $ln, $em, $pswd, $d);
$stmt->execute();
$stmt->close();
kb0
  • 1,143
  • 1
  • 8
  • 13
0

Follow below process

$connect=mysqli_connect("localhost","my_user","my_password","my_db");

mysqli_query($connect,"INSERT INTO users ($fName,$lName,$uAge) 
VALUES ('Maruti','somawanshi',27)");

mysqli_close($connect);
Tom J Muthirenthi
  • 3,028
  • 7
  • 40
  • 60
0

You need to pass your connection link to mysqli_query.

For example:

$conn = mysqli_connect("localhost","user","password","db");

then pass this $conn to mysqli_query.

mysqli_query($conn,"INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')")
Ankit Kumar
  • 180
  • 9