Jump to content

[SOLVED] MySQL fetch array / query problem ????


ciaran

Recommended Posts

hey everybody , I am having a problem , I am building a forum application and I nearly have it completed , but I have a problem. The code below, should display the user's personal information and the comment's left by all the users. I have check it in the database and all the info is there and nothing wrong with the actaully code connect ups as I have tested with echo. here is the errorr I get.

 

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

 

Hope some1 can help me , thanks

 

<?php
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$threadid = $_GET['threadid'];

// Retrieve all the data from the "$threadid" table
$result3 = mysql_query("SELECT * FROM $threadid")
or die(mysql_error());

$row3 = mysql_fetch_array( $result3 );

////////////////// user id get end///////////////////


// Retrieve all the data from the "login" table
$result2 = mysql_query("SELECT * FROM login WHERE id ='$row3[userid]'")
or die(mysql_error());

while($row2 = mysql_fetch_array( $result2 )) {
// Print out the contents of each row into a table
echo "<tr><td width=\"100\">$row2[userid]</td>\n";}

////////////////////////User Id col extract information///////////////////////////////////////////


// Retrieve all the data from the "$threadid" table
$result = mysql_query("SELECT * FROM $threadid")
or die(mysql_error());


while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<td align=\"top\">$row[comment]</td></tr>\n";}

echo "</table></div>\n";

echo "<p id=\"menu2\">
<a class=\"menlinks\" href=\"addreplyform.php?threadid=$threadid\">Add Reply</a>    
<a class=\"menlinks\" href=\"#\">Report Aduse</a>    
<a class=\"menlinks\" href=\"#\">Time</a>    
</p>\n";?>

Link to comment
Share on other sites

Thanks for your input but....

 

$threadid is a variable yes and I did what you said but still no joy. Getting this erorr now

 

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''thread31867'' at line 1"

 

thread31867 is the name of a table in the database.

 

Kind Regards

Ciaran Mc Cann

Link to comment
Share on other sites

unfortunatley PHP / MySql does not agree with you, it is saying that it does not understand either the table name or the syntax of the query, did you use the syntax as I put it in my post?? is so then its the table name it does not like, have you connected ok to the correct database name?

 

can you do me a favour, instead of executing the query can you echo out the query for me

 

echo "SELECT * FROM '$threadid' ";

 

to see if it is properly constituted

Link to comment
Share on other sites

not sure what you mean me to do , will you post the code you would use, do I just use what you post and the

 

$threadid = $_GET[uesrid]; or what.

 

It connects to the database fine on the first piece of information added to it , but then when you add more its throws out that error.

Link to comment
Share on other sites

I am just after fixing it. here is the code I used below , just put {} around the variable and it works fine.

 

$result3 = mysql_query("SELECT * FROM {$threadid} ")

 

Thanks for your help anyway

Kind Regards

Ciaran Mc Cann

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.