Solved

how to write a simple query

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

920 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

13 Experts available now in Live!

Get 1:1 Help Now