0

I feel like such a noob for asking another question, but I cba to google for hours to see if I can find a fix anywhere. Since no one has 100% the same code, I'm just gonna go ahead and ask another question since thats what this community is here for :P

So yeah, what I did is the following; I made a member_list where all the usernames are listed and linked to their own page. Now I want to create that own page by getting their information from the database. The links are as following: profile.php?username=USERNAMEOFTHEUSER, example: domain.com/profile.php?username=robert

I made a new function called 'fetch_user_info' and stored it in 'user_info.php'. This is the code of the file:

    $users = array();
$result = mysqli_query($mysqli,"SELECT username FROM members");
while($row = mysqli_fetch_assoc($result)){
  $users[] = $row['username']; 
}

function fetch_user_info($username){
$mysqli = "SELECT username, name, age, FROM members  WHERE username = '$username'"; 

    $result = mysqli_query($mysqli);
 return mysqli_fetch_assoc($result);
}

What I'm troublin' with is getting his (or anyone elses) data from the database. I know I must be using the $_GET. This is my testdocuments code;

<?php
include '../../includes/db/db_connect.php';
include '../../includes/fct/functions.php';
include 'user_info.php';

sec_session_start();

$user_info = fetch_user_info($_GET['username']);

print_r($user_info);

?>
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

</body>
</html>

I'm getting the following errors:

Warning: mysqli_query() expects at least 2 parameters, 1 given in Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in

What I want is to echo their username, name, age etc. out anywhere I want. So it must be a variable!

Any help would be appreciated!

  • 1
    `mysqli_query() expects at least 2 parameters, 1 given` did you read the manual of `mysqli_query` ? – Royal Bg Dec 28 '13 at 00:15
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Jan 02 '14 at 00:27

3 Answers3

1

You're using mysqli_query procedural Style. You need to pass it two params.

The second one is the query you want to execute, the first one is the link.

The link in something like this: $link = mysqli_connect("localhost", "my_user", "my_password", "world");

Goikiu
  • 574
  • 3
  • 12
  • So does he need a mysqli_connection in every function he make? – Royal Bg Dec 28 '13 at 00:17
  • 1
    In every query, yes. Otherwise, stick to PDO. – Eisa Adil Dec 28 '13 at 00:18
  • It seems for me worse than `global` syntax. Imagine 20 function, each has `mysqli_connect()`. I would suggest a semi object oriented style, where in constructor you have connection established to private property – Royal Bg Dec 28 '13 at 00:20
  • Well, the procedural style need 2 params, 1 is the link. With OOP Style you need only the query (php.net say so) – Goikiu Dec 28 '13 at 00:21
  • 1
    That was not my point. You can still have the procedural style encapsulated in a class. `public function __construct() { $this->_conn = mysqli_connect(.......); } public function fetch_user_info() { mysqli_query($this->_conn, ...); }`. Not the best coding style, but still seems to me better than using global or declaring connection in each function. – Royal Bg Dec 28 '13 at 00:23
  • i normally put a second parameter when launching the function, like public function launchQueryOne($var, $link){//etc}, isn't as good as others one... but it can work too maybe. I believe you need only 1 link for run, maybe two but only if you use more than one database at time. – Goikiu Dec 28 '13 at 00:28
  • I cannot stress enough that you should really look at using PDO. The syntax/usage is a little odd at first, but I find it MUCH easier to use and read than the older procedural style. – JC. Dec 28 '13 at 00:29
0

That's not even the right syntax. You need to specify the connection each time you query.

$con = mysqli_connect("localhost", "my_user", "my_password", "world");

function fetch_user_info($username, $con){
$mysqli = "SELECT username, name, age FROM members WHERE username = '$username'";   

$result = mysqli_query($con, $mysqli);
return mysqli_fetch_assoc($result);
}
Eisa Adil
  • 1,743
  • 11
  • 16
0

I've removed the function and did it the newbie way. Not sure whether it's actually a good way to do this, but it works. Please correct me if necessary. I'm here to learn.

For someone with the same problem, here you go:

$username = ($_GET['username']);
$getUsers = mysqli_query($mysqli,"SELECT * FROM members WHERE username = '$username'");
if ( mysqli_num_rows($getUsers) > 0 ) { 
      $row = mysqli_fetch_array($getUsers);
        $username = $row['username']; 
        $name = $row['name']; 
        $age = $row['age']; 
        $sex = $row['sex']; 
        $place = $row['place']; 
      } else {
        echo "Woopie, something went wrong!";
      }

then u can echo it anywhere in the page you like by:

etc.