/    Sign up×
Community /Pin to ProfileBookmark

PHP MYSQL Question.

Hello everyone. Have a good question for all of you. I want to display info from my MYSQL database. This database contains a user’s email, first name, and three different locations named place1, 2, and 3. I want to pull the info and write it to the page for other’s to read. Simple enough right? Well, I got that part down fine, but here is where it gets tricky. I want to separate the results in the table by alphabetical order so that I can make name anchors for direct linking to each individual letter. For example, I want to pull all of the ‘a’ emails together and array their results, b so on. Also, when I print the information to the page, I want it to display all results for each letter, not just the first entry. I have my sample code here. I get it to search the table once for the first query, however since I need 26 different letters, I have to do 26 different queries. It won’t query for the second letter and so on down the list. Then, since I’ve been testing my code by using only the letter a and multiple simple mysql entries, it only displays the first entry in the ascending order, not all of the entries.

<?
session_start();
mysql_connect(“localhost” , “**********” , “*******“)
or die(‘Database is not responding.’);
mysql_select_db(“*
**
****“) or die(‘Unable to connect to database’);
/*

$match=”SELECT first_name2, place1, place2, place3, email FROM placelists WHERE first_name LIKE ‘a%'”;
$qry = mysql_query($match)
or die(‘Could not match data because of database error’ . mysql_error());;
$num_rows = mysql_num_rows($qry);
while ($row = mysql_fetch_array($qry)) {
if ($row == “”) {
$row == “No Places listed under this letter.”;
} elseif ($row !== “” ) {
$row == mysql_fetch_array($qry);
$first_name2 = $row[‘first_name2’];
$place1 = $row[‘place1’];
$place2 = $row[‘place2’];
$place3 = $row[‘place3’];
$email = $row[’email’];
}
}
mysql_close();
echo $first_name2;
echo $place1;
echo $place2;
echo $place3;
echo $email;
I also want to display the above text “No Places listed under this letter”, when there is no data lsited under that letter in the MYSQL table. It just displays a blank page when I try to test a letter that isn’t in the database.
Each letter would be approached the same way, except when it comes to variables each one will be given a corresponding number a=row b=row2 etc. I know it’s a lot, but can someone help me on the path to getting this to work? Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@holidayDec 08.2007 — I haven't tested it, but I think it might do what you're trying to do.

[code=php]
<?php

session_start();
mysql_connect("localhost" , "**********" , "*******")
or die('Database is not responding.');
mysql_select_db("*******") or die('Unable to connect to database');


//make query
$results = mysql_query("SELECT first_name2, place1, place2, place3, email FROM placelists ORDER BY email");

//count rows in query
$num_rows = mysql_num_rows($results);

//set letters var
$letters = "abcdefghijklmnopqrstuvwxyz";

//set on letter and first of var
$on_letter = 0;
$first_of = true;

//for each row in query
while ($row = mysql_fetch_array($results)) {

//get first letter of email address
$letter_of_email = strtolower(substr($row['email'], 0, 1));

$go = true;

while($go) {
$on_letter_val = substr($on_letter, 0, 1);

if($first_of) {
$first_of = false;
echo "email addresses that start with: " . $on_letter_val ) . "<br />n";

if($letter_of_email!=$on_letter_val ) {
echo "No emails listed under this letter."; . $on_letter_val ) . "<br />n";
}
}

if( $letter_of_email==$on_letter_val ) {
$go = false;

echo $row['first_name2'];
echo $row['place1'];
echo $row['place2'];
echo $row['place3'];
echo $row['email'];
} else {
$on_letter++;
$first_of = true;
}
}


}

mysql_close();


?>
[/code]
×

Success!

Help @tcsnguy07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...