Jump to content

querys not working?!!?


newb

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?
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

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.