TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,970
  • Latest: bynw
Stats
  • Total Posts: 196,055
  • Total Topics: 21,337
  • Online today: 201
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 158
  • Total: 158

SQL Query in PHP article

Started by ed_m, March 20, 2008, 08:34:47 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ed_m2

I now have TP up and running in a hidden corner of my server (hence no link for you yet) and am going through the process of making sure i can reproduce all the features i have on the current mambo site.

One thing I have is a race results page which calls a mambo wrapper to a php query.
The perfect application for a PHP article methinks.

This query pulls & prints results from a seperate database & table to SMF/TP
And does so succesfully in my PHP article.

However it breaks the next block along in the right sidebar which happens to be latest posts.

SMF version: 1.1.4
TP version: TP 0.9.8
Theme name and version: Copy of default
Mods installed: None
Related Error messages:
Contained in the recent posts block:
Quote
Warning: mysql_query(): 8 is not a valid MySQL-Link resource in xxxxxxx/public_html/forum/Sources/Subs.php on line 321
2: mysql_query(): 8 is not a valid MySQL-Link resource
File: xxxxxxxxx/public_html/forum/Sources/Subs.php
Line: 321

The PHP article sets up some pretty default sounding variables for the query:

// Set the variables for the database access:
$Host = "localhost";
$User = "xxxxxxxxxx";
$Password = "xxxxxxxxxx";
$DBName = "xxxxxxxxxxxx";
$TableName = "race2008";

$Link = mysql_connect ($Host, $User, $Password);

$Query = "SELECT * from $TableName where cat='MS'";

$Result = mysql_db_query ($DBName, $Query, $Link) or die ('Error: '.mysql_error ());


There's also a close link at the end:
mysql_close ($Link);

I thought perhaps i might be overwriting variables SMF was trying to use afterwards but I've prefixed all the above variables to make them unique... which doesnt help.

If I take the close out at the end i get a different error:
QuoteDatabase Error: Table 'XXXXXX.smf_messages' doesn't exist
File: xxxxxxxx/public_html/forum/SSI.php
Line: 361

In this case the tablename XXXXX given is the one I am querying, so yeh.. smf_messages doesnt exist.

I'm not quite familiar enough with the SMF coding to know what I've broken.

Any ideas?
Am I missing something obvious ?

(Sorry for the rather long post)

Ed


JPDeni

There's a really nice structure for performing queries within TP and SMF. You don't need to include the host or password or user or name of the database. It's all done for you.


$query = db_query("
SELECT *
FROM race2008
WHERE cat = 'MS'", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($query))
{
  [whatever you want to do with each of the entries
}


This assumes that your table is in the same database as SMF, which I would suggest.

ed_m2

ah ok.. thanks.. easy answer then.

it's not currently in the same db as SMF due to the ad hoc structure of the old site, but i guess it can be when i make the change.

i'll be off to check whats next on my list then!

cheers.

G6Cad

Seems like it's solved so green solved icon for this one :)

This website is proudly hosted on Crocweb Cloud Website Hosting.