Solved

how to write a simple query

Posted on 2009-05-19
5
198 Views
Last Modified: 2012-05-07
hi

i'm not gettng an error but i'm getting no result with this -what am i doing wrong?

$query_rsSubMenu = "SELECT menu_item_title FROM menu_items WHERE page_parent_id ='50'";

see code 4 how i'm using it:
else :
				$query_rsSubMenu = "SELECT menu_item_title FROM menu_items WHERE page_parent_id ='50'";
				
				
				echo "\t<li style=\"background:url(images/photobutton_" . $value['id'] . ".jpg) no-repeat;\"><a ".($subpage=="management" && $value['id']=="50" ? 'class="active"' : ''). "   href=\"" . menus::checksef($url) . "\" $target $active tabindex=\"" . $ak->tabindex($key) . "\" onMouseover=\"dropdownmenu(this, event, menu" . $value['id'] . ", '192px')\" onMouseout=\"delayhidemenu()\">" . $value['title'] . "</a></li>\n"; 
				if ($value['id']=="50") //this is for that tricky 2nd level submenu
				echo "<ul class=\"secMenu\">
							<li>".$row_rsSubMenu['menu_item_title']."</li>
						</ul>";
			endif;

Open in new window

0
Comment
Question by:phillystyle123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:termlimit
ID: 24428314
You are setting the query, but it doesn't appear that you have executed the query.  All you have is the query statement, but no mysql_query statement to actually use the query.
0
 
LVL 2

Expert Comment

by:miyosia
ID: 24428318
I think the problem is you are only defining the query but not actually connecting to database and retrieving the data. I am not sure if you already have a code connecting to the database but assuming you already established connection prior to the code
$query_rsSubMenu = "SELECT menu_item_title FROM menu_items WHERE page_parent_id ='50'";

you then need to pass this value to the mysql_query function which retrieved the data for you. This function returns the data array so you can directly assign it to a variable like this:

$result =  mysql_query($query_rsSubMenu, $link); where $link is the connection established to the database.

you can then retrieve the per row value from the $result using $row = mysql_fetch_assoc($result)) where $row is also an array containing all data for each database row retrieved. to access the value for each data you can use the database entry name or the array index (arrangement is the same as how these data are arranged int he database)

Example: echo $row['entryID']; where entryID is a database column name..

you can try using this simple example to be able to run query in PHP.
<?php
if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
    echo 'Could not connect to mysql';
    exit;
}
if (!mysql_select_db('mysql_dbname', $link)) {
    echo 'Could not select database';
    exit;
}
$sql    = 'SELECT foo FROM bar WHERE id = 42';
$result = mysql_query($sql, $link);

if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
while ($row = mysql_fetch_assoc($result)) {
    echo $row['foo'];
}
mysql_free_result($result);
?>
0
 

Author Comment

by:phillystyle123
ID: 24428323
hmm - i don't get it - i've got this:

$query_rsSubMenu = "SELECT menu_item_title FROM menu_items WHERE page_parent_id ='50'";

and then i'm outputting it here:

<li>".$row_rsSubMenu['menu_item_title']."</li>

what am i doing wrong?


0
 
LVL 2

Accepted Solution

by:
miyosia earned 500 total points
ID: 24428392
you are missing the part where you have to retrieve the data..

this code $query_rsSubMenu = "SELECT menu_item_title FROM menu_items WHERE page_parent_id ='50'";
is just assigning the query to the $query_reSubMenu variable. to be able to actually retrieve the values you need
$result =  mysql_query($query_rsSubMenu, $link); where $link is derived from $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') <- this establishes connection to the database

then to output the datause this code
while ($row_rsSubMenu = mysql_fetch_assoc($result)) {
    echo "<li>".$row_rsSubMenu['menu_item_title']."</li>"
}

0
 

Author Closing Comment

by:phillystyle123
ID: 31583331
thank you!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question