Jump to content

Archived

This topic is now archived and is closed to further replies.

newb

querys not working?!!?

Recommended Posts

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

[code=php:0]
<?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;
}
?>
[/code]

Now do you get it?

Share this post


Link to post
Share on other sites
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.

[code]<?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";
}?>[/code]

Share this post


Link to post
Share on other sites
Look. $row[1] and $row[2] hold nothing in your code!!!

Share this post


Link to post
Share on other sites
i would suggest using separate queries till you figure it out...

Share this post


Link to post
Share on other sites
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.imageshack.us/img205/7595/tableod4.jpg

Share this post


Link to post
Share on other sites
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...

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

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

Share this post


Link to post
Share on other sites
well wtf, how do i make it equivelent to record 1???

Share this post


Link to post
Share on other sites
oh nevermind, i figured it out, i had to do a separate query for all of them like this:

[code]<?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];
?>
[/code]

Share this post


Link to post
Share on other sites
[quote]oh nevermind, i figured it out, i had to do a separate query for all of them like this:[/quote]
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.