Jump to content


Photo

querys not working?!!?


  • Please log in to reply
34 replies to this topic

#21 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 05 August 2006 - 07:20 AM

bah, ur code almost works thorpe, except $midlink doesnt show.


Because (AS IVE SAID!!!) your only selecting one field!


and hows that a problem? i dont get it, which fields should i select :s

#22 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 August 2006 - 07:42 AM

Which fields do you want? the table you showed us has the fileds id, name, active and data.

#23 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 05 August 2006 - 08:02 AM

yeah i know, the only field that i needed at the moment was the data field, i was planning on utilizing the other fields later on.

#24 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 August 2006 - 08:06 AM

Well then.... the only $row that will hoild a value is $row[0].

#25 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 05 August 2006 - 08:07 AM

ugh, i figured out the problem.
<?php
include "config.php";
//create connection
$connection = mysql_connect("$mysql_host", "$mysql_login", "$mysql_pass") or die ("Couldnt connect to the server.");
//select database
mysql_select_db("$mysql_database", $connection) or die ("Couldnt select the database. ".mysql_error());

$sql = mysql_query("SELECT data FROM `p16_blocks` WHERE id IN (0,1,2) ORDER BY `id` ASC", $connection);
$row = mysql_fetch_row($sql);

// STEP 1
// html definitions - format results by row
while ($row = mysql_fetch_row($sql)) {
  $midlink = $row[0];
  $footerlink = $row[1];
  $copyright = $row[2];
  
  echo $midlink;
  echo $footerlink;
  echo $copyright;
}
?>

I needed to remove
$row = mysql_fetch_row($sql);
. Fix:
<?php
include "config.php";
//create connection
$connection = mysql_connect("$mysql_host", "$mysql_login", "$mysql_pass") or die ("Couldnt connect to the server.");
//select database
mysql_select_db("$mysql_database", $connection) or die ("Couldnt select the database. ".mysql_error());

$sql = mysql_query("SELECT data FROM `p16_blocks` WHERE id IN (0,1,2) ORDER BY `id` ASC", $connection);

// STEP 1
// html definitions - format results by row
while ($row = mysql_fetch_row($sql)) {
  $midlink = $row[0];
  $footerlink = $row[1];
  $copyright = $row[2];
  
  echo $midlink;
  echo $footerlink;
  echo $copyright;
}
?>



#26 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 August 2006 - 08:10 AM

Still your looking at your database around the wrong way. This code will still work same as your above...

<?php
include "config.php";
//create connection
$connection = mysql_connect("$mysql_host", "$mysql_login", "$mysql_pass") or die ("Couldnt connect to the server.");
//select database
mysql_select_db("$mysql_database", $connection) or die ("Couldnt select the database. ".mysql_error());

$sql = mysql_query("SELECT data FROM `p16_blocks` WHERE id IN (0,1,2) ORDER BY `id` ASC", $connection);

// STEP 1
// html definitions - format results by row
while ($row = mysql_fetch_row($sql)) {
  $midlink = $row[0];
  echo $midlink;
}
?>

Now do you get it?

#27 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 05 August 2006 - 08:20 AM

ya i see what u mean. i tried implementing the code in a template system i made, but it kinda failed. well, the code worked, but the template repeated itself downward, it made 3 copies of itself going down.

<?php
include "sources/config.php";
//create connection
$connection = mysql_connect("$mysql_host", "$mysql_login", "$mysql_pass") or die ("Couldnt connect to the server.");
//select database
mysql_select_db("$mysql_database", $connection) or die ("Couldnt select the database. ".mysql_error());

$sql = mysql_query("SELECT data FROM `p16_blocks` WHERE id IN (0,1,2) ORDER BY `id` ASC", $connection);

// STEP 1
// html definitions - format results by row
while ($row = mysql_fetch_row($sql)) {
  $midlink = $row[0];
  $footerlink = $row[1];
  $copyright = $row[2];
  
include $tpath."header.php";
echo $midlink;

include $tpath."structure.php";
include $tpath."structure2.php";
echo $footerlink;
echo $copyright;
include $tpath."footer.php";
}?>


#28 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 August 2006 - 08:26 AM

Look. $row[1] and $row[2] hold nothing in your code!!!

#29 beamerrox

beamerrox
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationGrimshaw, Alberta, Canada

Posted 05 August 2006 - 09:35 AM

i would suggest using separate queries till you figure it out...

#30 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 05 August 2006 - 07:29 PM

wtf. $row[1] and $row[2] do hold someting. they hold the information stored in the data field just like $row[0] does. its shown right here: http://img205.images...95/tableod4.jpg

#31 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 06 August 2006 - 07:33 AM

any help pls?

#32 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 06 August 2006 - 08:06 AM

Your not listening. I suggest you go do a tutorial on querying a database in php. $row[1] and $row[2] do NOT hold any data because you have only queried 1 field in you database (the data field), this one filed shows up in $row[0]. Each time you loop through your result you get another record.

Hence... as ive shown you, this...

while ($row = mysql_fetch_row($sql)) {
    $midlink = $row[0];
    echo $midlink;
}

produces the same output as all your other redundant code. $row[1] is not equivelent to record 1 as it seems you expect.

#33 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 07 August 2006 - 01:36 AM

well wtf, how do i make it equivelent to record 1???

#34 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 07 August 2006 - 02:01 AM

oh nevermind, i figured it out, i had to do a separate query for all of them like this:

<?php
require "sources/config.php";
//create connection
$connection = mysql_connect("$mysql_host", "$mysql_login", "$mysql_pass") or die ("Couldnt connect to the server.");

//select database
mysql_select_db("$mysql_database", $connection) or die ("Couldnt select the database. ".mysql_error());

$sql = mysql_query("SELECT data FROM `p16_blocks` WHERE id IN (0) ORDER BY `id` ASC", $connection);
$sql1 = mysql_query("SELECT data FROM `p16_blocks` WHERE id IN (1) ORDER BY `id` ASC", $connection);
$sql2 = mysql_query("SELECT data FROM `p16_blocks` WHERE id IN (2) ORDER BY `id` ASC", $connection);

$row = mysql_fetch_row($sql);
  $midlink = $row[0];
  
  $row1 = mysql_fetch_row($sql1);
  $footerlink = $row1[0];
  
  $row2 = mysql_fetch_row($sql2);
  $copyright = $row2[0];
?>


#35 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 August 2006 - 03:42 AM

oh nevermind, i figured it out, i had to do a separate query for all of them like this:

What? That is a complete waste of resources. The code you had was working fine... you need the loop to retrieve all records.

The way you have it setup now your running 3 queries where you only need one. You REALLY need to understand this stuff.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users