一尘不染

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\ [duplicate]

mysql

This question already has answers here :

Warning: mysqli_query() expects at least 2 parameters, 1 given. What?
[duplicate]
(2 answers)

Closed 5 years ago.

I’m doing a tutorial in which the author has not updated his content to
reflect changes in the PHP documentation. Anyways, I need to know what is
parameter is being asked of me to provide. I’ve checked that all things are in
order, but I literally don’t know what I’m supposed to provide. Here’s what I
have:

Connects.php

<?php
$connect_error = 'Sorry, we\'re experiencing connection issues.';
$con = mysqli_connect('localhost', 'root', 'PwdSQL5');
mysqli_select_db('phpcadet') or die($connect_error);
?>

And yet I get the error: enter image description
here

Edit: After figuring out to resovle the Connects.php issue, here’s why I get
when fixed it… more errors and here’s my code. Remember I’m new to PHP and
am following a poorly done tutorial.

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli,
string given in C:\vhosts\phpcadet\core\functions\general.php on line 4

General.php

<?php
function sanitize($data)
{
return mysqli_real_escape_string($data, 'What goes here?');
}
?>

Then this: Warning : mysqli_query() expects parameter 1 to be mysqli,
string given in C:\vhosts\phpcadet\core\functions\users.php on line 7

Users.php

<?php
require 'core/database/connects.php';

function user_exists($username)
{
  $username = sanitize($username);
  $query = mysqli_query($_POST['username'], "SELECT * FROM users");
  $row = mysqli_fetch_array($query);

  if($row['username']==$username)
  {
     echo "Welcome, $username!";
     return true;   
  }
  else echo "Please enter a valid username and password";
  return false;
}

function user_active($username)
{
  $username = sanitize($username);
  $query = mysqli_query($_POST['username'], "SELECT * FROM users");
  $row = mysqli_fetch_array($query);

  if($row['username']==$username && $row['active']==1)
  {
     echo "Welcome, $username!";
     return true;   
  } 
  else echo "Your username and password could not be verified.";
  return false;
  }
 ?>

And finally this: Warning : mysqli_fetch_array() expects parameter 1 to be
mysqli_result, null given in C:\vhosts\phpcadet\core\functions\users.php on
line 8

See same code above

Some of my code is a compilation of other answers to solve issues with the
lesson, and were found on Stack. Due to inconsistecy of them, it makes it hard
for me to pick up what is going on… Coming from a different language, I not
that familiar here, and just need the help, so it all clicks. Then I’ll have
“understanding” of what the manual is saying. Thanks.


阅读 1561

收藏
2020-05-17

共1个答案

一尘不染

mysqli_select_db() should have 2 parameters, the connection link and
the database name -

mysqli_select_db($con, 'phpcadet') or die(mysqli_error($con));

Using mysqli_error in the die statement will tell you exactly what is
wrong as opposed to a generic error message.

2020-05-17