www.webdeveloper.com
Results 1 to 7 of 7

Thread: Need help displaying data from a mysql query

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    69

    Need help displaying data from a mysql query

    If I search a database and it returns multiple results, what should I use to grab each result so that I can display each one in the browser. Below is the search and I thought I would use the num_rows variable to how a message if no results are found.

    PHP Code:
    $result mysql_query("SELECT isbn, bookTitle, bookAuthor, thumbnail FROM BooksForSell WHERE ISBN='".$booksearch."'") or die(mysql_error());
    $num_rows  mysql_num_rows($result); 
    While the below is not quite formatted the way it will end up looking, I basically want to have a for loop go through the results and show each one in HTML similar to below. I assume that maybe an array would be best for this but I am not sure what the function is to get the results into an array.

    PHP Code:

    echo '
    Title: ' 
    .$title'
    Author: ' 
    $author 
    ISBN: ' 
    $isbn '
    Price: ' 
    .$price'     
    Condition: '
    $condition '     
    <img src=".$thumbnail.">'


  2. #2
    Join Date
    Nov 2013
    Posts
    69
    So here is where I am not. I found most of the following code at php net but I am still not getting anywhere. Am I even close to doing what I need to do? If I am being honest, I am not really familiar with objects in PHP so that is not helping matters in this case.

    PHP Code:
    if(mysqli_connect_errno()){
                
                echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
            }
            
    $query "SELECT isbn, bookTitle, bookAuthor, thumbnail FROM BooksforSell WHERE isbn='".$bookSearch."'";
            
    $result $mysqli->query($query);
            while(
    $newArray $result->fetch_array()){
                
    $isbn $newArray['isbn'];
                
    $bookTitle $newArray['bookTitle'];
                
    $bookAuthor $newArray['bookAuthor'];
                
    $thumbnail $newArray['thumbnail'];
            
                echo 
    $isbn;
                echo 
    $bookTitle;
                echo 
    $bookAuthor;
                echo 
    $thumbnail;
            } 

  3. #3
    Join Date
    Nov 2013
    Posts
    69
    And this should have been the first line in the code only with my database info

    PHP Code:
    $mysqli = new mysqli("localhost","user,"password","database"); 

  4. #4
    Join Date
    Nov 2012
    Location
    Jakarta
    Posts
    42
    You have to create a storage array.
    Code:
    $aStorage = new Array();
    while($newArray = $result->fetch_array()){
    $aStorage[] = array(
                'isbn' => $newArray['isbn'];
                'bookTitle' => $newArray['bookTitle'];
                'bookAuthor' => $newArray['bookAuthor'];
                'thumbnail' => $newArray['thumbnail'];
    );
    }
    Then, somewhere else, you create a loop calling the storage array.
    Code:
    for ($i = 0; $i < count($aStorage); $i++)
    {
                echo $aStorage[$i]['isbn'];
                echo $aStorage[$i]['bookTitle'];
                echo $aStorage[$i]['bookAuthor'];
                echo $aStorage[$i]['thumbnail'];
            }
    If I may suggest you read up the manual section on Array to get a better understanding of the code.
    http://us1.php.net/manual/en/language.types.array.php

    In short, an array is a pair of key and value. You call the key to retrieve the value.

    Now, to store the data you need a multidimensional array. To be specific, a two-dimensional array.

    This should be enough to get you started.

    Three-dimensional array and beyond are possible, but rarely used in my experience.

    Good luck!

  5. #5
    Join Date
    Nov 2013
    Location
    Romania
    Posts
    1
    <?php$username="username";
    $password="password";$database="your_database";
    $field1-name=$_POST['Value1'];
    $field2-name=$_POST['Value2'];
    $field3-name=$_POST['Value3'];
    $field4-name=$_POST['Value4'];
    $field5-name=$_POST['Value5'];
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query = "INSERT INTO tablename VALUES('','$field1-name','$field2-name',
    '$field3-name','$field4-name','$field5-name')";mysql_query($query);mysql_close();?>

  6. #6
    Join Date
    Nov 2013
    Posts
    69
    I tried the code that firesnaker and it did not work. new Array(); seems to be incorrect syntax because that gave me an error. After looking around on php.net, I changed it to just array(), but I was still not getting any data into the array I created.


    I have changed my code a bit since the object oriented style is not really making sense to me anyway but I still do not understand how to get the data. I think my problem is that I do not understand how it is returned.

    PHP Code:
            //creates a query
            
    $query "SELECT isbn, bookTitle, bookAuthor, bookCond, thumbnail, price FROM BooksforSell WHERE isbn='".$bookSearch."'";
            
    //executes a query and returns results
            
    $result mysqli_query($dbConnect$query);
            
    //assigns the number of rows returned from the query
            
    $numRows $result->num_rows;
            
    $row mysqli_fetch_array($resultMYSQLI_NUM); 
    The query I am running in the test returns three rows. I have been looking at php.net and on this if I echo $row[0], $row[1], ......, that will output the first row of data. What I am not understanding is how are the rest of the rows returned? Maybe if I understood that, I could get it.

  7. #7
    Join Date
    Nov 2013
    Posts
    69
    Just thought I would update. I have it working, finally but I did run into one really weird thing that I never did figure out. Below is the code to make the array.

    PHP Code:
    $query "SELECT isbn, bookTitle, bookAuthor, bookCond, thumbnail, price FROM BooksforSell WHERE isbn='".$bookSearch."'";
            
    //executes a query and returns results
            
    $result mysqli_query($dbConnect$query);
            
    //assigns the number of rows returned from the query
            
    $numRows $result->num_rows;
            
            
    $i 0//create index and set to 0
            
    $bookInfo = array(); //create a new empty array
            //iterate through and fill array with data
            
    while($row mysqli_fetch_array($resultMYSQLI_NUM)){
                
                    
    $bookInfo['isbn'][$i] = $row[0];
                    
    $bookInfo['title'][$i] = $row[1];
                    
    $bookInfo['author'][$i] = $row[2];
                    
    $bookInfo['condition'][$i] =$row[3];
                    
    $bookInfo['thumbnail'][$i] = $row[4];
                    
    $bookInfo['price'][$i] = $row[5];
                    
                    
    $i++;
                    
            }
    //end while loop 
    The weird thing is when I ran the for loop, the output was always twice the index of the array. I have been testing with a query that returns three results so when the for loop ran, it would run six times with the following code.

    PHP Code:
    for($i=0$i count($bookInfo); $i++) 
    I though maybe is was because I used $i in the while loop but it should be local. Either way, I changed the $i to $j and it still did the same thing. The only way I found to get it to work is by using the row count which I set earlier like so.

    PHP Code:
    for($i=0$i $numRows$i++) 
    Any ideas as to why this was happening?

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