www.webdeveloper.com
Results 1 to 4 of 4

Thread: mysql results from multiple rows in database table

  1. #1
    Join Date
    Feb 2013
    Posts
    2

    mysql results from multiple rows in database table

    I am running a mysql query with the code below but i cannot get the results from multiple rows in a table.
    I echo the results and all i get is Array. Help will be appreciated. Thank you

    The $row should echo all the fields under code1 in the table.

    Thank you

    PHP Code:
    mysql_connect('localhost''u''C');
    mysql_select_db('u');
    //
    $result mysql_query("SELECT code1 FROM codes")
    or die(
    mysql_error());
    $row mysql_fetch_assoc($result);
    echo (
    "<center> <font color=#FF0000 face=Verdana size=2><b>$row</b></font>"); 

  2. #2
    Join Date
    Feb 2013
    Posts
    2
    the reason for the echo statement was to see exactly what the problem was. This is what i want done
    To receive a variable from a form and test it against all the rows in a database column and if it finds one that corresponds to that row, then go to another page
    The code is below
    PHP Code:

    $code1
    =$_POST['code'];
    mysql_connect('localhost''u''C');
    mysql_select_db('u);
    //
    $result = mysql_query("SELECT code1 FROM codes")
    or die(mysql_error());
    $row = mysql_num_rows($result);

    //
    if ($row[0] =='
    $code1') {
    include ("mynewpage.php");
            } else {
            echo ("<center> <font color=#FF0000 face=Verdana size=2><b>Invalid Code!</b></font>");
            include("oldpage.php"); } 

  3. #3
    Join Date
    Feb 2013
    Location
    Philippines
    Posts
    3
    Hi, i think you should wrap your variable in curly brace {}.
    Try my example.

    <?php
    //your connection info
    include_once ("includes/connection.php");
    ?>

    <html>
    <head>


    <title>

    </title>
    </head>

    <?php
    $query = "Select menu_name from menu_rf";
    $result = mysql_query($query);

    $arr = array();
    while($rows = mysql_fetch_array($result)){
    echo "<center><font color=#FF0000 face=Verdana size=2><b>{$rows['menu_name']}</b></font></center>";
    }
    ?>
    </html>

    Note: I already test and it works. I hope it helps you.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,312
    $row is an associative array, so you need to access each column of the result row (even if it's only one column) as $row['code1'].

    The typical way to output all results is to use a while() loop:
    PHP Code:
    echo "<ul>\n";
    while(
    $row mysql_fetch_assoc($result) {
        echo 
    "<li>".$row['code1']."</li>\n";
    }
    echo 
    "</ul>\n"
    Last edited by NogDog; 02-18-2013 at 05:52 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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