Jump to content

Recommended Posts

Hey all!

 

Im trying to increase my skills in PHP and I am trying to code a menu in php that will fill with information gathered from a mysql database.

 

I know how to make the table, but I am trying to write the functions to use and I cant seem to write the functions correctly.

 

If you guys could help that would be awesome!

 

Here is my code :

 

<?php
//*** Start Session ***\\
session_start();

//*** Variables For MySQL Connection ***\\
$db_user = "root";
$db_pwd = "";
$db = "project";

//*** Variables ***\\
$about = 'about'; // about table
$services = 'services'; // services table
$support = 'support'; // support table

//*** Functions ***\\
function mysqlConnect($db_user, $db_pwd, $db) {
	$link = mysql_connect( "localhost", "$db_user", "$db_pwd");     // connects to mysql
	@mysql_select_db( $db );								        // selects the database 
}

function aboutMenu($about, $aboutResult) {
	$aboutResult = mysql_query("SELECT * FROM ($about)");           // selects about table
	$aboutFields_num = mysql_num_fields($aboutResult);              // grabs number of fields from about table
}

function servicesMenu($services, $servicesResult) {
	$servicesResult = mysql_query("SELECT * FROM ($services)");     // selects services table
	$servicesFields_num = mysql_num_fields($servicesResult);        // grabs number of fields from services table
}

function supportMenu($support, $supportResult) {
	$supportResult = mysql_query("SELECT * FROM ($support)");       // selects support table
	$supportFields_num = mysql_num_fields($supportResult);          // grabs number of fields from support table
}
?>

 

This is a file called "menu.php" and I will be including it in another file called "index.php". If needed, I can post the content of "index.php"

Link to comment
https://forums.phpfreaks.com/topic/206205-im-having-some-serious-issues/
Share on other sites

Also, you seem to have some confusion regarding what a function's argument list actually is.  The variables listed in an argument list are passed into the function.  Your various result variables (e.g., $aboutResult) don't exist outside of the function, but are rather created within the function.  Since they're not being passed into the function, they shouldn't be listed as an argument (and you'll probably get an error about passing an undefined variable into the function if you don't fix it).

 

You should have functions that look like:

 

function aboutMenu($about)
{
   $aboutResults = mysql_query("SELECT * FROM $about");
   $numAbout = mysql_num_fields($aboutResults);

   // continue processing, or return a value
}

Oh! So I had kind of the right idea. If I wanted to return something, I would have it be like this :

 

function aboutMenu($about) {
$aboutResult = mysql_query("SELECT * FROM $about");
$aboutFields_num = mysql_num_fields($aboutResult);
        return $aboutFields_num;
}

 

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.