We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,473 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how change mysqli_connect to mysql_connect and mysqli_fetch_assoc

i have this connection

<?php
    $db_host = 'localhost';
    $db_user = 'root';
    $db_pass = '';
    $db_name = 'databasename';      

    $conn = mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
        mysql_select_db($db_name,$conn);
mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');  
?>

but my old connection dosen't work with this code

//connect to database
$link = mysqli_connect('localhost','root','');
mysqli_select_db($link,'databasename');


//get all rows
$query = mysqli_query($link,'SELECT * FROM categories');
while ( $row = mysqli_fetch_assoc($query) )
{
    $menu_array[$row['catid']] = array('catname' => $row['catname'],'parentid' => $row['parentid']);
    $menu_array[$row['catid']] = array('catname' => $row['catname'],'parentid' => $row['parentid'],'catid'=>$row['catid']);
}

//recursive function that prints categories as a nested html unorderd list
function generate_menu($parent)
{
    $has_childs = false;
    //this prevents printing 'ul' if we don't have subcategories for this category

    global $menu_array;
    //use global array variable instead of a local variable to lower stack memory requierment

    foreach($menu_array as $key => $value)
    {
        if ($value['parentid'] == $parent) 
        {   
            //if this is the first child print '<ul>'            
            if ($has_childs === false)
            {
                //don't print '<ul>' multiple times              
                $has_childs = true;
                echo '<ul>';
            }
            echo '<li><a href="category.php?catid='. $value['catid'] . '">' . $value['catname'] . '</a>';
            generate_menu($key);
            //call function again to generate nested list for subcategories belonging to this category
            echo '</li>';
        }
    }
    if ($has_childs === true) echo '</ul>';
}

I think My erro located between this code .. Finally i need just replace mysqli_connect to mysql_connect Without I

while ( $row = mysqli_fetch_assoc($query) )
{
    $menu_array[$row['catid']] = array('catname' => $row['catname'],'parentid' => $row['parentid']);
}
4
Contributors
5
Replies
7 Hours
Discussion Span
8 Months Ago
Last Updated
6
Views
afroo.gfx
Newbie Poster
4 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

THERE is differenct between parameter passing in mysql and mysqli. Link is passed later in mysql and passed as first parameter in mysqli.
so refer manual of mysql and replace parameter position where applicable

urtrivedi
Posting Virtuoso
1,719 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

@urtrivedi
thanks for you but i'm try to do it but i do'nt understand :S any exampls ??!!

afroo.gfx
Newbie Poster
4 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

yes, he's saying you need to pass the database name in mysqli

like this:
$db = new mysqli($host, $user, $pass, $dbname) or die($error);

DarkMonarch
Junior Poster
120 posts since Sep 2011
Reputation Points: 11
Solved Threads: 16
Skill Endorsements: 0
diafol
Keep Smiling
Moderator
10,675 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

LOL i had to lookup that acronym, 1st encounter.

Funny and true.

DarkMonarch
Junior Poster
120 posts since Sep 2011
Reputation Points: 11
Solved Threads: 16
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0755 seconds using 2.73MB