+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
Web Development Thread, php and sql ? in Coding and Web Development; Hi I have phpmyadmin setup on my web server and have created a database called gecko with one table called ...
  1. #1

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,909
    Thank Post
    3,435
    Thanked 1,084 Times in 1,000 Posts
    Rep Power
    370

    php and sql ?

    Hi

    I have phpmyadmin setup on my web server and have created a database called gecko with one table called feedback.

    I am trying to create a php page that will execute a the Select field1, field2, field 3 from feedback but am just getting it returning

    Connected Succesfully and Resource id #3

    What does that mean and how do I fix it ?

    Code:
    <?php
        $user="gecko";
        $host='localhost:/var/run/mysqld/mysqld.sock';
        $password="password";
        $database="gecko";
    
    $link = mysql_connect($host, $user, $password);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    ?>
    <p>
    <?php
    $sql = 'SELECT Forename , Surname , Comment FROM feedback; ';
    $result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql);  //do the query
    
    print($result);
    mysql_close($link);    
        
    ?>
    I did find this :

    PHP/MYQSL brings back wrong value (resource ID #3) - Dynamic Drive Forums

    which basically says

    "You cannot echo your raw query. You must first grab the information it is returning with a function such as mysql_fetch_array()"

    Can someone correct my php code so that it displays what I have in my database table feedback.

  2. #2
    Jona's Avatar
    Join Date
    May 2007
    Location
    Cranleigh
    Posts
    471
    Thank Post
    14
    Thanked 51 Times in 49 Posts
    Rep Power
    24
    something like
    PHP Code:
    print_r(mysql_fetch_array($result)); 
    Should do the job, to display it in a more formatted way you'll want to assign the array to a variable and then do a for loop such as:
    PHP Code:
    $rows mysql_fetch_array($result);
    foreach(
    $rows as item){
    print 
    $item['field1'];
    print 
    $item['field2'];

    Hope that helps.

    Cheers
    Jona

    Ninja edit: To correct typo.
    Last edited by Jona; 19th October 2008 at 01:08 PM.

  3. #3

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,772
    Thank Post
    873
    Thanked 674 Times in 443 Posts
    Rep Power
    503
    if u have PHPMyAdmin running, then tehre should be an option somehwere to run a query, then get it do display the PHP coding for that query..

  4. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,909
    Thank Post
    3,435
    Thanked 1,084 Times in 1,000 Posts
    Rep Power
    370
    Quote Originally Posted by Jona View Post
    something like
    PHP Code:
    print_r(mysql_fetch_array($result)); 
    Should do the job, to display it in a more formatted way you'll want to assign the array to a variable and then do a for loop such as:
    PHP Code:
    $rows mysql_fetch_array($result);
    foreach(
    $rows as item){
    print 
    $item['field1'];
    print 
    $item['field2];

    Hope that helps.

    Cheers
    Jona
    hi

    ok tried that and got this error :

    Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /home/gecko/www/feedback.php on line 19

  5. #5

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,772
    Thank Post
    873
    Thanked 674 Times in 443 Posts
    Rep Power
    503
    PHP Code:
    $rows mysql_fetch_array($result);
    foreach(
    $rows as item){
    print 
    $item['field1'];
    print 
    $item['field2];

    Should read

    PHP Code:
    $rows mysql_fetch_array($result);
    foreach(
    $rows as item){
    print 
    $item['field1'];
    print 
    $item['field2'];

    missing ' after field2

  6. #6
    Jona's Avatar
    Join Date
    May 2007
    Location
    Cranleigh
    Posts
    471
    Thank Post
    14
    Thanked 51 Times in 49 Posts
    Rep Power
    24
    As above, typing error, will correct in the orignal post.

  7. #7

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,909
    Thank Post
    3,435
    Thanked 1,084 Times in 1,000 Posts
    Rep Power
    370

    no

    ok lets try again :

    Code:
    <?php
    $link = mysql_connect($host, $user, $password);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    ?>
    <p>
    <?php
    $sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30';
    $result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql);  //do the query
    
    $rows = mysql_fetch_array($result);
    foreach($rows as item){
    print $item['Forename'];
    print $item['Surname'];
    print $item['Comment'];
    }  
    
    
    mysql_close($link);    
        
    ?>
    I get that error using the above code which has the commas etc in the correct place yet if I put a dollar symbol on this part :

    Code:
    $rows = mysql_fetch_array($result);
    foreach($rows as $item){
    print $item['Forename'];
    print $item['Surname'];
    print $item['Comment'];
    }
    As you can see in the foreach part I have put a dollar symbol aka $ before the item and then each time I run it it just returns

    mmmmmm

    What good is that - I have emptied my database and have not got any fields then I go to my add.php which works fine and it adds a record into the database but I have not once entered mmmmmm

  8. #8

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,772
    Thank Post
    873
    Thanked 674 Times in 443 Posts
    Rep Power
    503
    ignore this...
    Last edited by Gatt; 19th October 2008 at 01:26 PM.

  9. #9

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,909
    Thank Post
    3,435
    Thanked 1,084 Times in 1,000 Posts
    Rep Power
    370

    SQL

    As you can see from my PHP above I only want it to return

    $sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30';

    The Forename, Surname and Comment that each user has made in the order they were posted.

    What PHP do I need to do this please.

  10. #10

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,772
    Thank Post
    873
    Thanked 674 Times in 443 Posts
    Rep Power
    503
    This is an exert from one of my PHP scripts:

    PHP Code:
                <? 
                    $query = "SELECT * FROM dvds ORDER BY dvd_name ASC";
                    $result = mysql_query($query);
                    $numofrows = mysql_num_rows($result);

                    while ($i < $numofrows) :
                          $information = mysql_result($result, $i, "dvd_name");
                          $i++;
                    endwhile;
                ?>
    SO yours would be:

    PHP Code:
                <? 
                    $query = "SELECT Forename, Surname, Comment FROM Feedback LIMIT 0, 30";
                    $result = mysql_query($query);
                    $numofrows = mysql_num_rows($result);

                    while ($i < $numofrows) :
                          $information = mysql_result($result, $i, "forename");
                          $i++;
                    endwhile;
                ?>

  11. #11

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,909
    Thank Post
    3,435
    Thanked 1,084 Times in 1,000 Posts
    Rep Power
    370
    Quote Originally Posted by Gatt View Post
    This is an exert from one of my PHP scripts:

    PHP Code:
                <? 
                    $query = "SELECT * FROM dvds ORDER BY dvd_name ASC";
                    $result = mysql_query($query);
                    $numofrows = mysql_num_rows($result);

                    while ($i < $numofrows) :
                          $information = mysql_result($result, $i, "dvd_name");
                          $i++;
                    endwhile;
                ?>
    SO yours would be:

    PHP Code:
                <? 
                    $query = "SELECT Forename, Surname, Comment FROM Feedback LIMIT 0, 30";
                    $result = mysql_query($query);
                    $numofrows = mysql_num_rows($result);

                    while ($i < $numofrows) :
                          $information = mysql_result($result, $i, "forename");
                          $i++;
                    endwhile;
                ?>
    ok tried that and am getting this :

    Connected successfully
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gecko/www/feedback.php on line 19


    The script I am using is this :

    Code:
    <?php
        $user="gecko";
        $host='localhost:/var/run/mysqld/mysqld.sock';
        $password="mypassword";
        $database="gecko";
    
    $link = mysql_connect($host, $user, $password);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    ?>
    <p>
    <?php
    $sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30';
    //$result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql);  //do the query
    
                    //$query = "SELECT Forename, Surname, Comment FROM Feedback LIMIT 0, 30";
                    $result = mysql_query($sql);
                    $numofrows = mysql_num_rows($result);
    
                    while ($i < $numofrows) :
                          $information = mysql_result($result, $i, "forename");
                          $i++;
                    endwhile;
                
    
    
    mysql_close($link);       
    ?>

  12. #12

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,909
    Thank Post
    3,435
    Thanked 1,084 Times in 1,000 Posts
    Rep Power
    370

    ok no errors but no results

    ok have this code now :

    Code:
    <?php
        $user="gecko";
        $host='localhost:/var/run/mysqld/mysqld.sock';
        $password="mypassword";
        $database="gecko";
    $link = mysql_connect($host, $user, $password);
    mysql_select_db("gecko", $link);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    ?>
    <p>
    <?php
    //$sql = 'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30';
    //$result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql);  //do the query
    
                    $query = "SELECT Forename, Surname, Comment FROM feedback LIMIT 0, 30";
                    $result = mysql_query($query, $link);
                    $numofrows = mysql_num_rows($result);
    
                    while ($i < $numofrows) :
                          $information = mysql_result($result, $i, "Forename");
                          $i++;
                    endwhile;
                
    
    
    mysql_close($link);       
    ?>
    It just states that its connected successfully but no errors so how do I make it print each result ie Forname, Surname and Comment ??

  13. #13

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,772
    Thank Post
    873
    Thanked 674 Times in 443 Posts
    Rep Power
    503
    Sorry my fault - add a line to decalre what $i is

    PHP Code:
    $i=
    Just after your DB details:

    PHP Code:
    <?php
        $user
    ="gecko";
        
    $host='localhost:/var/run/mysqld/mysqld.sock';
        
    $password="mypassword";
        
    $database="gecko";

    $i=0;  //Declare counter here..
    $link mysql_connect($host$user$password);
    if (!
    $link) {
        die(
    'Could not connect: ' mysql_error());
    }
    echo 
    'Connected successfully';
    ?>
    <p>
    <?php
    $sql 
    'SELECT `Forename` , `Surname` , `Comment` FROM feedback LIMIT 0 , 30';
    //$result = mysql_db_query($database, $sql) or die("Failed Query of : " . $sql);  //do the query

                    //$query = "SELECT Forename, Surname, Comment FROM Feedback LIMIT 0, 30";
                    
    $result mysql_query($sql);
                    
    $numofrows mysql_num_rows($result);

                    while (
    $i $numofrows) :
                          
    $information mysql_result($result$i"forename");
                          
    $information mysql_result($result$i"Surname");
                          
    $information mysql_result($result$i"Comment");
                          
    $i++;
                    endwhile;
                


    mysql_close($link);       
    ?>
    Does it work now?

  14. #14

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,909
    Thank Post
    3,435
    Thanked 1,084 Times in 1,000 Posts
    Rep Power
    370
    Nope

    getting this error :

    Connected successfully
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gecko/www/feedback.php on line 21

  15. #15

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,772
    Thank Post
    873
    Thanked 674 Times in 443 Posts
    Rep Power
    503
    Hmm.. I can only imagine its those quote marks..

    Here is my full php script..

    PHP Code:
    <?php
        
    include ("includes/db_connect.php");
    ?>
    <html>
        <head>
            <title> DVD Library - 
                <?php
                    
    echo gmdate("l, dS F, Y");
                
    ?>
            </title>
        </head>
        <body>
            <h1> DVD Library....</h1>
            <? 
                $counter = 0;
            ?>
            <form method='post' action='vew_disc.php'>
                <select name='dvds' MULTIPLE size=10>
                <? 
                    $query2 = "SELECT * FROM dvds ORDER BY dvd_name ASC";
                    $result2 = mysql_query($query2);
                    $numofrows2 = mysql_num_rows($result2);

                    while ($i < $numofrows2) :
                          $information = mysql_result($result2, $i, "dvd_name");
                         print "<option value='$information'>$information</option>";
                          $i++;
                    endwhile;
                ?>
      
                   </select>
                <BR>
                <!-- <input type='submit' name='submit' value='Submit'> -->
            </form>
    Also, this is how PHPMyAdmin generated the PHP Code for another query..

    PHP Code:
    $sql 'SELECT Season , Episode_Title , Stardate FROM `tng` LIMIT 0, 30 '

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. MS SQL 2005 - Dump SQL
    By tom_newton in forum Windows
    Replies: 7
    Last Post: 23rd January 2008, 05:10 PM
  2. PHP Help!
    By Cravon in forum Coding
    Replies: 2
    Last Post: 20th November 2007, 05:05 PM
  3. Help with PHP
    By Gatt in forum Web Development
    Replies: 1
    Last Post: 14th November 2006, 09:01 PM
  4. php help
    By beeswax in forum Windows
    Replies: 33
    Last Post: 14th October 2005, 04:51 PM

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
  •