Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    91
    Thanks
    1
    Thanked 0 Times in 0 Posts

    mySQL and PHP question

    Code:
    $sql = mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die(mysql_error());
    That is a peice of code I have to select something from the PHP database, now I would like to know how to print that I selected onto my page. I tried searching for an answer, and couldn't find one. I'm probably searching the wrong thing. I tried:

    Code:
    echo $sql;
    but that just prints:

    Resource id #3
    as opposed to what the actual question is.

    Thanks!

    Smesh

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That echo'ed correctly, what you need to do is this.

    PHP Code:
    $sql mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die(mysql_error()); 
    if (
    $sql)
    {
        
    $result mysql_fetch_assoc($sql);
        echo 
    $result['question'];
    }
    else
    {
       
    //query failed

    There are other function instead of mysql_fetch_assoc just look in the See Also section.

  • #3
    New Coder
    Join Date
    Nov 2004
    Posts
    91
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That code doesn't work. It all looks right, but for some reason I just get a blank screen. Any other ideas? Or whats the fucntion to print an error so I know whats wrong?

    Thanks!

    Smesh
    Last edited by smeshy123; 06-12-2005 at 07:16 AM.

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    More than likely an error in your Query

    PHP Code:
    $sql mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die(mysql_error());
    if (
    mysql_num_rows($sql) != 0)
    {
         if (
    $sql)
         {
             
    $result mysql_fetch_assoc($sql);
             
    var_dump($result);
             
    //if still nothing try mysql_fetch_array($sql, MYSQL_ASSOC);
        
    }
        else
        {
            
    //query failed
            
    echo mysql_error(); //should have already been processed in the or die though
         
    }

    else
    {
        echo 
    "No Rows Found : "mysql_num_rows($sql); 


  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    91
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by gsoft
    More than likely an error in your Query

    PHP Code:
    $sql mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die(mysql_error());
    if (
    mysql_num_rows($sql) != 0)
    {
         if (
    $sql)
         {
             
    $result mysql_fetch_assoc($sql);
             
    var_dump($result);
             
    //if still nothing try mysql_fetch_array($sql, MYSQL_ASSOC);
        
    }
        else
        {
            
    //query failed
            
    echo mysql_error(); //should have already been processed in the or die though
         
    }

    else
    {
        echo 
    "No Rows Found : "mysql_num_rows($sql); 

    If there was an error in my querry, wouldn't it die instead of just not doing anything, without any of that if code in my document?

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes my comments state that though

    PHP Code:
    //query failed
            
    echo mysql_error(); //should have already been processed in the or die though 
    Its why I also made sure that there were rows returned from the query using mysql_num_rows more than likely why your getting a blank screen, that mysql_error() was also put for safety net incase you got a blank screen an error would popup or mysql_error return no error results also giving you an idea on where the code is going and how to debug.

  • #7
    New Coder
    Join Date
    Nov 2004
    Posts
    91
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, I get it...only problem is that your code doesn't work either :'(. I know the problem isn't with the Query string because I went into PHP admin copy and pasted it into the SQL Run Query thing and it worked fine. I still also get a white screen. I'll post more code from higher up, maybe I made a mistake somewhere else:

    PHP Code:
     $link mysql_connect('localhost''root'); 
    if (!
    $link) {
        die(
    'Could not connect');
    }
    mysql_select_db("index");
    $randvalue rand(1,2);
    echo 
    $randvalue;


    $sql mysql_query("SELECT question FROM spokenword WHERE value=\"1\"") or die("doesnt work"); 
    if*(
    mysql_num_rows($sql)*!=*0

    *****if*(
    $sql
    *****{ 
    *********
    $result*=*mysql_fetch_assoc($sql); 
    *********
    var_dump($result); 
    *********
    //if still nothing try mysql_fetch_array($sql); 
    ****} 
    ****else 
    ****{ 
    ********
    //query failed 
    ********echo*mysql_error();*//should have already been processed in the or die though 
    *****} 
    }* 
    else 

    ****echo*
    "No Rows Found : ".*mysql_num_rows($sql);* 
    }* 
    Thanks for the quick response!

    - Smesh

    PS: I have NO clue why those *'s are popping up in the forum, and I can't figure out how to get rid of them.

    A couple other things to clear up: index = my database, spokenword = a table in my database, question = a column/row (i always confuse them) within my database. The database is higher up in the heiarchy of the server. The php file is Files/Testing/index.php while phpmyadmin is in Files/phpmyadmin

    I don't know if any of that makes a difference?
    Last edited by smeshy123; 06-12-2005 at 08:41 AM.

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    View source your file, if blank there too open your php.ini file and change the error reporting to E_ALL restart the server

    Edit:
    There should also be a third parameter in the mysql_connect function for password if its blank just use , ""

  • #9
    New Coder
    Join Date
    Nov 2004
    Posts
    91
    Thanks
    1
    Thanked 0 Times in 0 Posts
    where is the php.ini file located? I am new to php, and have never needed to use it before. I ran a quick serach on my laptop for it once, and couldn't find it for some reason, when I was playing around with some XML/PHP stuff.

    Thanks!

    Smesh

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You using Windows?
    If so there are a couple of locations

    Using IIS
    Should be in the PHP Directory with dlls (e.g. c:\php)
    System Folder

    Apache
    Same directory as the Apache Binary
    PHP Binary c:\php
    System Folder

    Since I dont use Windows with PHP much I usually have the php.ini with the Apache Binary to keep installation clean.

    If your using Linux it will depend on your --prefix= settings if set. e.g. --prefix=/usr/local/php would be in the /usr/local/php/lib directory

  • #11
    New Coder
    Join Date
    Nov 2004
    Posts
    91
    Thanks
    1
    Thanked 0 Times in 0 Posts
    actually im using Mac..OSX.3 with Apache
    Last edited by smeshy123; 06-12-2005 at 09:03 AM.

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #13
    New Coder
    Join Date
    Nov 2004
    Posts
    91
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by gsoft
    View source your file, if blank there too open your php.ini file and change the error reporting to E_ALL restart the server

    Edit:
    There should also be a third parameter in the mysql_connect function for password if its blank just use , ""
    ok, I have the php.ini file open in pico, what is the exact name of the error reporting thing? because I see a lot of different error stuff and all of them just say on or off.

    Thanks!

    Smesh

  • #14
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There should be a bit with

    error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
    Something like that, change that to just

    error_reporting = E_ALL
    Then below that make sure display_errors is On

    display_errors = On
    Restart the server.

  • #15
    New Coder
    Join Date
    Nov 2004
    Posts
    91
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How do you restart an Apache Server on Mac OSX 10.4? Never had to do it before...

    Thanks!

    Smesh


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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