doulie0178 2015-09-19 23:50
浏览 130
已采纳

PHP mysqli_query至少需要2个参数

so my problem is with this code gives me the Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\forum\create_cat.php on line 13 if any1 knows what is there to be fixed here pls help


$sql = "SELECT
        cat_id,
        cat_name,
        cat_description,
    FROM
        categories";

$result = mysqli_query($sql);
if(!$result)
{
echo 'The categories could not be displayed, please try again later.';
}
else
}
if(mysqli_num_rows($result) == 0)
{
        echo 'No categories defined yet.';
}
else
{
 echo '<table border="1">
          <tr>
            <th>Category</th>
            <th>Last topic</th>
          </tr>'; 
  while($row = mysqli_fetch_assoc($result))
    {     
 echo '<tr>';
            echo '<td class="leftpart">';
                echo '<h3><a href="category.php?id">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
            echo '</td>';
            echo '<td class="rightpart">';
                        echo '<a href="topic.php?id=">Topic subject</a> at 10-10';
            echo '</td>';
        echo '</tr>';
  }
}

and i`m using this other file to connect to mydb

$server = 'localhost';
$username   = 'root';
$password   = '';
$database   = 'project yi';
 $link=mysqli_connect($server, $username,  $password, $database);
  • 写回答

2条回答 默认 最新

  • dongwen5019 2015-09-20 01:14
    关注

    There are a few things wrong here.

    Firstly, you need to pass db connection to your query.

    What you are presently using:

    $result = mysqli_query($sql);
    

    What you need to use instead:

    $result = mysqli_query($link, $sql);
    

    Read the documention:

    Procedural style

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

    Then, you have a trailing comma in your query after cat_description

    $sql = "SELECT
            cat_id,
            cat_name,
            cat_description, <<< right there
        FROM
            categories";
    
    • You need to remove it.

    Checking for errors on your query would have spotted that.


    Bonus answer to code you didn't post originally but left in a comment instead:

    To answer what you posted above and not being part of your original question, is that your echo wrapping quotes are single quotes and you are also using single quotes inside there.

    • Just do echo "<p><form method='post' ...";

    You're using single quotes for both the opening and closing statement and using single quotes for what is inside the echo statement.

    Rewrite:

    echo "<p><form method='post' action='create_cat.php'></p> 
    Category name: <input type='text' name='cat_name' /> 
    Category description: <textarea name='cat_description' /></textarea> 
    <input type='submit' value='Add category' /> </form>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器