PHP forums, MySQL forums, Web Development resources

Home PageHome    PHP ResourcesTopic List    FAQFAQ    SearchSearch    MemberlistMemberlist    UsergroupsUsergroups 
 RegisterRegister
    ProfileProfile    Log in to check your private messagesLog in to check your private messages    Download the RSS Reader RSS Feed Download the RSS Reader RSS for this forum Log inLog in 

PHP Forum :: MySQL Forum :: Java Script Forum



PHP, MYSQL insert multiple variables

 
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General
View previous topic :: View next topic  
Author Message
bell



Joined: 16 Mar 2010
Posts: 2

PostPosted: Tue Mar 16, 2010 4:55 pm    Post subject: PHP, MYSQL insert multiple variables Reply with quote

Hi, I am currently building a project that requires tweets to be stored in a database. I am using CURL to read the last 10 tweets (from the .xml file) and echo them on a page, I am also trying to store them in a database if they haven't already been added.

I can get the oldest tweet (from that set of 10) to store in the database, the rest won't.

Here is the code

$query01 = "SELECT * FROM tweets4";

$result01 = mysql_query($query01)or die(mysql_error());

while($row = mysql_fetch_array($result01)){
$latest_id = $row['tweet_id'];
echo '<p>'.$latest_id.'</p>';
}

If ($tweet_id <= $latest_id){
echo "Same number";
}
else {
mysql_query("INSERT INTO tweets4(tweet,tweet_id,user_name,user_id,user_img,time_info) VALUES
('".mysql_real_escape_string($tweet)."',
'$tweet_id',
'$user_name',
'$user_id',
'$user_img',
'$time_info') ")
or die(mysql_error());
}

Just wondering if you could advise me how to store multiple records in a database with one query?
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 6033
Location: toronto, canada

PostPosted: Tue Mar 16, 2010 10:36 pm    Post subject: Reply with quote

mysql has a neat feature which allows you to stack queries for inserts

insert into table (field1, field2, field3)
values('$field1[$x]','$field2[$x]','$field3[$x]'),
('$field1[$x]','$field2[$x]','$field3[$x]'),
('$field1[$x]','$field2[$x]','$field3[$x]'),
('$field1[$x]','$field2[$x]','$field3[$x]')


So you can use php to build the statements up to the 10 that you need and just do one insert
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
bell



Joined: 16 Mar 2010
Posts: 2

PostPosted: Wed Mar 17, 2010 2:44 pm    Post subject: PHP, MYSQL insert multiple variables Reply with quote

Thanks for the reply.

Just wondering what the php "$x = ?" statement would look like

Sorry its probably a stupid question, I am quite new to php and mysql.

Thanks
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 6033
Location: toronto, canada

PostPosted: Wed Mar 17, 2010 3:14 pm    Post subject: Reply with quote

I was assuming that your data comes in an array. The $x is the row in the array that you are accessing
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
Display posts from previous:   
WeberTrivia Questions WeberTrivia Questions
 Think you are smart? Prove it!. Try your skills with these questions :
 WeberTrivia QuestionsRecursive arrays and multi-dimensional arrays are one and the same. (PHP and MySQL)
 WeberTrivia QuestionsThe \"cache_dir\" tag of the squid configuration has a default of /var/spool/squid. (Linux)

WeberTrivia Questions



PHP Code Examples
 Stream diffrent sizes of images from a single image to save disk space.
 JavaScript dropdown list menu to switch any page.
 Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
 PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside.
 Link Extractor - This function is used to extract links from a given URL. This will convert relative path into absolute path and also remove PHPSESSID stuff.
 Building a Dynamic Form using Javascript and innerHTML. Add form elements in realtime without refreshing the page.
 A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
PHP Forum :: MySQL Forum :: Java Script Forum