The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Question regarding php with mysql
Discuss Question regarding php with mysql in the PHP Development forum on Dev Shed. Question regarding php with mysql PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 22nd, 2013, 04:13 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 52
Time spent in forums: 2 Days 14 h 19 m 25 sec
Reputation Power: 1
|
|
|
Question regarding php with mysql
hi,
how would i go about coding in another Database just for one section of the page.
Code:
<?php
// Variables
$dBIP='mysql4.000webhost.com';
$dBADN='a5344706_test';
$dBPWD='w3l54666';
$dBDBS="a5344706_sigs";
// Connection
$CONNECT=mysql_connect($dBIP,$dBADN,$dBPWD);
$row=mysql_select_db($dBDBS, $CONNECT);
?>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>News - Draig Racing</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div class="page">
<div class="header header-news">
<a href="index.html" id="logo"><img src="images/logo.png" alt="logo"></a>
<ul>
<li>
<a href="index.html">Home</a> <span></span>
</li>
<li>
<a href="about.html">About</a> <span></span>
</li>
<li class="selected">
<a href="news.html">News</a> <span></span>
</li>
<li>
<a href="http://www.draigracing.co.uk/phpBB3/index.php">Forum</a> <span></span>
</li>
<li>
<a href="contact.html">Contact</a> <span></span>
</li>
</ul>
<div class="featured">
<h4>Featured News..</h4>
<h3>Placeholder Title One</h3>
<p>
</p>
<a href="about.html"></a>
</div>
</div>
<div class="body">
<div class="sidebar">
<div>
<h3>Connect</h3>
<a href="https://twitter.com/DraigRacing/" id="twitter">twitter</a> <a href="https://www.facebook.com/DraigRacing" id="fb">fb</a> <a href="http://freewebsitetemplates.com/go/googleplus/" id="googleplus">google+</a>
</div>
<div>
<div>
<h3>Current F1 News links</h3>
<ul>
<li>
<h4><a href="http://www.formula1.com/news/">Sky Sports F1</a></h4>
<p>
Current news on the formula 1 season, from all the teams.</p>
</li>
<li>
<h4><a href="http://www.bbc.co.uk/sport/0/formula1/">BBC Sport F1</a></h4>
<p>All news about formula 1 from the BBC's point of view.
</p>
</li>
<li>
<h4><a href="http://www.formula1.com/news/">Official Formula 1 Site</a></h4>
<p>
Offical outlet of all formula 1 news available here.
</p>
</li>
</ul>
</div>
</div>
<div>
<div>
<h3>Sim-racing Links</h3>
<ul>
<li>
<h4><a href="http://www.rfactor.net">rFactor</a></h4>
<p>
Official rFactor site with news about rFactor 1 & 2.
</p>
</li>
<li>
<h4><a href="http://www.iracing.com">iRacing</a></h4>
<p>
Official iRacing website with news about different series available.
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="content">
<ul>
<?php
// You only need a single query to draw all the news section
// Remember that Limit number -currently 3- is just the news items you want to show in the main index
$data = mysql_query("SELECT id, day, month, year, headline, text, signature, img1, img2, img3 FROM news ORDER by ID DESC LIMIT 3", $CONNECT);
while($row = mysql_fetch_assoc($data))
{
echo '
<li>
<span>'.date('F ',$row['month']).' '.$row['day'].' ,'.$row['year'].'</span> <a href="#"><img src="images/racer2.jpg" alt=""></a>
<div>
<h3>'.$row['headline'].'</h3>
<p>
'.$row['text'].'
</p>
<a href="#">Read more >></a>
</div>
</li>';
}
?>
</ul>
</div>
</div>
<div class="footer">
<ul>
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="news.html">News</a>
</li>
<li>
<a href="http://www.draigracing.co.uk/phpBB3/index.php">Forum</a>
</li>
<li>
<a href="contact.html">Contact</a>
</li>
</ul>
<p>
© Copyright 2013. Draig Racing all rights reserved
</p>
</div>
</div>
</body>
</html>
so i got this one database for collating news items, but i have a section just for featured news. i would like to use a databes to generate this obviously to cut down on always having to recode the page when needed.
is this possible and what code would i need?
many thanks
|

February 22nd, 2013, 04:20 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 313
  
Time spent in forums: 3 Days 15 h 8 m 35 sec
Reputation Power: 5
|
|
|
Are you aiming for a completely different database? Or perhaps just another table?
|

February 22nd, 2013, 04:23 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 52
Time spent in forums: 2 Days 14 h 19 m 25 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Triple_Nothing Are you aiming for a completely different database? Or perhaps just another table? |
what would u suggest?
if you want to know what im on about go to
http://draigracing.co.uk/Draigracingnewsite/news.html
its the featured news that i wanna do.
but like i said im really new to this so knowing best practice would be great
|

February 22nd, 2013, 05:31 PM
|
 |
Lost in code
|
|
|
|
|
It is possible to use more than one database on a page, but you shouldn't do it unless you have to. If all you need is another table, then add it to your current database.
|

February 23rd, 2013, 05:28 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 52
Time spent in forums: 2 Days 14 h 19 m 25 sec
Reputation Power: 1
|
|
|
how come im getting this???
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a5344706/public_html/Draigracingnewsite/news.html on line 50
|

February 23rd, 2013, 08:08 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 313
  
Time spent in forums: 3 Days 15 h 8 m 35 sec
Reputation Power: 5
|
|
|
The variable you provide for it to fetch the associations from does not hold a valid MySQL result. Can you post those few lines making this call?
|

February 23rd, 2013, 08:25 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 52
Time spent in forums: 2 Days 14 h 19 m 25 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Triple_Nothing The variable you provide for it to fetch the associations from does not hold a valid MySQL result. Can you post those few lines making this call? |
Code:
<?php
// Variables
$dBIP='mysql4.000webhost.com';
$dBADN='a5344706_test';
$dBPWD='w3l54666';
$dBDBS="a5344706_sigs";
// Connection
$CONNECT=mysql_connect($dBIP,$dBADN,$dBPWD);
$row=mysql_select_db($dBDBS, $CONNECT);
?>
<!DOCTYPE html>
<!-- Website template by freewebsitetemplates.com -->
<html>
<head>
<meta charset="UTF-8">
<title>News - Car Racing Website Template</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div class="page">
<div class="header header-news">
<a href="index.html" id="logo"><img src="images/logo.png" alt="logo"></a>
<ul>
<li>
<a href="index.html">Home</a> <span></span>
</li>
<li>
<a href="about.html">About</a> <span></span>
</li>
<li class="selected">
<a href="news.html">News</a> <span></span>
</li>
<li>
<a href="http://www.draigracing.co.uk/phpBB3/index.php">Forum</a> <span></span>
</li>
<li>
<a href="contact.html">Contact</a> <span></span>
</li>
</ul>
<div class="featured">
<ul>
<?php
// You only need a single query to draw all the news section
// Remember that Limit number -currently 3- is just the news items you want to show in the main index
$data1 = mysql_query("SELECT id, title, news, img1 FROM featured ORDER by ID DESC LIMIT 1", $CONNECT);
while($row = mysql_fetch_assoc($data))
{
echo '
<li>
<span></span> <a href="#"><img src="images/racer2.jpg" alt=""></a>
<div>
<h3>'.$row['title'].'</h3>
<p>
'.$row['news'].'
</p>
<a href="#">Read more >></a>
</div>
</li>';
}
?>
</ul>
<h4>Featured News..</h4>
<h3>Placeholder Title One</h3>
<p>
</p>
<a href="about.html"></a>
</div>
</div>
<div class="body">
<div class="sidebar">
<div>
<h3>Connect</h3>
<a href="https://twitter.com/DraigRacing/" id="twitter">twitter</a> <a href="https://www.facebook.com/DraigRacing" id="fb">fb</a> <a href="http://freewebsitetemplates.com/go/googleplus/" id="googleplus">google+</a>
</div>
<div>
<div>
<h3>Current F1 News links</h3>
<ul>
<li>
<h4><a href="http://www.formula1.com/news/">Sky Sports F1</a></h4>
<p>
Current news on the formula 1 season, from all the teams.</p>
</li>
<li>
<h4><a href="http://www.bbc.co.uk/sport/0/formula1/">BBC Sport F1</a></h4>
<p>All news about formula 1 from the BBC's point of view.
</p>
</li>
<li>
<h4><a href="http://www.formula1.com/news/">Official Formula 1 Site</a></h4>
<p>
Offical outlet of all formula 1 news available here.
</p>
</li>
</ul>
</div>
</div>
<div>
<div>
<h3>Sim-racing Links</h3>
<ul>
<li>
<h4><a href="http://www.rfactor.net">rFactor</a></h4>
<p>
Official rFactor site with news about rFactor 1 & 2.
</p>
</li>
<li>
<h4><a href="http://www.iracing.com">iRacing</a></h4>
<p>
Official iRacing website with news about different series available.
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="content">
<ul>
<?php
// You only need a single query to draw all the news section
// Remember that Limit number -currently 3- is just the news items you want to show in the main index
$data = mysql_query("SELECT id, day, month, year, headline, text, signature, img1, img2, img3 FROM news ORDER by ID DESC LIMIT 3", $CONNECT);
while($row = mysql_fetch_assoc($data))
{
echo '
<li>
<span>'.date('F ',$row['month']).' '.$row['day'].' ,'.$row['year'].'</span> <a href="#"><img src="images/racer2.jpg" alt=""></a>
<div>
<h3>'.$row['headline'].'</h3>
<p>
'.$row['text'].'
</p>
<a href="#">Read more >></a>
</div>
</li>';
}
?>
</ul>
</div>
</div>
<div class="footer">
<ul>
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="news.html">News</a>
</li>
<li>
<a href="http://www.draigracing.co.uk/phpBB3/index.php">Forum</a>
</li>
<li>
<a href="contact.html">Contact</a>
</li>
</ul>
<p>
© Copyright 2013. Draig Racing all rights reserved
</p>
</div>
</div>
</body>
</html>
this is my whole code,
the part thats playing up is this
Code:
<div class="featured">
<ul>
<?php
// You only need a single query to draw all the news section
// Remember that Limit number -currently 3- is just the news items you want to show in the main index
$data1 = mysql_query("SELECT id, title, news, img1 FROM featured ORDER by ID DESC LIMIT 1", $CONNECT);
while($row = mysql_fetch_assoc($data))
{
echo '
<li>
<span></span> <a href="#"><img src="images/racer2.jpg" alt=""></a>
<div>
<h3>'.$row['title'].'</h3>
<p>
'.$row['news'].'
</p>
<a href="#">Read more >></a>
</div>
</li>';
}
?>
</ul>
thanks triple
|

February 23rd, 2013, 08:29 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 313
  
Time spent in forums: 3 Days 15 h 8 m 35 sec
Reputation Power: 5
|
|
|
I see 1 is called $data, while the other is $data1. Is this intended?
|

February 23rd, 2013, 08:32 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 52
Time spent in forums: 2 Days 14 h 19 m 25 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Triple_Nothing I see 1 is called $data, while the other is $data1. Is this intended? |
well i confess to know absolutely nothing about this, i thouhgt it was going to clash with the original $data.
the 1st one is my attempt at editing the 2nd one.
so maybe i got this wrong?
i did change the $data to $data1, but if its wrong then ill change it to what is needed
|

February 23rd, 2013, 08:38 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 313
  
Time spent in forums: 3 Days 15 h 8 m 35 sec
Reputation Power: 5
|
|
|
Well, as long as you're done with the prior $data info, you can always reset it. Make all 4 references just $data, and no $data1.
|

February 23rd, 2013, 08:41 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 52
Time spent in forums: 2 Days 14 h 19 m 25 sec
Reputation Power: 1
|
|
|
right they are all set as $data
still getting same error
|

February 23rd, 2013, 08:45 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 313
  
Time spent in forums: 3 Days 15 h 8 m 35 sec
Reputation Power: 5
|
|
|
If you're only getting an error from 1 of your mysql_fetch_assoc() then I would manually run your MySQL query and see if you get a valid response.
EDIT: And just a side note, which many will tell you here, try to take a step away from the deprecated mysql functions, and take a look into mysqli or PDO and use Prepared Statements.
Last edited by Triple_Nothing : February 23rd, 2013 at 08:49 AM.
|

February 23rd, 2013, 08:49 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 52
Time spent in forums: 2 Days 14 h 19 m 25 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Triple_Nothing If you're only getting an error from 1 of your mysql_fetch_assoc() then I would manually run your MySQL query and see if you get a valid response. |
????????????? 
|

February 23rd, 2013, 08:50 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 313
  
Time spent in forums: 3 Days 15 h 8 m 35 sec
Reputation Power: 5
|
|
|
Are you familiar with and have access to PHPMyAdmin? If so, run the query in there.
SELECT id, title, news, img1 FROM featured ORDER by ID DESC LIMIT 1
|

February 23rd, 2013, 08:50 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 52
Time spent in forums: 2 Days 14 h 19 m 25 sec
Reputation Power: 1
|
|
|
the lower mysql_fetch_assoc() works no problem coming from news table
but the one i posted which is line 50 isnt working from the featured table
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|