Jump to content

Mysql Select Comes Back As Null?


Lethal323

Recommended Posts

Alright well im making something and when I try to load something from my DB the result comes back as 0 which is also null. I have been working on this for around 2 days and cannot get it to work. Here is my code:

 

<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) 
{
$userId_var = $_POST['username'];
$password_var = $_POST['password'];
$sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')";
$avatar = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'");
$result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 
if (mysql_num_rows($result) == 1) 
	{
	$_SESSION['db_is_logged_in'] = true;
	$_SESSION['username'] = $userId_var;
	$_SESSION['avatar'] = $avatar;
	header('Location: index.php');
	} 
    else 
	{
	$error = 1;
	$errorMessage = 'Sorry, wrong user id / password';
	}
}

 

The avatar is what will not load. The username as you can see there. It does load but the avatar doesnt.

Link to comment
https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/
Share on other sites

Alright well I tried this.

 

<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) 
{
$userId_var = $_POST['username'];
$password_var = $_POST['password'];
$sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')";
$avatar = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'");
$result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 
if (mysql_num_rows($result) == 1) 
	{
	$_SESSION['db_is_logged_in'] = true;
	$_SESSION['username'] = $userId_var;
	$_SESSION['avatar'] = mysql_fetch_array($avatar);
	header('Location: index.php');
	} 
    else 
	{
	$error = 1;
	$errorMessage = 'Sorry, wrong user id / password';
	}
}

 

 

The result came back as nothing not a zero or anything.

 

 

BTW: the result is suppoes to be '<img src=imgs/avatars/dizzy-avatar.jpg>'

Trie this mate might work if not i am sure a user will tell me why....

 

might all be wrong theo always backup.........

<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) 
{
$userId_var = $_POST['username'];
$password_var = $_POST['password'];

$sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')";

$avatar = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'");

$result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 

                 while($x=mysql_fetch_assoc($result)) 

{
	$_SESSION['db_is_logged_in'] = true;
	$_SESSION['username'] = $x['userId_var'];
	$_SESSION['avatar'] = $x['avatar'];
	header('Location: index.php');
	} 
    else 
	{
	$error = 1;
	$errorMessage = 'Sorry, wrong user id / password';
	}
}
?>

No no no. D:  He's only fetching one result.  The while loop is overkill.

 

<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) 
{
$userId_var = $_POST['username'];
$password_var = $_POST['password'];
$sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')";
$avatarrows = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 
       $avatar = mysql_fetch_assoc($avatarrows);
if (mysql_num_rows($result) == 1) 
	{
	$_SESSION['db_is_logged_in'] = true;
	$_SESSION['username'] = $userId_var;
	$_SESSION['avatar'] = $avatar['user_avatar'];
	header('Location: index.php');
	} 
   else 
	{
	$error = 1;
	$errorMessage = 'Sorry, wrong user id / password';
	}
}

I found another problem here is the updated code:

 

<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) 
{
$userId_var = $_POST['username'];
$password_var = $_POST['password'];
$sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')";
$avatarrows = mysql_query("SELECT * FROM users WHERE user_id = '.$userId_var.'");
$result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 
        $avatar = mysql_fetch_assoc($avatarrows);
if (mysql_num_rows($result) == 1)
	{
	$_SESSION['db_is_logged_in'] = true;
	$_SESSION['username'] = $userId_var;
	$_SESSION['avatar'] = $avatar['user_avatar'];
	header('Location: index.php');
	} 
    else 
	{
	$error = 1;
	$errorMessage = 'Sorry, wrong user id / password';
	}
}
?>

 

It still has a syntax error..

 

Here is the website if you want to test it...

 

www.thecsclan.net

Username: Dizzy

Password: Kev1b00

 

Go to edit profile, and notice how it doesnt load the avatar image.

Here:

 

$avatarrows = mysql_query("SELECT * FROM users WHERE user_id = $userId_var") OR die(mysql_error());

 

You didn't end the string before concatenation.  Not a PHP error, just a MySQL one.  And since you didn't error check....it didn't work. =/  Now it should.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.