Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
  #1  
Old 08-29-2007, 07:34 PM
stephenNYC stephenNYC is offline
 
Join Date: Mar 2007
Location: NYC
Posts: 14
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default SQL queries in vBulletin

Hi, I've looked at a lot of postings here and suspect I am being dense, but I am not quite understanding some things and need a little help.
I added a table (cNews) to my vBulletin db and wrote a php page to INSERT or SELECT but need some help integrating and then calling the variables ($num, $headline, $news) in throughout the site.

I read Brad's 'vBulletin and mySQL' article (https://vborg.vbsupport.ru/showthread.php?t=75207) but still don't where and how to write SELECT, INSERT and UPDATE statements,

Thanks very much
Stephen

Here's my code.
Code:
$conn = mysql_connect($server, $userName, $password) or die(mysql_error());
mysql_select_db($db_name,$conn) or die(mysql_error());

$postback = $_POST["postback"];
if (empty($postback)) { //no new input so get last
$sql="SELECT num, headline, news FROM cNews ORDER BY num DESC LIMIT 1";
$result = mysql_query($sql, $conn) or die(mysql_error());
}
else { //insert new data then get last
$headline = $_POST["headline"];
$news = $_POST["news"];
$sql = "INSERT INTO cNews VALUES (num,'".$headline."','".$news."')";
$result = mysql_query($sql, $conn) or die(mysql_error());
$sql="SELECT num, headline, news FROM cNews ORDER BY num DESC LIMIT 1";
$result = mysql_query($sql, $conn) or die(mysql_error());
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$num = $row['num'];
$headline = $row['headline'];
$news = $row['news'];
}
Reply With Quote
  #2  
Old 08-30-2007, 12:06 AM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Firstly, at the top of your new script write

require_once('global.php');

if the script lives in another directory you must first chdir to where global.php lives then chdir back.

Once you have done that, you have access to the $vbulletin->db object, so you can do all your selects/inserts etc using the usual vb database methods.

Secondly, you can then require_once your script and then either

1) put your table stuff into individual functions and call them

or

2) keep themn as global variables, and where ever you want to use them just put a
gloabl $varname;

at the top of the function that needs to use them.

As to where the hooks should go....how long is a piece of string? it depends on where and how you want to access and view the news items.
Reply With Quote
  #3  
Old 08-30-2007, 12:22 AM
EnIgMa1234 EnIgMa1234 is offline
 
Join Date: Mar 2006
Location: .:: Ireland ::.
Posts: 1,306
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Heres how select and insert are written.

[sql]$vbulletin->db->query_read("SELECT * FROM " . TABLE_PREFIX . "table ......");
[/sql]

[sql]?vbulletin->db->query_write("INSERT INTO " . TABLE_PREFIX . "table .........");[/sql]
Reply With Quote
  #4  
Old 08-30-2007, 02:58 AM
stephenNYC stephenNYC is offline
 
Join Date: Mar 2007
Location: NYC
Posts: 14
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks very much. Those were very helpful replies and I got a lot closer: on my test pages the code writes out correctly, but I am confused about how to make $cUserNewsContent & $cNewsContent into global variables so I can call when needed.
Thanks again
Stephen
code below (lots shorter!)
Code:
	$postback = $_POST["postback"];
if ($postback=="yes") { // new input so insert
	$cNewsHeadline = $_POST["headline"];
	$cNewsNews = $_POST["news"];
(num,'".$cNewsHeadline."','".$cNewsNews."')";
echo "1";
}

$NewsResult=$db->query_read("SELECT *  FROM " . TABLE_PREFIX . "cNews ORDER BY num DESC LIMIT 1");
$row = mysql_fetch_assoc($NewsResult);
	}

	$cNewsContent= "<form action='indexTest.php' method='post'><input type='text' name='cNewsHeadline' value='".$row['headline']."'><BR>";
	$cNewsContent=$cNewsContent."<INPUT TYPE='text' NAME='cNewsNews' value='".$row['news']."'><BR><input type='hidden' name='postback' value='yes'>";
	$cNewsContent=$cNewsContent."<INPUT TYPE='submit'></form>";

	$cUserNewsContent= "<table><tr><td>".$row['headline']."</td></tr>";
	$cUserNewsContent=$cNewsContent."<tr><td>"..$row['news']."</td></tr></table>";
Reply With Quote
  #5  
Old 08-30-2007, 03:14 AM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by stephenNYC View Post
Thanks very much. Those were very helpful replies and I got a lot closer: on my test pages the code writes out correctly, but I am confused about how to make $cUserNewsContent & $cNewsContent into global variables so I can call when needed.
Thanks again
Stephen
near the top of the php script that you want the variable to appear in, put

require_once("whatever the news php scrit is called.php")


Inside any function or method which needs to use the variables, put

global $variablename;
Reply With Quote
  #6  
Old 08-30-2007, 02:47 PM
stephenNYC stephenNYC is offline
 
Join Date: Mar 2007
Location: NYC
Posts: 14
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks again for the reply, but I think I have some of the basics wrong.

I created a page (cNews.php) with the script that creates two variables ($cNewsContent and $cUserNewsContent) and assigns values to them

Then put an include in index.php 'include(cNews.php")'

now, if I write "echo $cNewsContent;" in index.php, the correct data shows at the top of index.php, but I want to be able to call that variable in other places.

Was indexphp the wrong place for ' include(cNews.php")'
thanks
Stephen
Reply With Quote
  #7  
Old 08-30-2007, 02:57 PM
calorie calorie is offline
 
Join Date: May 2003
Posts: 2,804
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Including cNews.php in index.php makes $cNewsContent and $cUserNewsContent available in certain code that follows, but not necessarily in other files such as showthread.php for example. If you want to have $cNewsContent and $cUserNewsContent available on most front-end vB pages, try using the global_start hook.
Reply With Quote
  #8  
Old 08-30-2007, 03:12 PM
stephenNYC stephenNYC is offline
 
Join Date: Mar 2007
Location: NYC
Posts: 14
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks- got it!
I had tried to avoid the plugins, but this seems good
Stephen
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:41 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05718 seconds
  • Memory Usage 2,232KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete