Page 1 of 1
please help with code error
Posted: Sat Apr 25, 2015 4:45 pm
by preetksgill
my code is
$description preg_replace('"<a href"','"<a rel=nofollow href"',$description);
it does not work as required and gives a parse error T_String
please guide.
Re: please help with code error
Posted: Sat Apr 25, 2015 7:52 pm
by requinix
Have you not noticed you need to use the assignment operator?
When that eventually works, you'll discover it's not replacing things they way you want - or at all. Instead of preg_replace() which does so much more than what you're trying to do (and is more complicated making it easier to misuse, as you're doing), use
str_replace with about half as many quotes.
Re: please help with code error
Posted: Sat Apr 25, 2015 9:42 pm
by preetksgill
yes i forgot the assignment operator, and i realized it after making my post, it works now thanks
btw how do i use str_replace on the above code ?
Re: please help with code error
Posted: Sun Apr 26, 2015 12:58 pm
by Christopher
preetksgill wrote:btw how do i use str_replace on the above code ?
Code: Select all
$description = str_replace('<a href', '<a rel="nofollow" href', $description);
Re: please help with code error
Posted: Fri May 15, 2015 6:40 am
by preetksgill
okay i have 2 problems.
first problem is that this code gives an error when q is not present in url
index.php
index.php?q=xyz
<?php
//get the q parameter from URL
$q = $_GET['q'];
second problem is that i get a result based on above q value but i am not able to echo it
<?php
//get the q parameter from URL
$q = $_GET['q'];
//sql connect
require_once('../mysqli_connect.php');
$sql = "SELECT Link FROM rss WHERE Title = '" . $q . "'";
$result = @mysqli_query($conn, $sql);
if ($result)
{
echo "Query executed";
}
echo $result;
i am trying to get the Link i put in database for the given title.
Re: please help with code error
Posted: Fri May 15, 2015 6:46 am
by Celauran
preetksgill wrote:first problem is that this code gives an error when q is not present in url
index.php
index.php?q=xyz
<?php
//get the q parameter from URL
$q = $_GET['q'];
You need to check if it's set before trying to use it.
isset()
preetksgill wrote:second problem is that i get a result based on above q value but i am not able to echo it
<?php
//get the q parameter from URL
$q = $_GET['q'];
//sql connect
require_once('../mysqli_connect.php');
$sql = "SELECT Link FROM rss WHERE Title = '" . $q . "'";
$result = @mysqli_query($conn, $sql);
if ($result)
{
echo "Query executed";
}
echo $result;
i am trying to get the Link i put in database for the given title.
Pay attention to return types.
mysqli::query returns a [
http://php.net/manual/en/class.mysqli-result.php]mysqli_result[/url] object from which you must fetch rows.
$sql = "SELECT Link FROM rss WHERE Title = '" . $q . "'";
This is also ripe for SQL injection. Use prepared statements.
Re: please help with code error
Posted: Fri May 15, 2015 7:40 am
by preetksgill
i am a newbie to code, please guide me with changes required.
i have set isset)_ for the first problem, and enclosed whole of the rest in {}
Re: please help with code error
Posted: Fri May 15, 2015 8:01 am
by Celauran
Post your code and let's have a look.
Re: please help with code error
Posted: Fri May 15, 2015 8:13 am
by preetksgill
<?php
// get the q parameter from URL
// index.php?q=xyz
if isset($_GET['q'])
{
$q = $_GET['q'];
require_once('../mysqli_connect.php');
$sql = "SELECT Link FROM rss WHERE Title = '" . $q . "'";
$result = @mysqli_query($conn, $sql);
if ($result)
{
echo "Query executed <br />";
echo $result;
mysqli_close($conn);
}
}
this is my whole code.
i am expecting it to return a url value "
http://www.example.com/ " from the database.
i was able to insert it in local xampp database after watching a tutorial for it on youtube.
Re: please help with code error
Posted: Fri May 15, 2015 10:07 pm
by preetksgill
Pay attention to return types. mysqli::query returns a [
http://php.net/manual/en/class.mysqli-result.php]mysqli_result[/url] object from which you must fetch rows.
damn, this is where i was wrong, it works now
$sql = "SELECT Link FROM rss WHERE Title = ?";
i am using this now
Re: please help with code error
Posted: Sat May 16, 2015 4:22 am
by preetksgill
okay, i am stuck again.
3 problems, and all of them with querystring
if there is no querystring or it is simple index.php
if there is a querystriong but no value index.php?q=
if there is a querystring with a value index.php?q=xyz
for my page to work, it is is essential that i collect q=xyz from the page hyperlink, but at the same time i wish to safeguard against the first 2 situations
how do i do this ?