www.webdeveloper.com
Results 1 to 3 of 3

Thread: Get data from a MySQL table with PHP

  1. #1
    Join Date
    May 2010
    Posts
    2

    Get data from a MySQL table with PHP

    I'm building a customizable menu on my website. Customizable because you can change the order of the itens. For that, i create a colum 'barra' on my users MySQL's table, and I save thing like this: 02,05,04,09,01, where each number is a menu item (like: 02 is Movies, so the first item is Movies). To get the code saved on the table, just afeter the login code, I'm using:
    PHP Code:
    $n_query="SELECT barra FROM users WHERE username='$username'";
    $bar_t mysql_query($n_query) or die ("Wasn't possible to contact the server."); 
    Then I was using:
    PHP Code:
    $bar explode(","$bar_t);
    $_SESSION["bar1"] = $bar[0];
    $_SESSION["bar2"] = $bar[1];
    $_SESSION["bar3"] = $bar[2];
    $_SESSION["bar4"] = $bar[3];
    $_SESSION["bar5"] = $bar[4]; 
    To split the values into different vars and save in the section. However, when I ran the code, I get 'Resource id #4' on my pages. I searched the internet and a saw that I have to use something like this:
    PHP Code:
    $bar mysql_fetch_array($bar_t
    first, but when I do it, it returns 'Array'. Could someone, please, help me with this? My site is http://dragaodiario.6te.net/ (on Portuguese).

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    So you're only getting one row because a username is unique.

    Use this:

    PHP Code:
    $n_query="SELECT barra FROM users WHERE username='$username'"
    $bar_t mysql_result(mysql_query($n_query),0,0) or die ("Wasn't possible to contact the server.");

    $bar explode(","$bar_t); 
    $_SESSION["bar1"] = $bar[0]; 
    $_SESSION["bar2"] = $bar[1]; 
    $_SESSION["bar3"] = $bar[2]; 
    $_SESSION["bar4"] = $bar[3]; 
    $_SESSION["bar5"] = $bar[4]; 
    Don't copy and paste the code, read up about mysql_result @ http://php.net/manual/en/function.mysql-result.php
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  3. #3
    Join Date
    May 2010
    Posts
    2
    Thanks very, very, very much! It works perfectly!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles