Jump to content


Photo

Another SQL problem! Fun, fun!


  • Please log in to reply
12 replies to this topic

#1 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 15 July 2006 - 03:27 AM

The 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');

?>

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...
http://www.DaveLinger.com
dave at linger dot com

#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 03:30 AM

You need to do this...
if ($result) {
  // do stuff
}
else {
  echo mysql_error();
}

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 15 July 2006 - 03:31 AM

whats wrong with

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

?
http://www.DaveLinger.com
dave at linger dot com

#4 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 15 July 2006 - 03:33 AM

maybe the . instead of ,
or im saying it wrong put , instead . if not srry i wasted our time lol

#5 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 03:36 AM

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

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 July 2006 - 03:37 AM

while ($row = mysql_fetch_assoc($result)) <<<<<<<<<<<<<<<<<<<<<<<<<
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 15 July 2006 - 03:45 AM

redarrow, are you married?



...thanks...
http://www.DaveLinger.com
dave at linger dot com

#8 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 03:49 AM

while ($row = mysql_fetch_assoc($result)) <<<<<<<<<<<<<<<<<<<<<<<<<

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.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 July 2006 - 04:00 AM

Anytime Mate.

your welcome.


pixy..................................lol
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 15 July 2006 - 04:47 AM

the difference in speed would be unnoticeable for such a short query and loop.

#11 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 04:57 PM

Well, one day, DaveLinger will have many entries and it will make a 0.000003 second difference. So THERE.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#12 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 15 July 2006 - 11:17 PM

heck, it would probably make a 0.000003 second difference NOW!
http://www.DaveLinger.com
dave at linger dot com

#13 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 11:36 PM

The point is, my way is beterrrrrrrrrr. :D

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users