duangu9173 2014-10-27 20:03
浏览 55
已采纳

警告:mysqli_select_db()需要2个参数,1在C中给出:[重复]

This question already has an answer here:

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.

</div>
  • 写回答

1条回答 默认 最新

  • douyue9704 2014-10-27 20:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题