-2

i would love if any one could help me out with this issue.

The config.php file which is included works fine, and the database loads fine.

It's just about echoing stuff out of the database when connected with an Included file. How would i do that the simplest cleanest way?

<?php
include 'config.php';
$username = $_COOKIE["ava_username"];
$user_id = $_COOKIE["ava_userid"];
$useridquery = mysql_query("SELECT id FROM ava_users WHERE username=$cookie");

if (isset($_COOKIE["ava_username"])) {
$result = mysql_query("SELECT points FROM ava_users WHERE username=$username");
        echo "Cookie is enabled, and User status login is 0<br>";
        echo "User ID: ". $user_id . "<br>";
        echo "User Name: " . $_COOKIE["ava_username"]. "<br>";
        while ($row = mysqli_fetch_array($result))
        {
        echo $row['points'] . " and Joined " . $row['joined'];
        echo "<br>";
  }


}      
?>
elixenide
  • 44,308
  • 16
  • 74
  • 100

5 Answers5

1

You have mixed up mysql_* with mysql_i*

$useridquery = mysql_query("SELECT id FROM ava_users WHERE username=$cookie");

above line contains wrong query (need to use quotation )and seems that $useridquery is unused. this line should be like this:

$useridquery = mysqli_query("SELECT id FROM ava_users WHERE username='$cookie'");

same wrong query in this line :

 $result = mysql_query("SELECT points FROM ava_users WHERE username=$username");

should be :

$result = mysqli_query("SELECT points FROM ava_users WHERE username='$username'");

try this:

include 'config.php';
$username = $_COOKIE["ava_username"];
$user_id = $_COOKIE["ava_userid"];
$useridquery = mysqli_query("SELECT id FROM ava_users WHERE username='$cookie'"); //add quotation in  variable 

if (isset($_COOKIE["ava_username"])) {
$result = mysqli_query("SELECT points FROM ava_users WHERE username='$username'"); //add quotation in username variable 
        echo "Cookie is enabled, and User status login is 0<br>";
        echo "User ID: ". $user_id . "<br>";
        echo "User Name: " . $_COOKIE["ava_username"]. "<br>";
        while ($row = mysqli_fetch_array($result))
        {
        echo $row['points'] . " and Joined " . $row['joined'];
        echo "<br>";
  }


}  
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
0

You are mixing between mysql and mysqli .

Try this:

 <?php
include 'config.php';
$username = $_COOKIE["ava_username"];
$user_id = $_COOKIE["ava_userid"];
$useridquery = mysqli_query("SELECT id FROM ava_users WHERE username=$cookie");

if (isset($_COOKIE["ava_username"])) {
$result = mysqli_query("SELECT points FROM ava_users WHERE username=$username");
    echo "Cookie is enabled, and User status login is 0<br>";
    echo "User ID: ". $user_id . "<br>";
    echo "User Name: " . $_COOKIE["ava_username"]. "<br>";
    while ($row = mysqli_fetch_array($result))
    {
    echo $row['points'] . " and Joined " . $row['joined'];
    echo "<br>";
 }


}      
?>
echo_Me
  • 37,078
  • 5
  • 58
  • 78
0

You are mixing functions from the mysql library with functions from the mysqli. They aren't the same, and that won't work. Please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead.

Another problem: you use $cookie in your first query, but it looks like you mean $username. $cookie isn't defined. It doesn't look like you ever even use that query's results, though, so you could just take it out.

Another problem: you are wide open to SQL injection.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
0

The problem is you are mixing two libraries (the mysql_*-family and the mysqli_*-family). You cannot use mysqli_fetch_array(...) with mysql_query(...).

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
0

Basically the mysqli_query will not work till both the parameters are included. Mysqli query requires 2 parameters $link = connection to the database and $sql = the sql query. So typically the mysqli_query is of the following format mysqli_query($link, $sql) . You can try as below

<?php
include 'config.php';
global $link; //where $link = mysqli_connect("DBserver", "DBuser", "DBpswd", "DBname")

$username = $_COOKIE["ava_username"];
$username = mysqli_real_escape_string($username);
$user_id = $_COOKIE["ava_userid"];
//if the id is an integer then instead of mysqli_real_escape_string use
$user_id = intval($user_id);

$sql = "SELECT id FROM ava_users WHERE username = '$username'";
$useridquery = mysqli_query($link, $sql);

if (isset($_COOKIE["ava_username"])) {
$sql = "SELECT points FROM ava_users WHERE username='$username'";
$result = mysqli_query($link, $sql);
echo "Cookie is enabled, and User status login is 0<br>";
echo "User ID: ". $user_id . "<br>";
echo "User Name: " . $_COOKIE["ava_username"]. "<br>";
while ($row = mysqli_fetch_array($result))
{
echo $row['points'] . " and Joined " . $row['joined'];
echo "<br>";
 }


}      
?>

However even this code is highly vulnerable to MYSQL injection. You should always use parameterized queries to get protection against sql injection.

Donkarnash
  • 12,433
  • 5
  • 26
  • 37