Jump to content

[SOLVED] Trouble Calling From Database


refiking

Recommended Posts

I am trying to call the analyst_id from the Analysts table and store it on the Records table.  Here is my code:

 

$analyst = mysql_query(" SELECT `analyst_id` FROM `Analysts` WHERE username = '$username'");

$que = mysql_query($analyst);

while($row = mysql_fetch_assoc($que){

echo $row["analyst_id"]."<br />\n";

}

 

 

What am I doing wrong here?

Link to comment
Share on other sites

I am now getting the following error message:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/ctpwebco/public_html/leads/proc.php on line 64

 

 

Line 64 is the following:

 

while($row = mysql_fetch_assoc($que)){

Link to comment
Share on other sites

I looked, but I didn't see anything.  Here are lines 63 - 68:

 

$analyst = mysql_query(" SELECT `analyst_id` FROM `Analysts` WHERE username = '$username'");

$que = mysql_query($analyst);

while($row = mysql_fetch_assoc($que)){

echo $row['analyst_id']."

\n";

}

Link to comment
Share on other sites

First, is your table called `Analysts` and not `analysts`? Yes, case matters!

 

Second, try changing this line:

$analyst = mysql_query(" SELECT `analyst_id` FROM `Analysts` WHERE `username` = '".$username."'");

 

Also your "echo" statement try keeping the code on one line:

echo $row['analyst_id']."\n";

 

Try that and tell me how it goes.

Link to comment
Share on other sites

See this bit?

supplied argument is not a valid MySQL

 

That is telling you that the error is your SELECT query. Remove the space before the "SELECT" bit:

$analyst = mysql_query("SELECT `analyst_id` FROM `Analysts` WHERE `username` = '".$username."'");

 

Try that...

Link to comment
Share on other sites

My bad - I'm tired as its almost 3am here...

 

$que = mysql_query("SELECT `analyst_id` FROM `Analysts` WHERE username = '$username'");
while($row = mysql_fetch_assoc($que)){
echo $row['analyst_id']."\n";
}

 

I just noticed that you're using mysql_query() twice!!! Use the above code and see if it works.

Link to comment
Share on other sites

Yes.  I think we are saying the same thing.  I want to take the analyst's personal id and place it on each one of their records they are responsible for.  There are two tables.  Analysts and Records.  The Analysts table just stores the info about the analysts.  The Records table stores the info about the records.  Did I make it clear or make it more confusing?

Link to comment
Share on other sites

That sounds OK to me so I'll give it a shot...

 

Above you've got your code to pull data from the table `Analysts` so now we need to insert that into the table `Records`. Something like this would work:

$que = mysql_query("SELECT `analyst_id` FROM `Analysts` WHERE username = '$username'");
while($row = mysql_fetch_assoc($que)){
  mysql_query("INSERT INTO `Records` VALUES (`analyst_id`) VALUES ('".$row['analyst_id']."')");
}

This presumes you have a field called "analyst_id" defined in your `Records` table.

Link to comment
Share on other sites

Check that analyst_id in both tables are of the same type (ie. SMALLINT or TINYINT)

 

That's all I can think of. You can try adding the "echo" statement back into the loop so you can see what data is being pulled out of the `Analysts` table.

Link to comment
Share on other sites

Wait a second!!!  I almost forgot. I am adding the other fields to the same record later in the page (around 250 lines later).  Does that make a difference?

 

The ID is not an INT.  It's a VARCHAR.  When I did the echo, it came right up with no problem.

Link to comment
Share on other sites

VARCHAR shouldn't matter except that you can't take VARCHAR and insert it into an integer data type if the value is not a number. For example, 12 would go into integer but 12a wouldn't.

 

The later lines shouldn't make a difference because you're still adding records to a table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.