mySQL code problem (php)
Moderator: General Moderators
mySQL code problem (php)
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
$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
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:
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]";
?>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");
$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");
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]";
?>To explain further or at least give you another point to look at it, try using qads example...
...and right after that add the following:
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?
Code: Select all
$show_data = mysql_fetch_array($ask_mysql);//mysql_fetch_array is to showCode: Select all
echo "<pre>"; // to give the following printout a good look
print_r($show_data); // print out the array you just fetchedNow try and see what happens if you select two users from your table ("... where id < 3" might be a way).
Edit: Sorry... Better?
Last edited by JAM on Fri Oct 03, 2003 10:02 pm, edited 1 time in total.
-
Cruzado_Mainfrm
- Forum Contributor
- Posts: 346
- Joined: Sun Jun 15, 2003 11:22 pm
- Location: Miami, FL
in the echo you should place { } around the variables, if not, the variables will not be printed out... ex.: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]"; ?>
Code: Select all
<?php
echo "{$show_data[pro_name]}, {$show_data[pro_mail]}, {$show_data[pro_location]}, {$show_data[pro_misc]}";
?>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