从mysql填充的PHP下拉菜单不起作用

时间:2013-11-14 13:21:13

标签: php mysql function mysqli

我花了几个小时阅读并试图弄清楚为什么这不起作用但是下拉菜单没有填充。我认为这很简单,但我看不到它。任何人吗?

dbconn.php

<?php

define('DB_NAME' , 'artprints');
define('DB_USER' , 'root');
define('DB_PASS' , '');
define('DB_HOST' , 'localhost');

func.php

<?php

include_once 'dbconn.php';

function connect(){
    $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS) or die ('Could not connect to the database' . mysl_error());
    mysqli_select_db($connection, DB_NAME);
}

function close(){
    mysql_close();
}

function query(){
    $myData = mysql_query("SELECT * FROM artists");
    while ($record = mysql_fetch_array($myData)){
    echo '<option value="' . $record['artistID'] . '">' . $record['artistID'] . '</option>';
    }
}

test.php的

<?php
 include_once 'func.php';
 connect();
 ?>

 <html>
 <head>
 <title>Drop down testing</title>
 </head>
 <body>
 <select name='artist'>
 <?php query() ?>
 </select>
 <?php close() ?>
 </body>
 </html>

2 个答案:

答案 0 :(得分:8)

您正在混合mysqli_*mysql_*

mysqli_connectmysqli_select_db

针对

mysql_querymysql_fetch_array

答案 1 :(得分:1)

将其复制并粘贴到您的function.php

include_once ('dbconn.php');

function connect()
{
    $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS) or die ('Could not connect to the database' . mysl_error());
    mysqli_select_db($connection, DB_NAME);
}

function close(){
    mysqli_close();
}

function query(){
    $myData = mysqli_query("SELECT * FROM artists");
    while ($record = mysql_fetch_array($myData)){
    echo '<option value="' . $record['artistID'] . '">' . $record['artistID'] . '</option>';
    }
}