Page 1 of 1

mySQL code problem (php)

Posted: Fri Oct 03, 2003 3:18 pm
by Rob
I wrote this code to get an ID number from a field. I tryed to do it by looking for the username and then getting the ID. This is my code.

$coolguy = $_SESSION['loggedin'];
$usertoget = "SELECT id FROM usersys WHERE user='$coolguy'";
$usertoget1 = MYSQL_QUERY("$usertoget");

It returns resource #4 every time for some reason. Please help

Posted: Fri Oct 03, 2003 5:27 pm
by qads
your code only checks to see if the id is there or not, u don't have anything to get the data out and display it.

use this:

Code: Select all

<?php
$coolguy = $_SESSION['loggedin']; 
$usertoget = "SELECT id FROM usersys WHERE user='$coolguy'"; 
$usertoget1 = MYSQL_QUERY("$usertoget"); 
$userdata = mysql_fetch_array($usertoget1);
echo "id = $userdata[id]";
?>

Posted: Fri Oct 03, 2003 5:33 pm
by Rob
Thanks man, that works great. I've been stuck on that for 2 days.

Posted: Fri Oct 03, 2003 6:00 pm
by Rob
sorry, im new to using SQL and php...why wont this work to display the stuff?


$coolguy = $_SESSION['loggedin'];
$usertoget = "SELECT id FROM usersys WHERE user='$coolguy'";
$usertoget1 = MYSQL_QUERY("$usertoget");
$userdata = mysql_fetch_array($usertoget1);
$sql = "select pro_name, pro_mail, pro_location, pro_misc from usersys where ID=1 LIMIT 1;";
MYSQL_QUERY("$sql");

Posted: Fri Oct 03, 2003 6:22 pm
by qads
you are checking to see if they are any rows with those values in selected columns, if it finds it, it will just say something like resource ID #4 etc, so you need to disaply the data:

Code: Select all

<?php
$coolguy = $_SESSION['loggedin']; 
$usertoget = "SELECT id FROM usersys WHERE user='$coolguy'"; 
$usertoget1 = MYSQL_QUERY("$usertoget"); 
$userdata = mysql_fetch_array($usertoget1); 
$sql = "select pro_name, pro_mail, pro_location, pro_misc from usersys where ID=1 LIMIT 1;"; 

$ask_mysql = MYSQL_QUERY("$sql");//mysql_query is to ask
$show_data = mysql_fetch_array($ask_mysql);//mysql_fetch_array is to show

//now to output whatever it is in the columns to user.
echo "$show_data[pro_name], $show_data[pro_mail], $show_data[pro_location], $show_data[pro_misc]";

?>

Posted: Fri Oct 03, 2003 7:28 pm
by JAM
To explain further or at least give you another point to look at it, try using qads example...

Code: Select all

$show_data = mysql_fetch_array($ask_mysql);//mysql_fetch_array is to show
...and right after that add the following:

Code: Select all

echo "<pre>"; // to give the following printout a good look
print_r($show_data); // print out the array you just fetched
Just perhaps, you will see how the search result look after retrieving it using mysql_fetch_array(). The order that you select will be the order of the keys in the array. You'll also see numbers, that are the equilant to the key's name.

Now try and see what happens if you select two users from your table ("... where id < 3" might be a way).

Edit: Sorry... Better? ;)

Posted: Fri Oct 03, 2003 9:58 pm
by qads
yea, what jam said...


and its qads..why do people get it wrong all the time?..and my real name is even harder to say :lol:

Posted: Fri Oct 03, 2003 10:34 pm
by Cruzado_Mainfrm
qads wrote:

Code: Select all

<?php
//now to output whatever it is in the columns to user.
echo "$show_data[pro_name], $show_data[pro_mail], $show_data[pro_location], $show_data[pro_misc]";
?>
in the echo you should place { } around the variables, if not, the variables will not be printed out... ex.:

Code: Select all

<?php
echo "{$show_data[pro_name]}, {$show_data[pro_mail]}, {$show_data[pro_location]}, {$show_data[pro_misc]}";
?>
a little side note, global variables doesn't seem to work for me in Heredoc, so maybe that's the same for everybody, even if you use {}...

EDIT: don't use quotes like in: mysql_query("$query"); , just write mysql_query($query); ... you are saving 2 bytes for every query that u have made :wink: