Dynadot โ€” .com Registration $8.99

More PHP Help

Spaceship Spaceship
Watch

snike

Established Member
Impact
3
Alright I have a script that needs to retrieve data from inside a mysql column. It need to be a speific column. Here is the database:

Database---users(table)---*username*(the column)----health(data needed)

**= i am using a login script and each person needs to sign up and a column thing is created in the users table (username, password, health, etc.)

How do i retrive the data in health from a certain user that is logged in?

<? echo $_COOKIE['user']; ?> that line echos the username of the logged in person on to the webpage.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
<?php

$user = $_COOKIE['user'];

$query = mysql_query("SELECT health FROM users WHERE username = '".$user."'") or die("Error getting user info:<br />".mysql_error());

//rest here

?>
 
0
•••
Thank you sooo! much!

i get this:
welcome qwerty error #103You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #5' at line 1The query was Resource id #5

ok i know whats wrong:
i have this:
PHP:
<? include("check.php"); ?>
<? include("db.php"); ?>
<html>
<head>
<title></title>
</head>
 
<body bgcolor="#FFFFFF">
 
welcome <? echo $_COOKIE['user']; ?>
 
<?
## Retrive Health Start
 
//Define query
 
$user = $_COOKIE['user']; 

$query = mysql_query("SELECT health FROM users WHERE username = '".$user."'") or die("Error getting user info:<br />".mysql_error()); 

 
if($r = mysql_query($query))
{
  //Retrive and print
  while ($row = mysql_fetch_array($r))
  {
    print "$query\n";
  }
}
else
{
  die('error #103'.mysql_error().'The query was '.$query);
}
 
mysql_close();
 
?>
</body>
</html>
 
Last edited:
0
•••
You can't echo $query.

After SecondVersion's script (still inside the PHP tags) add:

$health = mysql_fetch_row($query);
echo $health;

edit: I see your edit..

You need to echo $row[0] and not $query (print.. echo.. w/e)

mysql_fetch_assoc is slower than mysql_fetch_row and other ones.
 
0
•••
Is this okay? cuz it still dont work
<? include("check.php"); ?>
<? include("db.php"); ?>
<html>
<head>
<title></title>
</head>

<body bgcolor="#FFFFFF">

welcome <? echo $_COOKIE['user']; ?>

<?
## Retrive Health Start

//Define query

$user = $_COOKIE['user'];

$query = mysql_query("SELECT health FROM users WHERE username = '".$user."'") or die("Error getting user info:<br />".mysql_error());


if($r = mysql_query($query))
{
//Retrive and print
while ($row = mysql_fetch_array($r))
{
echo "$row[0]\n";
}
}
else
{
die('<b>Kingdom Tempost Error #103</b> '.mysql_error().'The query was '.$query);
}

mysql_close();

?>
</body>
</html>
 
Last edited:
0
•••
PHP:
<? include("check.php");
 include("db.php"); ?>
<html>
<head>
<title></title>
</head>
 
<body bgcolor="#FFFFFF">
 
welcome <? echo $_COOKIE['user'];

## Retrive Health Start
 
//Define query
 
$user = $_COOKIE['user']; 

$query = mysql_query("SELECT health FROM users WHERE username = '".$user."'") or die("Error getting user info:<br />".mysql_error()); 
if (!$query) {
   echo 'Could not run query: ' . mysql_error();
   exit;
}

$row = mysql_fetch_row($query);
echo "$row[0]\n";
 
mysql_close();
 
?>
</body>
</html>
 
0
•••
thank you it worked! Thanks second version and Dan Friedman!
 
0
•••
There is a problem with this query:
$query = mysql_query("INSERT INTO users (inven)VALUES($inchange) WHERE username = '".$user."'");
 
0
•••
$query = mysql_query("UPDATE users SET inven = '$inchange' WHERE username = '$user'");
 
0
•••
I get this:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
 
0
•••
PHP:
$query = mysql_query("UPDATE users SET inven = $inchange WHERE username = '$user'");
 
0
•••
SV your code didn't work but Dan's did even thou the error came up. Also I want to add that data to the database not update it. So do i use INSERT INTO?
 
0
•••
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back