Print contents of MySQL database table

Discussion in 'PHP' started by sirgogo, Jun 29, 2011.

  1. #1
    Hi all,

    So I set up a database on my server, made an account, and created a table called "content". I used MySQL workbench to edit values in the table. "category", "title", and "description" are all varchar(45) 's. Here's a pic of the table. dogbert-table-content.jpg

    I'm trying to print the content in my page, using this script:
    
    $category = $_GET["cat"];
        if (($category == "") || ($category == NULL))
    		$category = 'index';
    $conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die ("Can't connect");
    	mysql_select_db($dbname, $conn);
    $query = sprintf("SELECT * FROM content WHERE category='%s'", mysql_real_escape_string($category));
        $q = mysql_query($query)
    	or die("Bad query of links table: " . ">$query<" . mysql_error());
    	
    	print($q);
    
    Code (markup):
    but all it prints is "Resource id #4" (see pic)
    dogbert-page.jpg


    Any ideas how I can edit my script or my database to print contents correctly? Any tutorials/links to how to print tables from databases would be helpful too.

    Thanks!
     
    sirgogo, Jun 29, 2011 IP
  2. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Consider this a bump with info.

    I isolated the problem, I think. I echo'd each variable, and it appears that the connection (mysql_connect) is fine (does not return false), but the mysql_selectdb does. Any ideas? Pic in original post shows existence of the database "dogbertData" as well as the table "content".
     
    Last edited: Jun 30, 2011
    sirgogo, Jun 30, 2011 IP
  3. dakshhmehta

    dakshhmehta Active Member

    Messages:
    220
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    85
    #3
    Hello,

    try:
    $query = sprintf("SELECT * FROM content WHERE category='%s'", mysql_real_escape_string($category));
    while($row = mysql_num_rows($query)){
    echo $row['col1_name'] ."". $row['col12name'] ."". $row['col3_name'] .".......". $row['colN_name'];
    }

    thank,
    daksh
     
    dakshhmehta, Jun 30, 2011 IP
  4. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks, I put it in, but no content printed. I think the initial problem is with mysql_selectdb. If that returns false, how can I expect the query to even be submitted? amiright?
     
    sirgogo, Jun 30, 2011 IP
  5. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    so i fixed the mysql_selectdb problem, i had to add
    bind-address    = 192.168.42.102
    Code (markup):
    to my /etc/my.cnf.

    I'm still not able to print the data however, it simply writes "Resource id #n", where N is a number usually 1-4 (right now its printing 3). I think its related to the number of connections open... not sure.
     
    sirgogo, Jun 30, 2011 IP
  6. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Solved!

    www.codingforums.com/archive/index.php/t-137667.html
     
    sirgogo, Jun 30, 2011 IP
  7. dizert

    dizert Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Better link - us.php.net/mysql_query, in section "Return value" :)
     
    dizert, Aug 27, 2011 IP