Link to home
Start Free TrialLog in
Avatar of danz67
danz67Flag for Italy

asked on

Php - var

hi,
After creating this query, how do I display the value of the echo id_user?

session_start();
$username = $_SESSION['username'];
$passwd = $_SESSION['password'];
$query = "SELECT id_user FROM users WHERE username='$username' AND passwd='$passwd'";

Open in new window


Thanks
Avatar of Gary
Gary
Flag of Ireland image

Is that all the code you have?  Where's your code to execute the query?
danz67,

Your variable $query is only a string.  First you need to connect to the database

// Connect to database at URL or IP, with Username, and Password
$DB_Link = mysql_connect(strURL, $username, $passwd);

if (!$DB_Link)
{
    die('Could not connect: ' . mysql_error());
}

// Then you need to select the DB name
$db_selected = mysql_select_db(DB_Name, $DB_Link);
if (!$db_selected)
{
    die ('Can\'t use DB_Name : ' . mysql_error());
}

// Then you need to run the query
$result = mysql_query($query);
if (!$result)
{
    die('Invalid query: ' . mysql_error());
}

// Then you need to fetch the row the query returned
if ($row = mysql_fetch_assoc($result))
{
    echo $row['id_user '];
}

// Close the connection
mysql_free_result($result);
Avatar of danz67

ASKER

I thought more, I do not have much experience in this language, you can help me please? thanks
Avatar of danz67

ASKER

Hostname = localhost
dbname = catweb_db
username = root
password = root
Your variable $query is only a string.  First you need to connect to the database

// Connect to database at URL or IP, with Username, and Password
$DB_Link = mysql_connect(strURL, $username, $passwd);

if (!$DB_Link)
{
    die('Could not connect: ' . mysql_error());
}

// Then you need to select the DB name
$db_selected = mysql_select_db(DB_Name, $DB_Link);
if (!$db_selected)
{
    die ('Can\'t use DB_Name : ' . mysql_error());
}

// Then you need to run the query
$result = mysql_query($query);
if (!$result)
{
    die('Invalid query: ' . mysql_error());
}

// Then you need to fetch the row the query returned
if ($row = mysql_fetch_assoc($result))
{
    echo $row['id_user '];
}

// Close the connection
mysql_free_result($result);
how to use these parameters in the example you sent me? I can not, thanks
Your variable $query is only a string.  First you need to connect to the database

$Hostname = localhost;
$dbname = catweb_db;
$username = root;
$password = root;

// Connect to database at URL or IP, with Username, and Password
$DB_Link = mysql_connect($Hostname, $username, $password);

if (!$DB_Link)
{
    die('Could not connect: ' . mysql_error());
}

// Then you need to select the DB name
$db_selected = mysql_select_db(DB_Name, $dbname);
if (!$db_selected)
{
    die ('Can\'t use DB_Name : ' . mysql_error());
}

// Then you need to run the query
$result = mysql_query($query);
if (!$result)
{
    die('Invalid query: ' . mysql_error());
}

// Then you need to fetch the row the query returned.
// If more than one row, use a 'while' instead of an 'if'
if ($row = mysql_fetch_assoc($result))
{
    echo $row['id_user'];
}

// Close the connection
mysql_free_result($result);
Avatar of danz67

ASKER

I tried coem you have explained but I get this error "Can not use db_name"

TableName = users
That means that there isn't a table named 'users' in the selected database, or you do not have sufficient privileges to use it.
Avatar of danz67

ASKER

User generated image
These items must be quoted to work properly.  

$Hostname = "localhost";
$dbname = "catweb_db";
$username = "root";
$password = "root";

Without the quotes they are not considered to be strings.
ASKER CERTIFIED SOLUTION
Avatar of AielloJ
AielloJ
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of danz67

ASKER

Done, but now get "Invalid query: Query was empty"
Avatar of danz67

ASKER

Now work fine, thanks, Regards