0

我的文件名是contacts.php;我希望如果按下选择按钮,则调用选择函数。但它显示错误。我编写了以下代码:

           //contacts.php
    <?php
    //1.CREATE CONNECTION
    $connection=mysql_connect("localhost","root","human");
    if(!$connection)
    {
    die("DataBase Connection Failed: ".mysql_error());
    }

    //2.SELECT DATABASE
    $db_select=mysql_select_db("vikas",$connection);
    if(!$db_select)
    {
    die("DataBase Selection Failed: ".mysql_error());
    }

    if(isset($_REQUEST['select']))
    {
    select();
    }

    //select function
    function select()
    {
    echo '<div id="show" style="background-color:#DDDDDD;">';
    $result=mysql_query("select * from contacts", $connection);
    if(!$result)
    {
    die("DataBase Query Failed: ".mysql_error());
    }
    echo <<<END
    <table name=contact>
    <tr><th>ID</th>
    <th>NAME</th>
    <th>NUMBER</th></tr>
    END;
    while($row=mysql_fetch_array($result))
    {
    echo '<tr><td>'.$row["number"].'</td><td>'.$row["name"].'</td></tr>';
    }
    echo '</div>';
    }
    //end of select
    ?>

    <html>
    <body> 
    <form action="contacts.php">
    ENTER ID:<br>
    <input type="text" name="id"/><br>
    ENTER NAME(20 chars):<br>
    <input type="text" name="name"/><br>
    ENTER NUMBER(10 digits):<br>
    <input type="text" name="number"/><br>
    <input type="submit" name="select" value="select"/>
    </form>

    </body>
    </html>

但它显示错误未定义的 var 连接。请帮助这是错误“未定义的变量:C:\wamp\www\site2sms-call\contacts.php 中的连接”

4

3 回答 3

2

因为$connection不在select函数范围内。通过函数参数传递它。

function select($connection)
{
echo '<div id="show" style="background-color:#DDDDDD;">';
$result=mysql_query("select * from contacts", $connection);

调用选择函数时

if(isset($_REQUEST['select']))
{
    select($connection);
}
于 2013-07-26T04:50:11.587 回答
1

我认为您在选择功能中收到了消息?内部函数变量有一个自己的范围,见这里http://php.net/manual/en/functions.arguments.php 这意味着你的函数内部 PHP 不知道你的“外部”变量 $connection。你必须将它传递给函数

于 2013-07-26T04:48:11.270 回答
1
<?php
    //1.CREATE CONNECTION
    $connection=mysql_connect("localhost","root","");
    if(!$connection)
    {
    die("DataBase Connection Failed: ".mysql_error());
    }

    //2.SELECT DATABASE
    $db_select=mysql_select_db("test",$connection);
    if(!$db_select)
    {
    die("DataBase Selection Failed: ".mysql_error());
    }

    if(isset($_REQUEST['select']))
    {
    select();
    }

    //select function
    function select()
    { ?>
    <div id="show" style="background-color:#DDDDDD;">
    <?php 
    $result=mysql_query("select * from contacts", $connection);
    if(!$result)
    {
    die("DataBase Query Failed: ".mysql_error());
    } ?>

   <table name=contact>
    <tr><th>ID</th>
    <th>NAME</th>
    <th>NUMBER</th></tr>
   <?php
    while($row=mysql_fetch_array($result))
    { ?>
       <tr><td><?php echo $row["number"];?>
       </td><td><?php echo $row["name"]; ?></td></tr>
   <?php  } ?>
    </div>
  <?php  }
    //end of select
    ?>
    <html>
    <body> 
    <form action="contacts.php">
    ENTER ID:<br>
    <input type="text" name="id"/><br>
    ENTER NAME(20 chars):<br>
    <input type="text" name="name"/><br>
    ENTER NUMBER(10 digits):<br>
    <input type="text" name="number"/><br>
    <input type="submit" name="select" value="select"/>
    </form>
    </body>
    </html>
于 2013-07-26T05:00:01.967 回答