mySQL code problem (php)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Rob
Forum Commoner
Posts: 33
Joined: Fri Oct 03, 2003 3:18 pm

mySQL code problem (php)

Post 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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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]";
?>
User avatar
Rob
Forum Commoner
Posts: 33
Joined: Fri Oct 03, 2003 3:18 pm

Post by Rob »

Thanks man, that works great. I've been stuck on that for 2 days.
User avatar
Rob
Forum Commoner
Posts: 33
Joined: Fri Oct 03, 2003 3:18 pm

Post 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");
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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]";

?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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? ;)
Last edited by JAM on Fri Oct 03, 2003 10:02 pm, edited 1 time in total.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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:
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post 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:
Post Reply