Hi I am having trouble with this code:

<?php
//init.inc.php file

session_start();

mysql_connect('mysql.host.com', 'username', 'password');
mysql_select_db('DB name');

include('include');

// Get users from table
function fetch_users() {

$result = mysql_query('SELECT 'user_id' AS 'id', 'user_username' AS 'username' FROM profile'); // Error on this line: Parse error: syntax error, unexpected T_STRING on line 14

$users = array();

while ($row = mysql_fetch_assoc($result) !== false) {

     $users[] = $row;
}

return $users();

}

?>

I dont know how to fix this. I would be glad if you helped.

Recommended Answers

All 3 Replies

Try this:

<?php
//init.inc.php file

session_start();

mysql_connect('mysql.host.com', 'username', 'password') or die(mysql_error());
mysql_select_db('DB name');

include('include'); // whatever your including, meh!

// Get users from table
function fetch_users() {

$query = "SELECT user_id AS id, user_name AS username FROM profile";
$result = mysql_fetch_array($query);

$users = array();



while ($row = mysql_fetch_assoc($result) !== false) {

     $users[] = $row; // you need to specificially identify which row to include, e.g. $row['username'];
}

return $users;

}

?>

It now says an error: Parse error: syntax error, unexpected T_STRING on line 15. Here is my code:

<?php
//init.inc.php file

session_start();

// Get users from table
function fetch_users() {

$query = mysql_query('SELECT `user_id` AS `id`, `user_name` AS `username` FROM profile');

$users = array();

while ($row = mysql_fetch_assoc($result) !== false) {

     $users[] = $row; // you need to specificially identify which row to include, e.g. $row['username'];

}
return $users;
}
?>

The YouTube video I watched to get this is here: http://www.youtube.com/watch?v=dsgnKTuQeMw&feature=autoplay&list=SP074EAEF57B0DA2A4&playnext=3

Try this:

<?php
//init.inc.php file

session_start();

// Get users from table
function fetch_users() {

$query = mysql_query('SELECT `user_id` AS `id`, `user_name` AS `username` FROM profile');

$users = array();

while(($row = mysql_fetch_assoc($query)) !== false)
{
    $users[] = $row;

}
return $users;
}
?>

I'm an idiot.. Sorry!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.