Results 1 to 7 of 7

Thread: Need help displaying data from a mysql query

  1. #1
    Join Date
    Nov 2013

    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: ' 
    Author: ' 
    ISBN: ' 
    $isbn '
    Price: ' 
    Condition: '
    $condition '     
    <img src=".$thumbnail.">'

  2. #2
    Join Date
    Nov 2013
    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:
    "Failed to connect to MySQL: " mysqli_connect_error();
    $query "SELECT isbn, bookTitle, bookAuthor, thumbnail FROM BooksforSell WHERE isbn='".$bookSearch."'";
    $result $mysqli->query($query);
    $newArray $result->fetch_array()){
    $isbn $newArray['isbn'];
    $bookTitle $newArray['bookTitle'];
    $bookAuthor $newArray['bookAuthor'];
    $thumbnail $newArray['thumbnail'];

  3. #3
    Join Date
    Nov 2013
    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
    You have to create a storage array.
    $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.
    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.

    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
    @mysql_select_db($database) or die( "Unable to select database");
    $query = "INSERT INTO tablename VALUES('','$field1-name','$field2-name',

  6. #6
    Join Date
    Nov 2013
    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
    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];
    //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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12516 seconds
  • Memory Usage 2,950KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (8)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates