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
https://forums.phpfreaks.com/topic/14647-another-sql-problem-fun-fun/
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...
[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.

Archived

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

×
×
  • 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.