First things first.. i am new to php and mysql. I have tried a couple of different pages to do the same thing. i have narrowed my lack of understanding down to the variable used in the query string. i have gone through a few tutorials and compared my code to others. i ahev taken working examples and added my variables.
i am missing something that i'm sure the more experienced people will see right off. i have a small table i am trying to use for a test before putting the code in a live page. some of you may recognize part of the code from one of the tutorials.
when i use the static number value, i get a table with the supporter id and the group id's 3 belongs to. What goes wrong is i get an empty table when i put in the variable $supporter_id
the one that works is this:
<?php
// db stuff
$mysql_host = "localhost";
$mysql_user = "username";
$mysql_pwd = "pwd";
$mysql_db = "support";
// db table variables
$mysql_supporters_table = "supporters";
//query to match the supporter id with any group it is a member of
$supporter_query = "SELECT * FROM $mysql_supporters_table WHERE supporter_id=3";
// Connecting, selecting database
$link = mysql_connect($mysql_host, $mysql_user, $mysql_pwd)
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db($mysql_db) or die('Could not select database');
//user stuff
$supporter_id = "3";
// Performing SQL query
//$query = 'SELECT * FROM supporters';
$result = mysql_query($supporter_query) or die('Query failed: ' . mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Group I.D.</th> <th>Supporter I.D.</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['group_id'];
echo "</td><td>";
echo $row['supporter_id'];
echo "</td></tr>";
}
echo "</table>";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
the one that doesn't work is this, switching out the number 3 for the variable in $supporter_query:
<?php
/ this document opens the "support" db, grabs all of the groups and puts them in a table, checks the
membership of the "c1_staff" supporter and checks the boxes c1_staff is already a member of. if the checked
boxes change, it updates the membership of the supporter when clicking "Submit".
/
// db stuff
$mysql_host = "localhost";
$mysql_user = "username";
$mysql_pwd = "pwd";
$mysql_db = "support";
// db table variables
$mysql_supporters_table = "supporters";
//query to match the supporter id with any group it is a member of
$supporter_query = "SELECT * FROM $mysql_supporters_table WHERE supporter_id=$supporter_id";
// Connecting, selecting database
$link = mysql_connect($mysql_host, $mysql_user, $mysql_pwd)
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db($mysql_db) or die('Could not select database');
//user stuff
$supporter_id = "3";
// Performing SQL query
//$query = 'SELECT * FROM supporters';
$result = mysql_query($supporter_query) or die('Query failed: ' . mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Group I.D.</th> <th>Supporter I.D.</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['group_id'];
echo "</td><td>";
echo $row['supporter_id'];
echo "</td></tr>";
}
echo "</table>";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
i have tried surrounding the $supporter_id variable with single quotes and double quotes, both quote types with periods on either side, periods inside single quotes inside double quotes. putting the 3 in double/single quotes and no quotes.
you can see i have spent a few minutes on this
any help would be appreciated...and if you see something i could be doing different or better that's also appreciated
thanks in advance😃