I want to execute a mysql query from php.
$sql = insert into q_links values ( 'garment.png', 'imgs\ques\p1\garment.png' );
I couldn't store the url as it is, rather it is being stored like this: imgsquesp1garment.png
. But I want to store the url like: imgs\ques\p1\garment.png
. So I tried this:
$sql = mysql_real_escape_string($sql);
But this way my $sql
looks like:
insert into q_links values ( \'garment.png\', \'imgs\\ques\\p1\\garment.png\' );
which do not work in the mysql database.
I have to insert this url in the database for later use. The url is imgs\ques\p1\garment.png
. How can I achieve this?
Update: And I tried with the first comment which worked for me.
So the solution is:
$sql = "insert into q_links values ( 'garment.png', '".mysql_real_escape_string( 'imgs\ques\p1\garment.png' )."' );";
You shouldn't have to do anything special to store a hyperlink within your database as they are simply strings. So you'll want to use a VARCHAR or TEXT field and you may want to consider making it fairly large (ie VARCHAR(512) or VARCHAR(MAX)) as URLS "can" be quite large although you may not run into any that big.
The datatype for the URL is VARCHAR.
URL is a built-in datatype of Semantic MediaWiki that is used for the most common kinds of URLs, URNs, and URIs. It accepts almost any string and interprets it as a URL. The URL appears as a link in text and the Factbox. Technically speaking, e-mail addresses and even telephone numbers are also kinds of URLs.
$url = "imgs\ques\p1\garment.png";
$url = mysql_real_escape_string($url);
$sql = "INSERT INTO q_links VALUES ('garment.png', '$url')";
As a side note, the mysql_* functions are deprecated, and you should move to Prepared statements with mysqli_* or PDO.
Example in PDO:
$pdo = new PDO("mysql:host=localhost;port=3306;dbname=mydb", "user", "password");
$stmt = $pdo->prepare("INSERT INTO q_links VALUES (?, ?)");
$stmt->execute(array("garment.png", "imgs\ques\p1\garment.png"));
$stmt->closeCursor();
Add the escape only for img field:
$sql = "insert into q_links values ( 'garment.png', '".mysql_real_escape_string( 'imgs\ques\p1\garment.png' )."' );"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With