Jump to content
php.lv forumi

Datu izvadīšana no datubāzes ar kvēriju - nepareizi izvada


NERVi

Recommended Posts

Ko es daru nepareizi?

 

<?php
require_once 'connect.inc.php';
               $query = "SELECT `id` FROM `categorys`";
           	$query_run = mysql_query($query);

       	for($field = 1;$field <= mysql_num_rows($query_run);$field++){

           	$query2 = "SELECT `title` , `link` FROM `categorys` WHERE `id` = '$field'";
      		if( $query2_run = mysql_query($query2)){
               	$query_title =  mysql_result($query2_run,`title`);
               	$query_link =  mysql_result($query2_run,`link`);

               	echo $query_title." Link: ".$query_link."<br>";
      		}
       	}
?>

 

 

Tabula izskatās šādi:

2s7jt04.png

 

 

Galarezultātam jābūt šādam:

category1 Link: http://lol.com

category2 Link: http://lol2.com

category3 Link: http://lol2.com

category4 Link: http://lol3.com

 

Bet ir šāds:

category1 Link: category1

category2 Link: category2

category3 Link: category3

category4 Link: category4

Edited by NERVi
Link to comment
Share on other sites

pareizāk būtu..

$query = "SELECT name, link FROM categorys";


$query = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($query)){// vai mysql_fetch_assoc
echo $row['name']." link: <a href='".$row['link']."'>link</a>";
}

Un jā, vēl tad varētu iesaistīt, mysql num rows un tml..

Edited

Edited by waplet
Link to comment
Share on other sites

pareizāk būtu..

$query = "SELECT name, link FROM categorys";


$query = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_row($query)){
echo $row['name']." link: <a href='".$row['link']."'>link</a>";
}

Un jā, vēl tad varētu iesaistīt, mysql num rows un tml..

 

Nospiežot uz linka, netiek redirektots uz citu lapu.Paliek tajā pašā lokalhostā

Edited by NERVi
Link to comment
Share on other sites

$query = "SELECT `name`, `link` FROM `categorys`";

btw, parādi visu failu.

 

<?php

require_once 'connect.inc.php';

$query = "SELECT `name`, `link` FROM `categorys`";
$query = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_row($query)){

echo $row['name']." link: <a href='".$row['link']."'>link</a>";

}


?>

Edited by NERVi
Link to comment
Share on other sites

Nu, kur links redirektē ir jaskatās, ko viņs izvelk laukā un tml... vai pareizi esi datus savadījis, šitais

"jau tikai darbinieks ,kas parāda, ko kāds cits dara, nevis pats atbild par viņu darbu(html'u)"
Link to comment
Share on other sites

Nu, kur links redirektē ir jaskatās, ko viņs izvelk laukā un tml... vai pareizi esi datus savadījis, šitais

 

Esmu visu pareizi savadījis

Lapas source:

link: <a href=''>link</a> link: <a href=''>link</a> link: <a href=''>link</a> link: <a href=''>link</a>

 

 

Šobrīd lapas kods izskatās šādi:

<?php
require_once 'connect.inc.php';
$query = "SELECT `name`, `link` FROM `categorys`";
$query = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_row($query)){
echo $row['name']." link: <a href='".$row['link']."'>link</a><br>";
}

?>

 

Datubāze:

 

CREATE TABLE `categorys` (
 `id` int(11) NOT NULL auto_increment,
 `name` varchar(30) NOT NULL,
 `link` varchar(40) NOT NULL,
 PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `categorys`
--
INSERT INTO `categorys` VALUES (1, 'category1', 'http://lol.com');
INSERT INTO `categorys` VALUES (2, 'category2', 'http://lol2.com');
INSERT INTO `categorys` VALUES (3, 'category3', 'http://lol3.com');
INSERT INTO `categorys` VALUES (4, 'category4', 'http://lol4.com');

Edited by NERVi
Link to comment
Share on other sites

Kļuda ir iekš:

 

while($row = mysql_fetch_row($query))

 

ja izmanto šo tad tiek izveidots nevis asocaitivais masīvs bet parastais

$row[0]." link: <a href='".$row[1]."'>link</a><br>";

tadēļ labāk izmantot : mysqli_fetch_array() tad varēsi izmantot abus izvades varjantus - ātrdarbības zaudējums būs tik niecīgs, ka pie maziem datu apjomiem - faktiski nebūs izmērāms

Edited by Grey_Wolf
Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...