grabbing table data with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Diz
    New Member
    • Feb 2006
    • 19

    grabbing table data with php

    Hi,
    I modified the code on the following page:


    this is my code:
    <?
    /* declare some relevant variables */
    $DBhost = "localhost" ;
    $DBuser = "myusername ";
    $DBpass = "password";
    $DBName = "webimages" ;
    $table = "artwork";

    mysql_connect($ DBhost,$DBuser, $DBpass) or die("Unable toconnect to database");
    @mysql_select_d b("$DBName") or die("Unable to select database $DBName"); $sqlquery = "SELECT * FROM $table WHERE dateCreated = '2002'";
    $result = mysql_query($sq lquery);
    $number = mysql_numrows($ result);

    $i = 0;

    if ($number < 1) {
    print "<CENTER><P>The re Were No Results for Your Search</CENTER>";
    }
    else {
    while ($number > $i) {
    $theimage = mysql_result($r esult,$i,"image ");
    $thetitle = mysql_result($r esult,$i,"title ");
    print "<p><b>imag e:</b> $thename<br><b> title:</b>
    $thetitle</p>";
    $i++;
    }
    }
    ?>


    and i get this error message when i try to run the code:

    Warning: mysql_numrows() : supplied argument is not a valid MySQL result resource in W:\www\dateCrea ted2001.php on line 21

    There Were No Results for Your Search

    although this says there are no results, there are several images with datCreated 2002 in the table. The "image" field is a tinyblob. This is my first attempt to pull a query through.
    Can anyone please see where i'm going wrong?
    thanks,
    Diz
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The php looks fine allthough I would use mysql_fetch_ass oc rather than mysql_numrows and mysql_result so I guess the error must be in the SQL statement.

    The is nothing obvious wrong so I would try changing

    WHERE dateCreated = '2002'

    to

    WHERE dateCreated='20 02'

    NOTE I have deleted 2 spaces

    and running the query directly on the database without going through php or going via phpmyadmin.

    Comment

    • Diz
      New Member
      • Feb 2006
      • 19

      #3
      thanks Banfa. I will those changes to the code. but i think i have to use php as what I'm working on is a website, mostly html, but a backend database to store the images in. i've created a few queries using phpmyadmin and mysql, and I've run a similar query directly on the database, but thought i would try and use the example code as i've just linked to the database but haven't managed to pull a query through yet!

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        What I do to test out new database stuff this:

        I have MySql install on my computer at home

        I download a backup of the database on the website using PhPMyAdmin

        I load it into my home sql server using the 'source' command

        I try out any new selects etc at home from the MySql command prompt rather than through a website.

        This allows you to work on the MySql statements independently of any other language or interface making it easy and quick to get you SQL right.

        Once it is right you can insert it into you PHP.

        Comment

        Working...