2

I've spent hours reading and trying to figure out why this is not working but the drop down menu does not populate. I think it's something simple but I just cannot see it. Anyone?

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
  • Please post error messages Commented Nov 14, 2013 at 13:23
  • 1
    var_dump($myData); please Commented Nov 14, 2013 at 13:24

2 Answers 2

8

You are mixing mysqli_* and mysql_*

mysqli_connect and mysqli_select_db

against

mysql_query and mysql_fetch_array

Sign up to request clarification or add additional context in comments.

3 Comments

That's a reason why we would rather use PDO
and he forgot ; after function call (query() and close()).
davidkonrad, you're my hero, thanks! Brewal, I'm investigating PDO now, I am reasonably new to PHP and didn't even know this existed. q0re, you're right although it works without them? As a matter of better coding practice I assume they are required and will use them in future. Thanks all!
1

Copy and paste this in your 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>';
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.