More PHP Help

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
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
Close
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
Close
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
Close
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
Close
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
Close
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
Close
thank you it worked! Thanks second version and Dan Friedman!
 
0
Close
There is a problem with this query:
$query = mysql_query("INSERT INTO users (inven)VALUES($inchange) WHERE username = '".$user."'");
 
0
Close
$query = mysql_query("UPDATE users SET inven = '$inchange' WHERE username = '$user'");
 
0
Close
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
Close
PHP:
$query = mysql_query("UPDATE users SET inven = $inchange WHERE username = '$user'");
 
0
Close
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
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back