mySQL 查询多个 - 返回错误 mysql_fetch_array [英] mySQL query multiple - returns error mysql_fetch_array

查看:70
本文介绍了mySQL 查询多个 - 返回错误 mysql_fetch_array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个数据库表(用于预订系统),其结构如下:

I have 2 database tables (for a booking system) with the following structures:

四开:

  • id_quarto.
  • tipo_quarto.
  • vista_quarto.

储备:

  • id_reserva.
  • n_cliente.
  • id_quarto.
  • check_in.
  • check_out.

我希望查询返回可用的 quartos(房间)(其中包含字段 id_quarto/tipo_quarto/vista_quarto),这些房间尚未在预订(预订)上预订,因此我编写了以下查询(也从以前的查询中选择信息)形式):

I want the query to return the quartos (rooms) available (with the fields id_quarto / tipo_quarto / vista_quarto from it) which arent already being booked on reservas (reservations) so i write the following query (also picking information from a previous form):

注意:此时我不考虑签入和签出日期因素......这只是一个测试,因此我也会添加条件来检查它,但如果有人对这些条件有一些想法,我会感激.:D

NOTE: At this time i am not considering the check_in and check_out dates factor... this is only a test and therefore i will add the conditions to check it too, but if anyone has some ideas for those conditions i would be grateful. :D

// Connect to database server
mysql_connect("localhost", "root") or die (mysql_error ());
// Select database
mysql_select_db("teste") or die(mysql_error());
// Get data from the database

$strSQL = "SELECT id_quarto,tipo_quarto,vista_quarto ".
          " FROM quartos,reservas ".
          " WHERE quartos.id_quarto!=reservas.id_quarto ".
          " AND quartos.tipo_quarto='". $_POST['tipo_quarto'] ."' ".
          " AND quartos.vista_quarto='". $_POST['vista_quarto'] ."'";

// Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {

?>
<table border="1">
    <tr align="left">
    <td width="75"><?php echo $row['id_quarto']; ?></td>
    <td width="75"><?php echo $row['vista_quarto']; ?></td>
    <td width="75"><?php echo $row['tipo_quarto']; ?></td></tr>
  </table>
 <?php 
 }

   // Close the database connection
//  mysql_close(); ?>

但是当我这样做时,它在 X 行返回一个错误,这是我循环记录集时的行,说mysql_fetch_array() 期望参数 1 是资源,布尔值".

But when I do this it returns an error on Line X, which is the line when i loop the recordset saying that "mysql_fetch_array() expects parameter 1 to be resource, boolean".

这是为什么,我可以做些什么来防止它?我该如何编写正确的代码?

Why is this and what can i do to prevent it? how do i write the correct code?

此外,我希望将结果显示为选择(列表/菜单)表单项,以便用户只能选择有效的结果.知道如何将记录集中的结果与此功能合并吗?

Also, i wanted the results to be featured as a Select (List/Menu) form item so that user the could only choose the valid results. Any idea how to incorporate the results from the recordset with this feature?

推荐答案

你忘记了 mysql_query,更改:

You forget about mysql_query, change:

// Select database
mysql_select_db("teste") or die(mysql_error());

// Get data from the database

$strSQL = "SELECT id_quarto,tipo_quarto,vista_quarto FROM quartos,reservas WHERE quartos.id_quarto!=reservas.id_quarto AND quartos.tipo_quarto='". $_POST['tipo_quarto'] ."' AND quartos.vista_quarto='". $_POST['vista_quarto'] ."'";

// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {

到:

// Select database
mysql_select_db("teste") or die(mysql_error());

// Get data from the database

$strSQL = "SELECT q.id_quarto, q.tipo_quarto, q.vista_quarto ".
          " FROM quartos q, reservas r".
          " WHERE q.id_quarto != r.id_quarto ".
          " AND q.tipo_quarto = '". mysql_real_escape_string($_POST['tipo_quarto']) ."' ".
          " AND q.vista_quarto = '". mysql_real_escape_string($_POST['vista_quarto']) ."'";

$rs = mysql_query($strSQL);

// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {

添加:使用 mysql_real_escape_string 来自用户的每个参数.

Added: Prevent SQL injection using mysql_real_escape_string on each parameter from user.

这篇关于mySQL 查询多个 - 返回错误 mysql_fetch_array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆