Jump to content

Archived

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

DaveLinger

Another SQL problem! Fun, fun!

Recommended Posts

The code:

[code]<?php
include('includes/config.php');
include('includes/header.php');

$sid = $_GET['sid'];

if (!$link = mysql_connect($sqlserver, $sqlusername, $sqlpassword)) {
  echo 'Could not connect to mysql';
  exit;
}

if (!mysql_select_db($sqldatabase, $link)) {
  echo 'Could not select database';
  exit;
}

$query="SELECT *, DATE_FORMAT(time, '%W, %M %D') AS my_date FROM nuke_stories WHERE sid = $sid";
$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
echo $query;

while ($row = mysql_fetch_assoc())
{
$sid = $row['sid'];
$title = $row['title'];
$hometext = $row['hometext'];
$counter = $row['counter'];
$my_date = $row['my_date'];

echo "<p>● <b>$title</b> - Posted by $aid on $my_date<br>$hometext</p>";
}

include('includes/footer.php');

?>
[/code]

echos only the header and footer. I echoed the query and it shows as "SELECT *, DATE_FORMAT(time, '%W, %M %D') AS my_date FROM nuke_stories WHERE sid = 1238", sounds right to me, so why isnt it displaying anything or giving an error? I double checked the db to make sure the table columns match exactly...

Share this post


Link to post
Share on other sites
You need to do this...
if ($result) {
  // do stuff
}
else {
  echo mysql_error();
}

Share this post


Link to post
Share on other sites
whats wrong with

$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());

?

Share this post


Link to post
Share on other sites
maybe the . instead of ,
or im saying it wrong put , instead . if not srry i wasted our time lol

Share this post


Link to post
Share on other sites
Well that way it returns TRUE if it worked and you know it's sucessfully, if it doesn't work then you handle the error.

Honestly, I have put "or die(mysql_error());" but I always check if the query was sucessful first.

What's better is to do something like...
if (mysql_num_rows($result) != 0) {
  // do while loop
}
else {
   echo 'There is nothing in the database that matches criteria.';
}

Oh, and since you're using double quotes you don't have to due the concatenation . to put the mysql_error() with the message. I make a habbit of coding with single quotes since it's faster for the script to process when it doesn't have to parse for variables and code...

Share this post


Link to post
Share on other sites
while ($row = mysql_fetch_assoc($result)) <<<<<<<<<<<<<<<<<<<<<<<<<

Share this post


Link to post
Share on other sites
[quote author=redarrow link=topic=100639.msg397509#msg397509 date=1152934652]
while ($row = mysql_fetch_assoc($result)) <<<<<<<<<<<<<<<<<<<<<<<<<
[/quote] Whoa, I totally missed that. XD

Also, if you are naming the fields specifically you can use mysql_fetch_array($result, MYSQL_NUM); and refer to $row[0] for the first row returned, $row[1], etc--I'm pretty sure it's supposed to be faster.

Share this post


Link to post
Share on other sites
Anytime Mate.

your welcome.


pixy..................................lol

Share this post


Link to post
Share on other sites
the difference in speed would be unnoticeable for such a short query and loop.

Share this post


Link to post
Share on other sites
Well, one day, DaveLinger will have many entries and it will make a 0.000003 second difference. So THERE.

Share this post


Link to post
Share on other sites
The point is, my way is beterrrrrrrrrr. :D

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.