php help

sax0n

New Member
Messages
11
Reaction score
0
Points
0
Can anyone help me with this?

PHP:
<html>
<head>
<title> Main </title>
</head>
<body>
<h3> Test </h3>
<?php 
$dbhost = 'localhost';
$dbuser = 'sax0n';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
                     ('Error connecting to mysql');

mysql_select_db("sax0n_main") or die; 

$username = saxon
$ui_table = mysql_query("SELECT * FROM user_info WHERE username == $username");
mysql_fetch_array($ui_table);
$co_long = $ui_table[co-ords_long];
$co_lat = $ui_table[co-ords_lat]; 

$player_location = mysql_query("SELECT Name FROM map WHERE co-ords-long == $co_long AND co-ords-lat = $co_lat"); 
echo $player_location
?>
</body>
</html>

It outputs this...
Parse error: syntax error, unexpected T_VARIABLE in /home/sax0n/public_html/main.php on line 18


Any help is much appreciated :biggrin: I bet it's something painfully obvious...
 
Last edited:

dickey

New Member
Messages
128
Reaction score
0
Points
0
check this code out. it isn't much different from what you posted. but look carefully and you see that you missed out on two semi-colons that signal the end of a command line. which will treat the unterminated line and the next line as one line of command hence the T_VARIABLE ERROR.
PHP:
<html>
<head>
<title> Main </title>
</head>
<body>
<h3> Test </h3>
<?php 
$dbhost = 'localhost';
$dbuser = 'sax0n';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
                     ('Error connecting to mysql');

mysql_select_db("sax0n_main") or die; 

$username = saxon; //you forgot the semi-colon here...
$ui_table = mysql_query("SELECT * FROM user_info WHERE username == $username");
mysql_fetch_array($ui_table);
$co_long = $ui_table[co-ords_long];
$co_lat = $ui_table[co-ords_lat]; 

$player_location = mysql_query("SELECT Name FROM map WHERE co-ords-long == $co_long AND co-ords-lat = $co_lat"); 
echo $player_location; // and this...
?>
</body>
</html>

and you're right it is easy but it isn't that obvious, it happens to me all the time. that's why I know it has something to do with syntax. :)
 
Last edited:

sax0n

New Member
Messages
11
Reaction score
0
Points
0
Thanks! That worked but now I have another error :happysad: :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sax0n/public_html/main.php on line 19

I don't like to be a pain but I can't figure it out.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
try putting an apostrophe around any variables in the database $username. if you don't, then it thinks that you are trying to compare 2 fields ;). the same goes for your second query. also, if you're just getting one row of data, then append LIMIT 1 to the end of your query.

for example, $username = bob. the query will look for " WHERE username = bob".

- a couple other pointers... one, you don't need 2 equal signs (=) to compare in the where clause. just a single one will do fine.
- unless you need keys (1,2...9,10) from your database, mysql_fetch_assoc will do just fine. mysql_fetch_array() will actually create an array ($key => $value), creating useless overhead (unless you have explicit purpose to do so).
- you have to define a variable for mysql_fetch_array/assoc. it will not automatically adhere to the variable in the query.
- also, you do not have to set the database connection to a variable. you can, but it's redundant unless you have a use for it, such as mysqli ;)

PHP:
<html>
<head>
<title> Main </title>
</head>
<body>
<h3> Test </h3>
<?php 
$dbhost = 'localhost';
$dbuser = 'sax0n';
$dbpass = 'password';

mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');

mysql_select_db("sax0n_main") or die; 

$username = 'saxon'; //you forgot the semi-colon here... and the quote to tell it it's a variable
$ui_query = mysql_query("SELECT * FROM user_info WHERE username = '$username' LIMIT 1");
$ui_table = mysql_fetch_assoc($ui_query);
$co_long = $ui_table['co-ords_long']; // putting apostrophes in here will reduce errors
$co_lat = $ui_table['co-ords_lat']; // and here too :)

$player_location = mysql_query("SELECT Name FROM map WHERE co-ords-long = '$co_long' AND co-ords-lat = '$co_lat'"); 
echo $player_location; // and this... why are you echoing a query???
// what you have done with the above is basically "echo true;" or "echo false;" depending on whether or not the query executes without any errors
//you would again want to do another mysql_fetch_assoc
?>
</body>
</html>

Sources: endless months of browsing php.net for w/e reason (prob cause i was bored :p), and a few years of experience ;)
 
Last edited:

sax0n

New Member
Messages
11
Reaction score
0
Points
0
Thanks guys! You are a great help!!!

Just so you know I don't rush to you every time I get an error, I do try to figure it out myself. Something that will imprrove in time.

I have modified my code and it now looks like this:

PHP:
<html>
<head>
<title> Main </title>
</head>
<body>
<h3> Test </h3>
<?php 
$dbhost = 'localhost';
$dbuser = 'sax0n';
$dbpass = 'password';

mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');

mysql_select_db("sax0n_main") or die; 

$username = 'saxon'; //you forgot the semi-colon here... and the quote to tell it it's a variable
$ui_query = mysql_query("SELECT * FROM user_info WHERE username = '$username' LIMIT 1");
$ui_table = mysql_fetch_assoc($ui_query);
echo $ui_table[level];
$co_long = $ui_table['co-ords_long']; // putting apostrophes in here will reduce errors
$co_lat = $ui_table['co-ords_lat']; // and here too :)

$map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'"); 
if ('$map_query'){
    echo "passed";
}
$map_table = mysql_fetch_assoc($map_query);
$player_loaction = $map_table[Name];
echo $player_location; // and this... why are you echoing a query???
// what you have done with the above is basically "echo true;" or "echo false;" depending on whether or not the query executes without any errors
//you would again want to do another mysql_fetch_assoc
?>
</body>
</html>

With this output:

Test
1passed
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/sax0n/public_html/main2.php on line 27

What's it doing now?!?

Thanks guys. Also I tried google but I still don't get the differnece between mysql_fetch_assoc and mysql_fetch_array.



...man I can't wait untill i'm the one answering these questions!
 

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
$map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");
if ('$map_query'){
echo "passed";
}
$map_table = mysql_fetch_assoc($map_query);

Where it has that ^^ copy:
$map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");

under the if statement.

so it should be
$map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");
if ('$map_query'){
echo "passed";
}
$map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");
$map_table = mysql_fetch_assoc($map_query);

usually what i find is that with mysql - after an if statement it clears variable.
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
Just before
PHP:
$map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");
place the line
PHP:
echo "<p>SQL = SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'</p>";

Take the echo'd SQL statement and (if you can't see any glaring errors) place it into phpMyAdmin and run it, that'll give you a much more useful error message.
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
well, first off, where you have if('$map_query'), it should be if($map_query). the apostrophe actually prints out $map_query, and does not parse _ANY_ variables. quotes would parse the variables. Another point: you do not have to call the same query twice. it will stay with that variable that you assign it to.
 

dickey

New Member
Messages
128
Reaction score
0
Points
0
PHP:
$username = 'saxon'; //you forgot the semi-colon here... and the quote to tell it it's a variable
$query_string="SELECT * FROM user_info WHERE username = '$username' LIMIT 1"
$ui_query = mysql_query($query_string); //sometimes this works. 
//insert this code here to prevent some errors I guess.
if ($ui_query==0) 
{
  echo "Invalid username";
}
  else
{
  $ui_table = mysql_fetch_assoc($ui_query);
  ...
  // try to decide which code should go here.
 
Top