newbie question about multiple queries


Posted on 16th Feb 2014 07:03 pm by admin

hi everyone,
I'm sorry to ask such a basic question, but I'm young and trying to learn php on my own. I bought the PHP6 Bible and I'm doing a tutorial on multiple-queries.

I followed the book exactly, but I'm not getting the right result.
here's my code:

Code: [Select]<?php require_once('Connections/mydb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

?>
<?php

function display_cities($database_mydb, $mydb)
{
mysql_select_db($database_mydb, $mydb);
$country_query = sprintf("Select 'id', continent, countryname from country order by continent, countryname");
$country_result = mysql_query($country_query, $mydb) or die(mysql_error());

print("<table border=1>n");
print("<tr><th>continent</th><th>country</th><th>cities</th></tr>");

while ($country_row = mysql_fetch_row($country_result))
{
$country_id = $country_row[0];
$continent = $country_row[1];
$country_name = $country_row[2];

print("<tr style='background:red'align=left valign=top>"); print("<td>$continent</td>"); print("<td>$country_name</td>");

print ("<td style='background:blue'>");

$city_query = "select cityname from city where countryID = $country_id order by cityname"; //<----Something with this line is throwing it off!
$city_result = mysql_query($city_query, $mydb) or die(mysql_error());

while ($city_row = mysql_fetch_row($city_result))
{
$city_name = $city_row[0];
print("$city_name
");
}
print("</td></tr>");
}
print("</table>n");
}
?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
display_cities($database_mydb, $mydb)
?>

</body>
</html>

I've narrowed my problem down to this line "$city_query = "select cityname from city where countryID = $country_id order by cityname";" but that's how the book shows it. But, when I change that line of code to something like "$city_query = "select cityname from city where countryID = 4 order by cityname", then I get all of the cities with a countryID of 4, but they ALL print over and over again in each <td> for the duration of the loop.

I have about 15 entries in the "city" database, and they're all supposed to be echoing into the table, inline with their corresponding country.

This has been driving me nuts for hours, and I've been searching online for an answer. I'm sure I'm overooking something very simple, but please go easy on me, this is my first time attempting this! If someone sees the problem, can you please let me know?

thanks

235 Like 19 Dislike
Previous php-forum Next php-forum
Other php-forum

Do not allow posting of whitespace
Currently the script below works if the user does not type a name/message, but if i create a whitesp

Multiple Do / While Statements?
I'm still very new to PHP, and running in to a problem when trying to execute a do/while loop inside

Array help
Hello i got this code to fetch data from database but it is not working it displays
7
Array

upload image name with extension using php
hi frds..

<input id="file1" type="file" name="file[]" &a

Using two $_POST Function / Switch () statements, second does not work.
Hi all. I’m new to php and am having a problem getting $_POST Function / switch () to work. I

Sessions work for me and not others?
I'm having a bit of bad luck with sessions. In the past they have worked fine for me, but this time

filesize problem
i use ubuntu 9.10 and i have mountet a folder from an windows

/mount/FOLDER
/mount/FOLDER/

Text to picture Generator
Hello, i have found this script and it works really good^^ But i have one problem, i would like to c

Problems with array
This code is the last lines in an upload script that uploads 4 images at the same time and writes th

Is there a function ... auto send IP
Is there some function that can be sent to a ddns service that returns the ip address of a ddns forw

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash