expected ALL but reads only first

Codes here !

Moderators: macek, egami, gesf

tomis
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Nov 08, 2012 4:39 am

expected ALL but reads only first

Postby tomis » Thu Nov 08, 2012 4:48 am

hy, I have table with three cols (nr, name, text). I want to read all cols even i need just one, "name".


okey, this is what i wrote:

class Struktura{
public $tekstas;
public $menuu;


public function menu()
{

$qq=mysql_query("SELECT * FROM tb_menu") ;
$this->menuu=mysql_fetch_array($qq,MYSQL_ASSOC);

}

}

...and
this is what i got after print_r(): i just was testing

Array ( [nr] => 1 [name] => vienas [text] => bvftyhjnb vgh )

, in other words, i got only first row of my table, but i need all. Please help.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: expected ALL but reads only first

Postby egami » Thu Nov 08, 2012 8:13 am

I've never been much for object programming..


but try this..

Code: Select all


<?php 
<?php 

$connect 
= mysql_connect('host','user','pass');
$db = mysql_select_db('MyDatabase');

$table = "change_me";

$query = "SELECT * FROM $table";
$result = mysql_query($query);

if (mysql_num_rows($result) > 0) 
{
  while ($row = mysql_fetch_assoc($result))
  {
    echo '<pre>'; print_r($row); echo '</pre>';
  }
}
 else { 
  echo mysql_error
();
}
 
 

tomis
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Nov 08, 2012 4:39 am

Re: expected ALL but reads only first

Postby tomis » Fri Nov 09, 2012 1:11 am

yes, i got you.

i know what you mean.

But the problem was at
$this->menuu=mysql_fetch_array($qq,MYSQL_ASSOC);

i didnt know that this line fetches only one time (first line). So need to do loop. i thought that this function is for whole array.

note: minority of internet "teachers" mention it. This is why we have so many programing forums ;)

anyway, thank you for time spending.

MeroD
New php-forum User
New php-forum User
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: expected ALL but reads only first

Postby MeroD » Fri Nov 09, 2012 9:54 pm

if($result = $mysqli->query($query)){
while($row = $mysqli->fetch_object($result)){
echo $row->column;
}
}


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest