SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inserting rows returned by mysqli_fetch_array into an array.

    Greetings everyone!

    I am Alex and this is my first day on this forum.

    I am new to PHP and MySQL. I have a book that I use for studying the languages, by Kevin Yank. In chapter 4 of this book, there is a small project where you can to create a database of jokes, called "ijdb". This database will have a table, called jokes. A table will have three columns: id, joketext and jokedate. A couple of jokes are already created.

    Here is a code for php controller file. Please, look through it. My question will be at the end:
    Code PHP:
    <?php
    //connect to database
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
    $output = "Could not set connection.";
    include 'output.html.php';
    exit();
    }
     
    //tell php to use UTF8 character set when talking to mysql
    if (!mysqli_set_charset($link, 'utf8'))
    {
    $output = "Could not set character set.";
    include 'output.html.php';
    exit();
    }
     
    //select a database to work with
    if (!mysqli_select_db($link, 'ijdb'))
    {
    $output = "Could not select a database.";
    include 'output.html.php';
    exit();
    }
     
    //create an SQL query
    $sql = 'SELECT id, joketext FROM jokes';
     
    //sending a request query to output data stored in a database to a webpage
    $result = mysqli_query($link, $sql));
    if (!$result)
    {
    $output = "Could not retrieve the result set.";
    include 'output.html.php';
    exit();
    }
     
    //fetching rows one by one from our result set
    while ($row = mysqli_fetch_array($result))
    {
    $jokes[] = array('id' => $row['id'], 'text' => $row['joketext'] );
    }
    $output = "Rows has been assigned to a new array!";
    include 'output.html.php';
    ?>

    Here is a PHP template for displaying an error. Here I don't open a document with the HTML elements for the sake of not making the code too big:
    Code PHP:
    <?php
    echo $output;
    ?>

    QUESTION:

    Is there anything wrong in this code, specifically in this part:
    Code PHP:
    //fetching rows one by one from our result set
    while ($row = mysqli_fetch_array($result))
    {
    $jokes[] = array('id' => $row['id'], 'text' => $row['joketext'] );
    }
    $output = "Rows has been assigned to a new array!";
    include 'output.html.php';

    Please, help. I am stuck, seriously. I cannot get through the while loop for some reason and it does not take me to my final destination; printing our the $output line in the output.html.php

    I seem to have this problem only when I try to fetch more than one column data from my jokes table. If I were say to use this sql query: 'SELECT joketext FROM jokes', I would easily got through the while loop with this code:
    Code PHP:
    while ($row = mysqli_fetch_array($result))
    {
    $jokes[] = $row['joketext'];
    }

    Still, when it comes to getting two elements from a single row, I am stuck with the while loop.

    On a page where I am to display the jokes, I use eachfor loop to display all the I have in my $jokes[] array;

    In case when id and joketext are fetched, id is used in a hidden form field to enable deleting of jokes by a user. So, id is a primary key here and is passed to a $_POST array. This is just feedback to create a complete picture.

    Please help.

    Alex.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Welcome to SP.

    From these pieces of code, I can't tell what is wrong. What do you mean by 'stuck with the while loop' ? Do you get an error? A blank page?

    And please post the code of output.html.php.

    Off Topic:

    Kevin and his jokes! Looking at the number of people that post here with problems, I hope they are at least funny ones

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I say: I get stuck on the while loop, I mean first it looks like I get into the infinite loop, and the the server starts cursing and tells me there is a mistake and server is temporary out of reach and suggests me to notify MS of this problem.
    Code PHP:
    <html>
    	<head>
    		<title>Add Joke</title>
    		<meta http-equiv="content-type"
    				content="text/html; charset=utf-8"/>
     
    	</head>
    	<body>
    <?php
    echo $output.html.php
    ?>
    	</body>
    </html>

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Very strange. The while loop isn't infinite. It goes from 0 (no results) to the number of rows extracted by the query, which might be huge (I don't know how many jokes you've inserted in the table), but never infinite.
    I have no idea.

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have up to 5 jokes in my table, sure not infinite.

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My code works!!! It has something to do with the configuration of my php and mysql. A friend of mine used my code and everything went fine. So, does that mean I will need to crash the php and mysql and apache and start if over? But how can I make sure that I have the right configuration and that it is going to work with this configuration? I was happy to realize it is a server side language so there is no such a think as a cross browser testing..... now, there is cross php versions testing??? Gosh.... :-) But I am happy and no longer stuck on this project :-)

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Try installing WAMP server, or something like that. It'll take care of installing everything, Apache, PHP and MySQL.

  8. #8
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have exactly the same problem, followed the instructions as per the book to install php, mysql and apache server, followed the examples and then this code does not work. Under win XP it gives a server error message and apache crashes out. Under Win7 the browser cannot load the page with a connect error.

    Though it isn't the while loop causing the problem, it is the mysqli_fetch_array($result)) call within the while loop.

    I have no idea why this should be so but nothing I have tried makes any difference.

    Not good!

  9. #9
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you please provide table structure here?

  10. #10
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by splott View Post
    I have exactly the same problem, followed the instructions as per the book to install php, mysql and apache server, followed the examples and then this code does not work. Under win XP it gives a server error message and apache crashes out. Under Win7 the browser cannot load the page with a connect error.

    Though it isn't the while loop causing the problem, it is the mysqli_fetch_array($result)) call within the while loop.

    I have no idea why this should be so but nothing I have tried makes any difference.

    Not good!
    Please tell me, why do you think it is mysqli_fetch_array($result) causes the error? The thing is we gave it a try fetching a row also with mysqli_fetch_row function, which is pretty much the same as far as I know, but that still does not solve a thing. Solution: install WAMP package and it will work just fine!

  11. #11
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I meant that it was that piece of code, rather than the while loop that was causing the problem.

    I have un-installed everything and downloaded and installed WAMP which does indeed work, but it is not good that if you follow the instructions in the book to install the packages individually there is currently a fundamental problem.

  12. #12
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by splott View Post
    I meant that it was that piece of code, rather than the while loop that was causing the problem.

    I have un-installed everything and downloaded and installed WAMP which does indeed work, but it is not good that if you follow the instructions in the book to install the packages individually there is currently a fundamental problem.
    Yeah I know, it is not good. That is why I emailed Kevin, the author, to see what is going on. Hopefully something is going to be done for the next publication of this book.


Bookmarks

Posting Permissions

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