mysqli_query()至少需要2个参数,其中1个是切换到php 7后给出的

时间:2018-10-03 11:22:08

标签: php mysql

<div class=" container hosteldetails">
<?php
  $get_data=mysqli_query("select * from hostels where hostel_id=1s");
    while($result_data=mysqli_fetch_assoc($get_data)){
      ?>

      <div>
   <h4 style="text-decoration: underline;"> General </h4>   
   <ul class="detailslist">  
   <li> Name: <?php echo $result_data['hostel_name'];?> </li> 
  <li> Address: <?php echo $result_data['hostel_address'];?> </li> 
  <li> Phone no: <?php echo $result_data['hostel_phone'];?> </li> 
 <li> Email: <?php echo $result_data['hostel_email'];?> </li>
 <li> Gender: <?php echo $result_data['hostel_gender'];?> </li>
 <li> Owner: <?php echo $result_data['hostel_owner'];?> </li>
</ul>

<h4 style="text-decoration: underline;"> Facilities </h4>  
   <ul class="detailslist">  
   <li> Facilities: <?php echo $result_data['hostel_facilities'];?> </li> 

</ul>
</d**strong text**iv>
<?php } ?>
</div>*emphasized text*

这是我用来从数据库中获取数据的代码。与php 5.6.3配合良好。我刚切换到php7。

它显示以下错误

enter image description here

2 个答案:

答案 0 :(得分:3)

使用mysqli_query时,您需要将链接作为第一个参数传递,因此请更改

$get_data=mysqli_query("select * from hostels where hostel_id=1s");

收件人

$get_data=mysqli_query($link, "select * from hostels where hostel_id=1s");

按照documentation

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
  

仅用于程序样式:mysqli_connect()或mysqli_init()返回的链接标识符

更新

根据@FunkFortyNiner的评论,您需要引用传递的值,但不要这样做,而应使用prepared statements

答案 1 :(得分:-1)

由于错误状态mysqli_query()需要2个参数。您的代码中只有一个。

更改:

$get_data = mysqli_query("select * from hostels where hostel_id=1s");

收件人:

$con = mysqli_connect('localhost','user','pass','db');
$get_data = mysqli_query($con, "select * from hostels where hostel_id='1s'");

$con传递执行查询的活动数据库连接详细信息