Jump to content

Recommended Posts

Hi Freaks!

I'm a beginner on PHP and mySQL and I need your help with writing a query more efficiently.

 

I am querying data from a mySQL database resulting in alphabetical lists of persons. What I do (and I am sure, there is a more elegant way for it) is repeating the following query for every list. Is there a way to write the query only once?

Your help is very much appreciated!

 

The page with the query can be found under

crimezzz dot net slash avis slash index dot php

 

=====START CODE=====

<?php
$db = mysql_connect("localhost", "dbxxxxx", "xxxxx");
mysql_select_db("xxxxx",$db);

# How many entries
$result = mysql_query("SELECT * FROM person WHERE 0100_lastname LIKE \"A%\" ORDER BY 0100_lastname ASC",$db);
$num = mysql_num_rows($result);
echo "<h4>$num Entries</h4>\n";
# Ende Wieviele Einträge

# Start query
if ($myrow = mysql_fetch_array($result)) {

  do {
  $forwardid = $myrow["forward_noyes"];

# IF forward
  if ($forwardid == 2) {
$string = $myrow["0120_case_code"];
$folderv = $string{0};
printf ("<li><span class=\"fwd\">></span> %s %s =><br /><a class=\"forward\" href=\"http://www.crimeZZZ.net/serialkillers/%s/%s.php\">%s</a></li>\n", $myrow["0100_lastname"], $myrow["0110_firstname"], $folderv, $myrow["0120_case_code"], $myrow["0120_case_code"]);
  }

# STANDARD Eintry
  else {

//Query Color Gender aus source_gender, in Übereinstimmung mit Ziffer aus 0300_gender
$genderid = $myrow["0300_gender"];
$gender = mysql_query("SELECT * FROM source_gender WHERE id = $genderid");
	if ($rowgender = mysql_fetch_array($gender)) {
	do { printf ("<li><span class=\"%s\">\n", $rowgender["gender"]);
	} while ($rowgender = mysql_fetch_array($gender));
	} else { printf ("");
}

# Query recent_status
$statusid = $myrow["0600_recent_status"];
$status = mysql_query("SELECT * FROM recent_status WHERE id = $statusid");
$rowstatus = mysql_fetch_array($status);
$string = $rowstatus["name"];
$stringfirst = $string{0};
printf ("%s</span>\n", $stringfirst);

$string = $myrow["0120_case_code"];
$folderv = $string{0};
    $string = $myrow["0300_gender"];
$genderv = $string{0};

printf ("<a href=\"http://www.crimeZZZ.net/serialkillers/%s/%s.php\" class=\"gender%s\">%s %s</a> <img src=\"images/flags/%s.png\" alt=\"%s\" /> \n", $folderv, $myrow["0120_case_code"], $genderv, $myrow["0100_lastname"], $myrow["0110_firstname"], $myrow["1601_crcounty1"], $myrow["1601_crcounty1"]);
printf ("<span class=\"suspected\">%s</span></li>\n", $myrow["1830_victims_suspected"]);

}
              
} while ($myrow = mysql_fetch_array($result));
  
} else {
echo "";	
}
?>

=====END OF CODE=====

 

added code tags ~ CV

Link to comment
https://forums.phpfreaks.com/topic/106945-solved-help-with-mysql-query/
Share on other sites

You could make a loop....? I think that's what you're wanting to do...right? I'm seeing several queries in this code, so are you trying to somehow condense all that code into one query, or are you wanting to repeat that entire block of code over and over again? Need more info please.

i'm not sure if i'm on the right tracks, but i looked at your site and noticed that you have created a separate PHP file for each

'letter', he's a couple of code snips if this is your issue, otherwise please advise:

<?php

// this creates A-Z links for your navigation

$letters = range('a','z');
foreach( $letters as $letter )
{

	print <<<_LINK
	<a href="xserienkiller?letter=$letter">{$letter}</a>
_LINK;

}
?>

 

<?php
$letter =@ substr($_REQUEST['letter'], 0, 1); // force only 1 character allowed

// this would create the sql to look it up
if ( $letter <> '' )
{

	// How many entries
	$result = mysql_query("SELECT * FROM person WHERE 0100_lastname LIKE \"{$letter}%\" ORDER BY 0100_lastname ASC",$db);	

	// ... rest of code here ...

}

?>

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.