Jump to content

Another SQL problem! Fun, fun!


DaveLinger

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...
Link to comment
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...
Link to comment
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.
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.