Forum Moderators: coopster
My questions...
1. Do I need to set up a relationship in the database like I do in Access? Also it would have to be a one to many (2) if I do.
My knowledge is limited, but I'm learning by doing...
2. I keep thinking the the query would like something like:
select * from table2 where table2.'State Rep' = table1.'State' order by table2.'Name'
which would display the 2 records from table 2 that have the state that was selected from table 1.
Does that look right?
If anyone has any good sources to look at for pulling data from a table and displaying it through php, I am completely open to receiving that. I just don't know exactly what I need to start with, other then the limited knowledge I have.
on first page load you pull all the states from your states table
the user selects one
at this point, as long as you have set up the relationship in the db and the info in your 'State Rep' (that shouldn't have spaces in it change to no space or underscore) column are only values from your state table then you can just grab from there
SELECT * FROM table2 WHERE State_Rep = 'selectedstate';
that would grab your 2 records where 'selectedstate' is the value selected from the dropdown by the user.
for some tutorials you could try our very own PHP Library [webmasterworld.com]
maybe this one
Extracting Data from MySQL using PHP [webmasterworld.com]
it would be
SELECT DISTINCT columnname FROM tablename
for the second part
you need to keep the database and its relational logic seperate from the display stuff. Yes, the relations are used by the display but you can do whatever you want with the data once you are on the php side of things.
pull the addresses, put them in an array
pull the letter and out it into a variable
output them later as you like
<?php
mysql_select_db($database_site, $site);
$query_senators_results = "SELECT * FROM senators";
$senators_results = mysql_query($query_senators_results, $whoelse) or die(mysql_error());
$row_senators_results = mysql_fetch_assoc($senators_results);
$totalRows_senators_results = mysql_num_rows($senators_results);
?><?
$host = "localhost";
$user = "user";
$pass = "password";
$dbname = "dbname";$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);$sql = "select * from senators where 'state'='" . $_POST['state']. "'";
echo $sql;$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) { echo "<p>",$row['state'];
}?>
Im clicking submit on page1.php and on page2.php I get the following error:
select * from senators where 'state'=''
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /path/public_html/senators-letter.php on line 30
my page1.php form is pulling the data (using DISTINCT ;P ) fine from the table put for some reason I keeps bombing out. I have gone through the threads and cannot see it( don't move until you see it...quote from bobby fisher movie i can't think of at this moment.)
Can anyone else see it?
$sql = "select * from senators where 'state'='" . $_POST['state']. "'";
To
$sql = "select * from senators where state ='" .[url=http://us2.php.net/manual/en/function.mysql-real-escape-string.php]mysql_real_escape_string[/url]($_POST['state']). "'";
[edited by: eelixduppy at 3:08 am (utc) on July 26, 2006]
$query = mysql_query($sql) or die(mysql_error());
select * from senators where state =''No Database Selected
Thats the error im getting now.
<?
$host = "localhost";
$user = "user";
$pass = "password";
$dbname = "contact";$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($contact);$sql = "select * from senators where state ='" .mysql_real_escape_string($_POST['state']). "'";
echo $sql;
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($query)) { echo "<p>",$row['state'];
}?>
so in my cpanel, i created a db called contact
in cpanel it shows as jake_contact
should i be putting the db name as contact, or jake_contact
my username is jake_admin, and that allows me access but when i change the db name to jake_contact also... i get the error
edited code by me
$dbname = "jake_contact"; Also, make sure your username and password is correct in your script. By the way, * selects all the cols in a table.
Good luck!
the select db should be:
mysql_select_db($dbname);
Your code says (i am new to this and couldn't find how to grab a snippet of previous post):
$dbname = "contact";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($contact);
mysql_real_escape_string($_POST['state'])
looks like either mysql_real_escape_string returning empty string or the $_POST['state'] doesn't contain anything. i think your usage of the mysql_real_escape_string is correct. it is also being called only after the connection to the database has been established.
i would put a debug statement to check if there is any value in the $_POST. i would re-write your code in the following way:
<?
echo 'STATE:' . $_POST['state'] . '<br>';$host = "localhost";
$user = "user";
$pass = "password";
$dbname = "contact";$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
if(!$connection)
{
echo 'ERROR: Could not connect to Database<br>';
}mysql_select_db($contact);
$sql = "select * from senators where state ='" .mysql_real_escape_string($_POST['state']). "'";
echo $sql;
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($query)) { echo "<p>",$row['state'];
}?>
if the state value is not present check the select statement in the previous html file and see if the field name you have given is state or something else.
another way to debug is to put '?state=CA' to the url and use $_GET in the code. this will atleast help you figure out if your mysql and php code is working properly or not.
HTH
$sql = "select * from senators where state ='" .mysql_real_escape_string($_GET['state']). "'";
http://www.example.com/senators-letter.php?state=CA
I still got the error.
while ($row = mysql_fetch_array($query)) { echo "<p>",$row['state'];
so you are getting exactly what you asked for.
When I am doing my first run in a query like this I often have my fetch array loop doing this
while ($row = mysql_fetch_array($query)) {
echo '<p><pre>';
print_r($row);
echo '</pre>';
}
then I can look at the full dump of each row and it helps me know what vars I need to output when doing my formatting.
select * from senators where state ='IL'from displaying on the page
Instead I am getting
person1
person2
person2
person1
I tried taking echo out of
while ($row = mysql_fetch_array($query)) { echo "<p>",$row['state'];
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");if(!$connection)
{
echo 'ERROR: Could not connect to Database<br>';
}mysql_select_db($dbname);
$sql = "select * from senators where state ='" . $_POST['state']. "'";
//echo $sql;
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($query)) {echo "<p>",$row['first_name'], " ",$row['last_name'], "<br>", $row['address'], "<br>",
$row['city'], ", ", $row['state'], " ", $row['zip'], "<BR>", $row['website'], $row['email'];
}?>
</body>
</html>
<?php
mysql_free_result($senators_results);
?>
just getting the duplicate results mirrored but getting results none the less. thanks for everyones help.