PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,145 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,733 people online right now. Registration is fast and FREE... Join Now!




Show Row - MySql

 

Show Row - MySql

fig79

6 Nov, 2009 - 10:41 PM
Post #1

D.I.C Head
**

Joined: 25 Feb, 2009
Posts: 50


My Contributions
PHP version 5.3
Database : MySQL v5.14
Apache server v2.2
CODE
$con = mysql_connect('localhost','me','pass1');
    if (!$con) {
        die("Error connecting to database server : " .mysql_error());
     }else {
         echo "Connection OK" ."<br />";
     }

$dbName = 'db01';
     if (!mysql_select_db($dbName, $con)){
          die("Error connecting to database : " .mysql_error());
     }else{
         echo "Selecting Database $dbName - Successed" ."<br />";    
     }

$query = 'select * from posts';
$result = mysql_query($query);
    
     while ($row = mysql_fetch_row($result)) {
          echo $row[1] ."<br />";
          echo $row[2];
     }


Inside My posts table :
- id (int ,auto increment)
- title (varchar)
- body (varchar)

why I can not use this ?
CODE
while ($row = mysql_fetch_row($result)) {
          echo $row['title'] ."<br />";
          echo $row['body'];
     }


Warning :
QUOTE

Notice: Undefined index: title in C:\Apache2.2\htdocs\database\db.php on line 28
Notice: Undefined index: body in C:\Apache2.2\htdocs\database\db.php on line 29


This post has been edited by fig79: 6 Nov, 2009 - 10:48 PM

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Show Row - MySql

6 Nov, 2009 - 11:07 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 838 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
mysql_fetch_row returns an array where all columns are stored as an offset, not the column name. This makes it easy for scripts that want to loop through the columns using an integer counter etc. What you want to be using is mysql_fetch_array which will return the array and keys will be the column names. Then you will be able to use $row['title'] and $row['body'].

smile.gif
User is offlineProfile CardPM
+Quote Post

fig79

RE: Show Row - MySql

6 Nov, 2009 - 11:22 PM
Post #3

D.I.C Head
**

Joined: 25 Feb, 2009
Posts: 50


My Contributions
QUOTE(Martyr2 @ 6 Nov, 2009 - 11:07 PM) *

mysql_fetch_row returns an array where all columns are stored as an offset, not the column name. This makes it easy for scripts that want to loop through the columns using an integer counter etc. What you want to be using is mysql_fetch_array which will return the array and keys will be the column names. Then you will be able to use $row['title'] and $row['body'].

smile.gif


Thx bro icon_up.gif

btw one more question if you don't mind.
This program is working when use ' (single protection)
CODE
$query = 'update posts set
             title = "Story",
             body = "bla bla bla..."             
             where id = 2
         ';


but, when use " (double protection) is not working?
CODE
$query = "update posts set
             title = "Story",
             body = "bla bla bla..."             
             where id = 2
         ";


1. why?
2. Is there different when use ' or "?
3. when we use ' or "?

This post has been edited by fig79: 6 Nov, 2009 - 11:23 PM
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Show Row - MySql

7 Nov, 2009 - 01:51 AM
Post #4

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,891



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
There is no difference, but that string isn't complete, if you're using double quotes to contain the string, you need to either escape them (\") within, or use single quotes within.

Also, if you just want an associative array, you might want to look into mysql_fetch_assoc, it probably won't make a difference, but in reality it's slightly faster than mysql_fetch_array whilst performing the same function smile.gif
User is offlineProfile CardPM
+Quote Post

fig79

RE: Show Row - MySql

7 Nov, 2009 - 02:38 AM
Post #5

D.I.C Head
**

Joined: 25 Feb, 2009
Posts: 50


My Contributions
QUOTE(RudiVisser @ 7 Nov, 2009 - 01:51 AM) *

There is no difference, but that string isn't complete, if you're using double quotes to contain the string, you need to either escape them (\") within, or use single quotes within.

Also, if you just want an associative array, you might want to look into mysql_fetch_assoc, it probably won't make a difference, but in reality it's slightly faster than mysql_fetch_array whilst performing the same function smile.gif


Thx biggrin.gif

CODE
function showData() {
         $query = 'select * from posts';
            $result = mysql_query($query);
        while ($row = mysql_fetch_assoc($result)) {
            echo $row['title'] ."<br />";
            echo $row['body'] ."<br />";
        }
        
    }

showData();


Can I put this variable ($query & $result) outside function showData ?
How?
QUOTE
$query = 'select * from posts';
$result = mysql_query($query);


User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Show Row - MySql

7 Nov, 2009 - 02:40 AM
Post #6

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,891



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Just copy the content of the function outside of the body.. Or do you mean you want showData to just show any rows??

CODE
function showData($results) {
   while ($row = mysql_fetch_assoc($results)) {
      echo $row['title'] ."<br />";
      echo $row['body'] ."<br />";
   }
}

$query = 'select * from posts';
$result = mysql_query($query);
showData($result);

User is offlineProfile CardPM
+Quote Post

fig79

RE: Show Row - MySql

7 Nov, 2009 - 03:30 AM
Post #7

D.I.C Head
**

Joined: 25 Feb, 2009
Posts: 50


My Contributions
QUOTE(RudiVisser @ 7 Nov, 2009 - 02:40 AM) *

Just copy the content of the function outside of the body.. Or do you mean you want showData to just show any rows??

CODE
function showData($results) {
   while ($row = mysql_fetch_assoc($results)) {
      echo $row['title'] ."<br />";
      echo $row['body'] ."<br />";
   }
}

$query = 'select * from posts';
$result = mysql_query($query);
showData($result);



Thx bro icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:46PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month