Solved

how to write a simple query

Posted on 2009-05-19
5
192 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Button Click 11 26
Not needed 13 58
PHP loop not working 4 33
php namespace question with Twilio 4 16
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now